UNPKG

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