UNPKG

3.99 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = startGoProc;
9
10var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
12var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
14var _child_process = require("child_process");
15
16var _get = _interopRequireDefault(require("lodash/get"));
17
18var paramsToArgs = function paramsToArgs() {
19 var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
20 return Object.keys(params).map(function (key) {
21 return ["-".concat(key), params[key]].join(' ');
22 });
23};
24
25var serializeData = function serializeData() {
26 var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
27 if (typeof data === 'string') return data;
28 return JSON.stringify(data);
29};
30
31var serializeDataArray = function serializeDataArray() {
32 var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
33
34 if (Array.isArray(data)) {
35 return data.map(serializeData);
36 }
37
38 return serializeData(data);
39};
40
41function startGoProc() {
42 return _startGoProc.apply(this, arguments);
43}
44
45function _startGoProc() {
46 _startGoProc = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
47 var _this = this;
48
49 var debug, uri, params, cmd, args, proc;
50 return _regenerator["default"].wrap(function _callee$(_context) {
51 while (1) {
52 switch (_context.prev = _context.next) {
53 case 0:
54 // const { debug } = this.config;
55 debug = this.config.debug || false;
56 uri = this.config.uri;
57
58 if (uri.includes('?')) {
59 uri = (0, _get["default"])(uri.split('?'), '0');
60 }
61
62 params = {
63 amqp: uri
64 }; // const cmd = GO_RABBIT;
65
66 cmd = this.goRabbitPath; // const cmd = `${GO_RABBIT}`;
67 // const cmd = `echo '${row}' | node /Users/isuvorov/projects/parser-cloud/packages/parser-cloud/rabbit-node.js`;
68
69 args = paramsToArgs(params);
70
71 if (debug) {
72 this.log.trace('[RM]', "".concat(cmd, " ").concat(args.join(' '))); // echo '{"_id": "5e91cc24001c9049700945bc", "videoId": "PRsKXb4e_Bo"}' | ./rabbit -queue z-dev-TEST
73 }
74
75 proc = (0, _child_process.spawn)(cmd, args, {
76 shell: true
77 });
78 proc.stdout.on('data', function (chunk) {
79 var strings = String(chunk).split('\n');
80 strings.forEach(function (str) {
81 if (debug) {
82 _this.log.trace('[RM] stdout:', str);
83 }
84
85 try {
86 var json = JSON.parse(String(str));
87
88 if (json.type === 'response') {
89 _this.emitter.emit(json.hash, {
90 complete: json.complete
91 });
92 }
93 } catch (e) {// this.log.trace(String(chunk), 'не получилось');
94
95 /* eslint no-empty: "error" */
96 } // this.log.trace('----------------------');
97
98 });
99 });
100 proc.stderr.on('data', function (chunk) {
101 _this.log.error("[RM] stderr: ".concat(chunk));
102 });
103 proc.on('close', function () {
104 if (debug) {
105 _this.log.warn('что то пошло не так');
106 }
107
108 _this.emitter.emit('close');
109
110 setTimeout(function () {
111 _this.startGoProc();
112 }, 100);
113 });
114 this.proc = proc;
115
116 case 12:
117 case "end":
118 return _context.stop();
119 }
120 }
121 }, _callee, this);
122 }));
123 return _startGoProc.apply(this, arguments);
124}
125//# sourceMappingURL=startGoProc.js.map
\No newline at end of file