UNPKG

14.3 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports["default"] = exports.WorkerApp = void 0;
8var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
12var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
14var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
15var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
16var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
17var _err = _interopRequireDefault(require("@lskjs/err"));
18var _arrayToObject = _interopRequireDefault(require("@lskjs/utils/arrayToObject"));
19var _asyncMapValues = _interopRequireDefault(require("@lskjs/utils/asyncMapValues"));
20var _importFn = _interopRequireDefault(require("@lskjs/utils/importFn"));
21var _get3 = _interopRequireDefault(require("lodash/get"));
22var _createErrorInfo = require("./createErrorInfo");
23var _SampleApp2 = require("./SampleApp");
24function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
25function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
26function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
27function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } // import { isDev } from '@lskjs/utils/env';
28var WorkerApp = /*#__PURE__*/function (_SampleApp) {
29 (0, _inherits2["default"])(WorkerApp, _SampleApp);
30 var _super = _createSuper(WorkerApp);
31 function WorkerApp() {
32 var _this;
33 (0, _classCallCheck2["default"])(this, WorkerApp);
34 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
35 args[_key] = arguments[_key];
36 }
37 _this = _super.call.apply(_super, [this].concat(args));
38 _this.preload = true;
39 _this.initedWorkers = {};
40 return _this;
41 }
42 (0, _createClass2["default"])(WorkerApp, [{
43 key: "model",
44 value: function () {
45 var _model = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
46 var _this$log;
47 var _len2,
48 args,
49 _key2,
50 modelsModule,
51 _args = arguments;
52 return _regenerator["default"].wrap(function _callee$(_context) {
53 while (1) switch (_context.prev = _context.next) {
54 case 0:
55 for (_len2 = _args.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
56 args[_key2] = _args[_key2];
57 }
58 (_this$log = this.log).warn.apply(_this$log, ['app.model deprecated'].concat(args));
59 _context.next = 4;
60 return this.module('models');
61 case 4:
62 modelsModule = _context.sent;
63 return _context.abrupt("return", modelsModule.model.apply(modelsModule, args));
64 case 6:
65 case "end":
66 return _context.stop();
67 }
68 }, _callee, this);
69 }));
70 function model() {
71 return _model.apply(this, arguments);
72 }
73 return model;
74 }()
75 }, {
76 key: "setProp",
77 value: function setProp(name, value) {
78 if (name === 'workers') return (0, _get2["default"])((0, _getPrototypeOf2["default"])(WorkerApp.prototype), "setProp", this).call(this, 'availableWorkers', value);
79 return (0, _get2["default"])((0, _getPrototypeOf2["default"])(WorkerApp.prototype), "setProp", this).call(this, name, value);
80 }
81 }, {
82 key: "init",
83 value: function () {
84 var _init = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
85 return _regenerator["default"].wrap(function _callee2$(_context2) {
86 while (1) switch (_context2.prev = _context2.next) {
87 case 0:
88 _context2.next = 2;
89 return (0, _get2["default"])((0, _getPrototypeOf2["default"])(WorkerApp.prototype), "init", this).call(this);
90 case 2:
91 if (!this.getErrorInfo) this.getErrorInfo = (0, _createErrorInfo.createErrorInfo)(this.errors);
92 case 3:
93 case "end":
94 return _context2.stop();
95 }
96 }, _callee2, this);
97 }));
98 function init() {
99 return _init.apply(this, arguments);
100 }
101 return init;
102 }()
103 }, {
104 key: "isWorkerClass",
105 value: function isWorkerClass(WorkerClass) {
106 return !!(WorkerClass && WorkerClass.__worker);
107 }
108 }, {
109 key: "isWorkerJob",
110 value: function isWorkerJob(WorkerJob) {
111 return !!(WorkerJob && WorkerJob.__workerJob);
112 }
113 }, {
114 key: "worker",
115 value: function () {
116 var _worker = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(nameOrNames) {
117 var _this2 = this;
118 var name, CurrentWorker, config, worker;
119 return _regenerator["default"].wrap(function _callee3$(_context3) {
120 while (1) switch (_context3.prev = _context3.next) {
121 case 0:
122 // eslint-disable-next-line no-param-reassign
123 if (nameOrNames === '*') nameOrNames = Object.keys(this.availableWorkers || {});
124 if (!Array.isArray(nameOrNames)) {
125 _context3.next = 3;
126 break;
127 }
128 return _context3.abrupt("return", (0, _asyncMapValues["default"])((0, _arrayToObject["default"])(nameOrNames), function (n) {
129 return _this2.module(n);
130 }));
131 case 3:
132 name = nameOrNames;
133 if (!this.initedWorkers[name]) {
134 _context3.next = 6;
135 break;
136 }
137 return _context3.abrupt("return", this.initedWorkers[name]);
138 case 6:
139 if (this.availableWorkers[name]) {
140 _context3.next = 8;
141 break;
142 }
143 throw new _err["default"]('WORKER_NOT_FOUND', "In worker \"".concat(name, "\" not found index with exported default function"));
144 case 8:
145 _context3.next = 10;
146 return (0, _importFn["default"])(this.availableWorkers[name]);
147 case 10:
148 CurrentWorker = _context3.sent;
149 _context3.next = 13;
150 return this.getWorkerConfig(name);
151 case 13:
152 config = _context3.sent;
153 if (this.isWorkerClass(CurrentWorker)) {
154 _context3.next = 16;
155 break;
156 }
157 throw new _err["default"]('notInstanceOfWorker', {
158 data: {
159 name: name
160 }
161 });
162 case 16:
163 _context3.next = 18;
164 return CurrentWorker.start({
165 app: this,
166 rabbit: this.rabbit,
167 config: config
168 });
169 case 18:
170 worker = _context3.sent;
171 this.initedWorkers[name] = worker;
172 return _context3.abrupt("return", worker);
173 case 21:
174 case "end":
175 return _context3.stop();
176 }
177 }, _callee3, this);
178 }));
179 function worker(_x) {
180 return _worker.apply(this, arguments);
181 }
182 return worker;
183 }()
184 }, {
185 key: "getWorkerConfig",
186 value: function () {
187 var _getWorkerConfig = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(name) {
188 var mConfig, wConfig, nConfig, res;
189 return _regenerator["default"].wrap(function _callee4$(_context4) {
190 while (1) switch (_context4.prev = _context4.next) {
191 case 0:
192 _context4.next = 2;
193 return this.getModuleConfig(name);
194 case 2:
195 mConfig = _context4.sent;
196 wConfig = (0, _get3["default"])(this, 'config.worker') || {};
197 if (!(typeof wConfig === 'string')) {
198 _context4.next = 6;
199 break;
200 }
201 throw new _err["default"]('DEPRECATED config.worker === string');
202 case 6:
203 nConfig = (0, _get3["default"])(this, "config.workers.".concat(name)) || {};
204 res = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, mConfig || {}), wConfig || {}), nConfig || {}), {}, {
205 // ns: 'worker',
206 log: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, (mConfig === null || mConfig === void 0 ? void 0 : mConfig.log) || {}), (wConfig === null || wConfig === void 0 ? void 0 : wConfig.log) || {}), (nConfig === null || nConfig === void 0 ? void 0 : nConfig.log) || {}), {}, {
207 ns: 'worker'
208 })
209 });
210 return _context4.abrupt("return", res);
211 case 9:
212 case "end":
213 return _context4.stop();
214 }
215 }, _callee4, this);
216 }));
217 function getWorkerConfig(_x2) {
218 return _getWorkerConfig.apply(this, arguments);
219 }
220 return getWorkerConfig;
221 }()
222 }, {
223 key: "runWorkers",
224 value: function () {
225 var _runWorkers = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
226 var _this3 = this;
227 var config, workerKey, workerNames;
228 return _regenerator["default"].wrap(function _callee6$(_context6) {
229 while (1) switch (_context6.prev = _context6.next) {
230 case 0:
231 _context6.next = 2;
232 return this.getWorkerConfig();
233 case 2:
234 config = _context6.sent;
235 workerKey = config.name;
236 if (!(!workerKey || typeof workerKey !== 'string')) {
237 _context6.next = 7;
238 break;
239 }
240 this.log.warn('!config.worker', '[ignore]');
241 return _context6.abrupt("return");
242 case 7:
243 this.log.trace("runWorkers(".concat(workerKey, ")"));
244 if (workerKey === '*') {
245 workerNames = Object.keys(this.availableWorkers || {});
246 } else if (workerKey) {
247 workerNames = workerKey.split(',').map(function (w) {
248 return w.trim();
249 });
250 }
251 _context6.next = 11;
252 return (0, _asyncMapValues["default"])(workerNames, /*#__PURE__*/function () {
253 var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(name) {
254 return _regenerator["default"].wrap(function _callee5$(_context5) {
255 while (1) switch (_context5.prev = _context5.next) {
256 case 0:
257 return _context5.abrupt("return", _this3.worker(name));
258 case 1:
259 case "end":
260 return _context5.stop();
261 }
262 }, _callee5);
263 }));
264 return function (_x3) {
265 return _ref.apply(this, arguments);
266 };
267 }());
268 case 11:
269 case "end":
270 return _context6.stop();
271 }
272 }, _callee6, this);
273 }));
274 function runWorkers() {
275 return _runWorkers.apply(this, arguments);
276 }
277 return runWorkers;
278 }()
279 }, {
280 key: "run",
281 value: function () {
282 var _run = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
283 return _regenerator["default"].wrap(function _callee7$(_context7) {
284 while (1) switch (_context7.prev = _context7.next) {
285 case 0:
286 _context7.next = 2;
287 return (0, _get2["default"])((0, _getPrototypeOf2["default"])(WorkerApp.prototype), "run", this).call(this);
288 case 2:
289 _context7.next = 4;
290 return this.runWorkers();
291 case 4:
292 case "end":
293 return _context7.stop();
294 }
295 }, _callee7, this);
296 }));
297 function run() {
298 return _run.apply(this, arguments);
299 }
300 return run;
301 }()
302 }]);
303 return WorkerApp;
304}(_SampleApp2.SampleApp);
305exports.WorkerApp = WorkerApp;
306var _default = WorkerApp;
307exports["default"] = _default;
308//# sourceMappingURL=WorkerApp.js.map
\No newline at end of file