@resolveio/server-lib 12.7.19 → 12.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -52,70 +52,92 @@ var CronManager = /** @class */ (function () {
52
52
  return __awaiter(this, void 0, void 0, function () {
53
53
  var _this = this;
54
54
  return __generator(this, function (_a) {
55
- if (!this._watchCrons$ || this._watchCrons$.closed) {
56
- cron_job_collection_1.CronJobs.find().then(function (res) {
57
- _this._jobs = res;
58
- _this._jobs.forEach(function (job) {
59
- _this.addCronJob(job);
60
- });
61
- });
62
- this._watchCrons$ = cron_job_collection_1.CronJobs.watchCollection([], { fullDocument: 'updateLookup' });
63
- this._watchCrons$.on('change', function (doc) { return __awaiter(_this, void 0, void 0, function () {
64
- var job, job;
65
- return __generator(this, function (_a) {
66
- if (doc.operationType === 'insert') {
67
- if (doc.fullDocument) {
68
- if (!this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
69
- this._jobs.push(doc.fullDocument);
55
+ switch (_a.label) {
56
+ case 0:
57
+ if (this._watchCrons$ && !this._watchCrons$.closed) {
58
+ this._watchCrons$.removeAllListeners();
59
+ this._watchCrons$.close();
60
+ this._watchCrons$ = null;
61
+ }
62
+ return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
63
+ case 1:
64
+ _a.sent();
65
+ if (!this._watchCrons$ || this._watchCrons$.closed) {
66
+ cron_job_collection_1.CronJobs.find().then(function (res) {
67
+ _this._jobs = res;
68
+ _this._jobs.forEach(function (job) {
69
+ _this.addCronJob(job);
70
+ });
71
+ });
72
+ this._watchCrons$ = cron_job_collection_1.CronJobs.watchCollection([], { fullDocument: 'updateLookup' });
73
+ this._watchCrons$.on('change', function (doc) { return __awaiter(_this, void 0, void 0, function () {
74
+ var oldDoc, job, job;
75
+ return __generator(this, function (_a) {
76
+ if (doc.operationType === 'insert') {
77
+ if (doc.fullDocument) {
78
+ if (!this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
79
+ this._jobs.push(doc.fullDocument);
80
+ }
81
+ this.addCronJob(doc.fullDocument);
82
+ }
70
83
  }
71
- this.addCronJob(doc.fullDocument);
72
- }
73
- }
74
- else if (doc.operationType === 'replace' || doc.operationType === 'update') {
75
- if (doc.fullDocument) {
76
- if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
77
- doc.fullDocument.next_run = null;
78
- this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1, doc.fullDocument);
79
- this.updateCronJob(doc.fullDocument);
84
+ else if (doc.operationType === 'replace' || doc.operationType === 'update') {
85
+ if (doc.fullDocument) {
86
+ oldDoc = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
87
+ if (oldDoc) {
88
+ this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1, doc.fullDocument);
89
+ if (oldDoc.name !== doc.fullDocument.name) {
90
+ this.removeCronJob(oldDoc.name);
91
+ this.addCronJob(doc.fullDocument);
92
+ }
93
+ else if (oldDoc.time_to_run !== doc.fullDocument.time_to_run ||
94
+ oldDoc.timezone !== doc.fullDocument.timezone) {
95
+ this.updateCronJob(doc.fullDocument);
96
+ }
97
+ }
98
+ else {
99
+ this._jobs.push(doc.fullDocument);
100
+ this.addCronJob(doc.fullDocument);
101
+ }
102
+ }
103
+ else {
104
+ if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
105
+ job = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
106
+ this.removeCronJob(job.name);
107
+ this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
108
+ }
109
+ }
80
110
  }
81
- else {
82
- this._jobs.push(doc.fullDocument);
83
- this.addCronJob(doc.fullDocument);
111
+ else if (doc.operationType === 'delete') {
112
+ if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
113
+ job = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
114
+ this.removeCronJob(job.name);
115
+ this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
116
+ }
84
117
  }
85
- }
86
- else {
87
- if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
88
- job = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
89
- this.removeCronJob(job.name);
90
- this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
91
- }
92
- }
93
- }
94
- else if (doc.operationType === 'delete') {
95
- if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
96
- job = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
97
- this.removeCronJob(job.name);
98
- this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
99
- }
100
- }
101
- return [2 /*return*/];
102
- });
103
- }); })
104
- .on('error', function (err) {
105
- _this._watchCrons$.close();
106
- })
107
- .on('end', function () {
108
- _this._watchCrons$.close();
109
- })
110
- .on('close', function () {
111
- _this._watchCrons$ = null;
112
- _this.watchCrons();
113
- });
114
- }
115
- else {
116
- this._watchCrons$.close();
118
+ return [2 /*return*/];
119
+ });
120
+ }); })
121
+ .on('error', function (err) {
122
+ _this._watchCrons$.removeAllListeners();
123
+ _this._watchCrons$.close();
124
+ _this._watchCrons$ = null;
125
+ _this.watchCrons();
126
+ })
127
+ .on('end', function () {
128
+ _this._watchCrons$.removeAllListeners();
129
+ _this._watchCrons$.close();
130
+ _this._watchCrons$ = null;
131
+ _this.watchCrons();
132
+ })
133
+ .on('close', function () {
134
+ _this._watchCrons$.removeAllListeners();
135
+ _this._watchCrons$ = null;
136
+ _this.watchCrons();
137
+ });
138
+ }
139
+ return [2 /*return*/];
117
140
  }
118
- return [2 /*return*/];
119
141
  });
120
142
  });
121
143
  };
@@ -140,8 +162,8 @@ var CronManager = /** @class */ (function () {
140
162
  if (this.doesCronJobExist(cron)) {
141
163
  this._cronManager.update(cron.name, cron.time_to_run, function () { return _this.runCronJob(cron); }, null, true, cron.timezone);
142
164
  var nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);
143
- if (cron.running || !cron.next_run || (nextDate && nextDate.getTime() !== cron.next_run.getTime())) {
144
- cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { running: false, next_run: nextDate || null } });
165
+ if (!cron.next_run || (nextDate && nextDate.getTime() !== cron.next_run.getTime())) {
166
+ cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { next_run: nextDate } });
145
167
  }
146
168
  }
147
169
  };
@@ -166,95 +188,93 @@ var CronManager = /** @class */ (function () {
166
188
  CronManager.prototype.runCronJob = function (cron) {
167
189
  var _this = this;
168
190
  var now = new Date();
169
- if (!cron.next_run || now.getTime() >= cron.next_run.getTime()) {
170
- cron_job_collection_1.CronJobs.findOneAndUpdate({
171
- $and: [
172
- { _id: cron._id },
173
- { running: false },
174
- {
175
- $or: [
176
- { next_run: { $exists: false } },
177
- { next_run: null },
178
- { next_run: { $lte: now } }
179
- ]
180
- }
181
- ]
182
- }, { $set: { running: true } }).then(function (res) { return __awaiter(_this, void 0, void 0, function () {
183
- var cronHistory, err_1, nextDate;
184
- return __generator(this, function (_a) {
185
- switch (_a.label) {
186
- case 0:
187
- if (!res) return [3 /*break*/, 13];
188
- cronHistory = {
189
- _id: (0, mongo_manager_1.objectIdHexString)(),
190
- name: res.name,
191
- reoccuring: res.repeat,
192
- time_to_run: res.time_to_run,
193
- method_name: res.method_run,
194
- id_cronjob: res._id,
195
- server_restart: true,
196
- passed: true,
197
- error: '',
198
- date_scheduled: cron.next_run,
199
- date_start: new Date(),
200
- date_end: null,
201
- date_next: null
202
- };
203
- return [4 /*yield*/, cron_job_history_collection_1.CronJobHistories.create(cronHistory)];
204
- case 1:
205
- _a.sent();
206
- _a.label = 2;
207
- case 2:
208
- _a.trys.push([2, 11, , 12]);
209
- if (!res.method_run_data) return [3 /*break*/, 4];
210
- return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_run, res.method_run_data)];
211
- case 3:
212
- _a.sent();
213
- return [3 /*break*/, 6];
214
- case 4: return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_run)];
215
- case 5:
216
- _a.sent();
217
- _a.label = 6;
218
- case 6:
219
- if (!res.method_complete) return [3 /*break*/, 10];
220
- if (!res.method_complete_data) return [3 /*break*/, 8];
221
- return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_complete, res.method_complete_data)];
222
- case 7:
223
- _a.sent();
224
- return [3 /*break*/, 10];
225
- case 8: return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_complete)];
226
- case 9:
227
- _a.sent();
228
- _a.label = 10;
229
- case 10: return [3 /*break*/, 12];
230
- case 11:
231
- err_1 = _a.sent();
232
- console.log(new Date(), 'Cron Error', JSON.stringify(err_1, null, 2));
233
- cronHistory.error = JSON.stringify(err_1, null, 2);
234
- cronHistory.passed = false;
235
- return [3 /*break*/, 12];
236
- case 12:
237
- if (res.repeat) {
238
- nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
239
- cronHistory.date_next = nextDate;
240
- cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } });
241
- }
242
- else {
243
- cron_job_collection_1.CronJobs.deleteOne({ _id: res._id });
244
- }
245
- cron_job_history_collection_1.CronJobHistories.updateOne({ _id: cronHistory._id }, { $set: {
246
- server_restart: false,
247
- passed: cronHistory.passed,
248
- error: cronHistory.error,
249
- date_end: new Date(),
250
- date_next: cronHistory.date_next
251
- } });
252
- _a.label = 13;
253
- case 13: return [2 /*return*/];
254
- }
255
- });
256
- }); }, function () { });
257
- }
191
+ cron_job_collection_1.CronJobs.findOneAndUpdate({
192
+ $and: [
193
+ { _id: cron._id },
194
+ { running: false },
195
+ {
196
+ $or: [
197
+ { next_run: { $exists: false } },
198
+ { next_run: null },
199
+ { next_run: { $lte: now } }
200
+ ]
201
+ }
202
+ ]
203
+ }, { $set: { running: true } }).then(function (res) { return __awaiter(_this, void 0, void 0, function () {
204
+ var cronHistory, err_1, nextDate;
205
+ return __generator(this, function (_a) {
206
+ switch (_a.label) {
207
+ case 0:
208
+ if (!res) return [3 /*break*/, 13];
209
+ cronHistory = {
210
+ _id: (0, mongo_manager_1.objectIdHexString)(),
211
+ name: res.name,
212
+ reoccuring: res.repeat,
213
+ time_to_run: res.time_to_run,
214
+ method_name: res.method_run,
215
+ id_cronjob: res._id,
216
+ server_restart: true,
217
+ passed: true,
218
+ error: '',
219
+ date_scheduled: res.next_run,
220
+ date_start: new Date(),
221
+ date_end: null,
222
+ date_next: null
223
+ };
224
+ return [4 /*yield*/, cron_job_history_collection_1.CronJobHistories.create(cronHistory)];
225
+ case 1:
226
+ _a.sent();
227
+ _a.label = 2;
228
+ case 2:
229
+ _a.trys.push([2, 11, , 12]);
230
+ if (!res.method_run_data) return [3 /*break*/, 4];
231
+ return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_run, res.method_run_data)];
232
+ case 3:
233
+ _a.sent();
234
+ return [3 /*break*/, 6];
235
+ case 4: return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_run)];
236
+ case 5:
237
+ _a.sent();
238
+ _a.label = 6;
239
+ case 6:
240
+ if (!res.method_complete) return [3 /*break*/, 10];
241
+ if (!res.method_complete_data) return [3 /*break*/, 8];
242
+ return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_complete, res.method_complete_data)];
243
+ case 7:
244
+ _a.sent();
245
+ return [3 /*break*/, 10];
246
+ case 8: return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_complete)];
247
+ case 9:
248
+ _a.sent();
249
+ _a.label = 10;
250
+ case 10: return [3 /*break*/, 12];
251
+ case 11:
252
+ err_1 = _a.sent();
253
+ console.log(new Date(), 'Cron Error', JSON.stringify(err_1, null, 2));
254
+ cronHistory.error = JSON.stringify(err_1, null, 2);
255
+ cronHistory.passed = false;
256
+ return [3 /*break*/, 12];
257
+ case 12:
258
+ if (res.repeat) {
259
+ nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
260
+ cronHistory.date_next = nextDate;
261
+ cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } });
262
+ }
263
+ else {
264
+ cron_job_collection_1.CronJobs.deleteOne({ _id: res._id });
265
+ }
266
+ cron_job_history_collection_1.CronJobHistories.updateOne({ _id: cronHistory._id }, { $set: {
267
+ server_restart: false,
268
+ passed: cronHistory.passed,
269
+ error: cronHistory.error,
270
+ date_end: new Date(),
271
+ date_next: cronHistory.date_next
272
+ } });
273
+ _a.label = 13;
274
+ case 13: return [2 /*return*/];
275
+ }
276
+ });
277
+ }); }, function () { });
258
278
  };
259
279
  return CronManager;
260
280
  }());
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/managers/cron.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,0EAA8D;AAE9D,qCAA8C;AAG9C,0FAA8E;AAC9E,2DAA8D;AAG9D;IAMC,qBAAY,UAAU;QAFd,UAAK,GAAmB,EAAE,CAAC;QAGlC,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,IAAI,qBAAc,EAAE,CAAC;QAEzC,IAAI,CAAC,UAAU,EAAE,CAAC;IACnB,CAAC;IAEY,gCAAU,GAAvB;;;;gBACC,IAAI,CAAC,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;oBACnD,8BAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,UAAA,GAAG;wBACvB,KAAI,CAAC,KAAK,GAAG,GAAG,CAAC;wBAEjB,KAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAA,GAAG;4BACrB,KAAI,CAAC,UAAU,CAAC,GAAG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,GAAG,EAAC,EAAE,EAAC,IAAI,EAAE;oCACzD,cAAc,EAAE,KAAK;oCACrB,MAAM,EAAE,WAAW,CAAC,MAAM;oCAC1B,KAAK,EAAE,WAAW,CAAC,KAAK;oCACxB,QAAQ,EAAE,IAAI,IAAI,EAAE;oCACpB,SAAS,EAAE,WAAW,CAAC,SAAS;iCAChC,EAAC,CAAC,CAAC;;;;;iBAEL,EAAE,cAAO,CAAC,CAAC,CAAC;SACb;IACF,CAAC;IACF,kBAAC;AAAD,CA/NA,AA+NC,IAAA;AA/NY,kCAAW","file":"cron.manager.js","sourcesContent":["import { CronJobModel } from '../models/cron-job.model';\nimport { CronJobs } from '../collections/cron-job.collection';\nimport ResolveIOMainServer from '../server-app';\nimport { CronJobManager } from '../cron/cron';\nimport * as moment from 'moment';\nimport { ChangeStream, ChangeStreamDocument } from 'mongodb';\nimport { CronJobHistories } from '../collections/cron-job-history.collection';\nimport { objectIdHexString } from '../managers/mongo.manager';\nimport { CronJobHistoryModel } from '../models/cron-job-history.model';\n\nexport class CronManager {\n\tprivate _cronManager: CronJobManager;\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _watchCrons$: ChangeStream;\n\tprivate _jobs: CronJobModel[] = [];\n\n\tconstructor(mainServer) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._cronManager = new CronJobManager();\n\n\t\tthis.watchCrons();\n\t}\n\n\tpublic async watchCrons() {\n\t\tif (!this._watchCrons$ || this._watchCrons$.closed) {\n\t\t\tCronJobs.find().then(res => {\n\t\t\t\tthis._jobs = res;\n\n\t\t\t\tthis._jobs.forEach(job => {\n\t\t\t\t\tthis.addCronJob(job);\n\t\t\t\t});\n\t\t\t});\n\n\t\t\tthis._watchCrons$ = CronJobs.watchCollection([], {fullDocument: 'updateLookup'});\n\n\t\t\tthis._watchCrons$.on('change', async (doc: ChangeStreamDocument<CronJobModel>) => {\n\t\t\t\tif (doc.operationType === 'insert') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tif (!this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'replace' || doc.operationType === 'update') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tif (this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tdoc.fullDocument.next_run = null;\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1, doc.fullDocument);\n\t\t\t\t\t\t\tthis.updateCronJob(doc.fullDocument);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'delete') {\n\t\t\t\t\tif (this._jobs.some(a => a._id === (<any>doc).documentKey['_id'])) {\n\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t\t.on('error', (err) => {\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('end', () => {\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('close', () => {\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tthis.watchCrons();\n\t\t\t});\n\t\t}\n\t\telse {\n\t\t\tthis._watchCrons$.close();\n\t\t}\n\t}\n\n\tprivate doesCronJobExist(cron: CronJobModel) {\n\t\treturn this._cronManager.exists(cron.name);\n\t}\n\n\tprivate doesCronJobNameExist(cron_name: string) {\n\t\treturn this._cronManager.exists(cron_name);\n\t}\n\n\tprivate addCronJob(cron: CronJobModel) {\n\t\tif (!this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.add(\n\t\t\t\tcron.name,\n\t\t\t\tcron.time_to_run,\n\t\t\t\t() => this.runCronJob(cron),\n\t\t\t\tnull,\n\t\t\t\ttrue,\n\t\t\t\tcron.timezone,\n\t\t\t\tnull, \n\t\t\t\tfalse\n\t\t\t);\n\n\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts'])\n\n\t\t\tif (cron.running || !cron.next_run || (nextDate && nextDate.getTime() !== cron.next_run.getTime())) {\n\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, next_run: nextDate || null}});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate updateCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.update(cron.name, cron.time_to_run, () => this.runCronJob(cron), null, true, cron.timezone);\n\t\t\t\n\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts'])\n\n\t\t\tif (cron.running || !cron.next_run || (nextDate && nextDate.getTime() !== cron.next_run.getTime())) {\n\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, next_run: nextDate || null}});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate removeCronJob(cron_name: string) {\n\t\tif (this.doesCronJobNameExist(cron_name)) {\n\t\t\tthis._cronManager.delete(cron_name);\n\t\t}\n\t}\n\n\tprivate startCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.start(cron.name);\n\t\t}\n\t}\n\n\tprivate stopCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.stop(cron.name);\n\t\t}\n\t}\n\n\tprivate stopAllCronJobs() {\n\t\tthis._cronManager.stopAll();\n\t}\n\n\tprivate runCronJob(cron: CronJobModel) {\n\t\tlet now = new Date();\n\n\t\tif (!cron.next_run || now.getTime() >= cron.next_run.getTime()) {\n\n\t\t\tCronJobs.findOneAndUpdate({\n\t\t\t\t$and: [\n\t\t\t\t\t{_id: cron._id},\n\t\t\t\t\t{running: false},\n\t\t\t\t\t{\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{next_run: {$exists: false}},\n\t\t\t\t\t\t\t{next_run: null},\n\t\t\t\t\t\t\t{next_run: {$lte: now}}\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}, {$set: {running: true}}).then(async res => {\n\t\t\t\tif (res) {\n\t\t\t\t\tlet cronHistory: CronJobHistoryModel = {\n\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\tname: res.name,\n\t\t\t\t\t\treoccuring: res.repeat,\n\t\t\t\t\t\ttime_to_run: res.time_to_run,\n\t\t\t\t\t\tmethod_name: res.method_run,\n\t\t\t\t\t\tid_cronjob: res._id,\n\t\t\t\t\t\tserver_restart: true,\n\t\t\t\t\t\tpassed: true,\n\t\t\t\t\t\terror: '',\n\t\t\t\t\t\tdate_scheduled: cron.next_run,\n\t\t\t\t\t\tdate_start: new Date(),\n\t\t\t\t\t\tdate_end: null,\n\t\t\t\t\t\tdate_next: null\n\t\t\t\t\t};\n\t\t\t\t\t\n\t\t\t\t\tawait CronJobHistories.create(cronHistory);\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tif (res.method_run_data) {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run, res.method_run_data);\t\n\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (res.method_complete) {\n\t\t\t\t\t\t\tif (res.method_complete_data) {\n\t\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete, res.method_complete_data);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tcatch (err) {\n\t\t\t\t\t\tconsole.log(new Date(), 'Cron Error', JSON.stringify(err, null, 2));\n\t\t\t\t\t\tcronHistory.error = JSON.stringify(err, null, 2);\n\t\t\t\t\t\tcronHistory.passed = false;\n\t\t\t\t\t}\n\t\n\t\t\t\t\tif (res.repeat) {\n\t\t\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);\n\t\t\t\t\t\tcronHistory.date_next = nextDate;\n\t\t\t\t\t\tCronJobs.updateOne({_id: res._id}, {$set: {running: false, next_run: nextDate}});\n\t\t\t\t\t} \n\t\t\t\t\telse {\n\t\t\t\t\t\tCronJobs.deleteOne({_id: res._id});\n\t\t\t\t\t}\n\n\t\t\t\t\tCronJobHistories.updateOne({_id: cronHistory._id}, {$set: {\n\t\t\t\t\t\tserver_restart: false,\n\t\t\t\t\t\tpassed: cronHistory.passed,\n\t\t\t\t\t\terror: cronHistory.error,\n\t\t\t\t\t\tdate_end: new Date(),\n\t\t\t\t\t\tdate_next: cronHistory.date_next\n\t\t\t\t\t}});\n\t\t\t\t}\n\t\t\t}, () => {});\n\t\t}\n\t}\n}"]}
1
+ {"version":3,"sources":["../../src/managers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Q,EAAC,EAAC,CAAC,CAAC;yBACjF;6BACI;4BACJ,8BAAQ,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAC,CAAC,CAAC;yBACnC;wBAED,8CAAgB,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,WAAW,CAAC,GAAG,EAAC,EAAE,EAAC,IAAI,EAAE;gCACzD,cAAc,EAAE,KAAK;gCACrB,MAAM,EAAE,WAAW,CAAC,MAAM;gCAC1B,KAAK,EAAE,WAAW,CAAC,KAAK;gCACxB,QAAQ,EAAE,IAAI,IAAI,EAAE;gCACpB,SAAS,EAAE,WAAW,CAAC,SAAS;6BAChC,EAAC,CAAC,CAAC;;;;;aAEL,EAAE,cAAO,CAAC,CAAC,CAAC;IACd,CAAC;IACF,kBAAC;AAAD,CAjPA,AAiPC,IAAA;AAjPY,kCAAW","file":"cron.manager.js","sourcesContent":["import { CronJobModel } from '../models/cron-job.model';\nimport { CronJobs } from '../collections/cron-job.collection';\nimport ResolveIOMainServer from '../server-app';\nimport { CronJobManager } from '../cron/cron';\nimport * as moment from 'moment';\nimport { ChangeStream, ChangeStreamDocument } from 'mongodb';\nimport { CronJobHistories } from '../collections/cron-job-history.collection';\nimport { objectIdHexString } from '../managers/mongo.manager';\nimport { CronJobHistoryModel } from '../models/cron-job-history.model';\n\nexport class CronManager {\n\tprivate _cronManager: CronJobManager;\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _watchCrons$: ChangeStream;\n\tprivate _jobs: CronJobModel[] = [];\n\n\tconstructor(mainServer) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._cronManager = new CronJobManager();\n\n\t\tthis.watchCrons();\n\t}\n\n\tpublic async watchCrons() {\n\t\tif (this._watchCrons$ && !this._watchCrons$.closed) {\n\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\tthis._watchCrons$.close();\n\t\t\tthis._watchCrons$ = null;\n\t\t}\n\t\n\t\tawait new Promise(resolve => setTimeout(resolve, 1000));\n\t\n\t\tif (!this._watchCrons$ || this._watchCrons$.closed) {\t\t\t\n\t\t\tCronJobs.find().then(res => {\n\t\t\t\tthis._jobs = res;\n\n\t\t\t\tthis._jobs.forEach(job => {\n\t\t\t\t\tthis.addCronJob(job);\n\t\t\t\t});\n\t\t\t});\n\n\t\t\tthis._watchCrons$ = CronJobs.watchCollection([], {fullDocument: 'updateLookup'});\n\n\t\t\tthis._watchCrons$.on('change', async (doc: ChangeStreamDocument<CronJobModel>) => {\n\t\t\t\tif (doc.operationType === 'insert') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tif (!this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'replace' || doc.operationType === 'update') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tlet oldDoc = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\tif (oldDoc) {\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1, doc.fullDocument);\n\n\t\t\t\t\t\t\tif (oldDoc.name !== doc.fullDocument.name) {\n\t\t\t\t\t\t\t\tthis.removeCronJob(oldDoc.name);\n\t\t\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse if (oldDoc.time_to_run !== doc.fullDocument.time_to_run || \n\t\t\t\t\t\t\t\toldDoc.timezone !== doc.fullDocument.timezone\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tthis.updateCronJob(doc.fullDocument);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'delete') {\n\t\t\t\t\tif (this._jobs.some(a => a._id === (<any>doc).documentKey['_id'])) {\n\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t\t.on('error', (err) => {\n\t\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tthis.watchCrons();\n\t\t\t})\n\t\t\t.on('end', () => {\n\t\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tthis.watchCrons();\n\t\t\t})\n\t\t\t.on('close', () => {\n\t\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tthis.watchCrons();\n\t\t\t});\n\t\t}\n\t}\n\n\tprivate doesCronJobExist(cron: CronJobModel) {\n\t\treturn this._cronManager.exists(cron.name);\n\t}\n\n\tprivate doesCronJobNameExist(cron_name: string) {\n\t\treturn this._cronManager.exists(cron_name);\n\t}\n\n\tprivate addCronJob(cron: CronJobModel) {\n\t\tif (!this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.add(\n\t\t\t\tcron.name,\n\t\t\t\tcron.time_to_run,\n\t\t\t\t() => this.runCronJob(cron),\n\t\t\t\tnull,\n\t\t\t\ttrue,\n\t\t\t\tcron.timezone,\n\t\t\t\tnull, \n\t\t\t\tfalse\n\t\t\t);\n\n\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);\n\n\t\t\tif (cron.running || !cron.next_run || (nextDate && nextDate.getTime() !== cron.next_run.getTime())) {\n\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, next_run: nextDate || null}});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate updateCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.update(cron.name, cron.time_to_run, () => this.runCronJob(cron), null, true, cron.timezone);\n\n\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);\n\n\t\t\tif (!cron.next_run || (nextDate && nextDate.getTime() !== cron.next_run.getTime())) {\n\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {next_run: nextDate}});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate removeCronJob(cron_name: string) {\n\t\tif (this.doesCronJobNameExist(cron_name)) {\n\t\t\tthis._cronManager.delete(cron_name);\n\t\t}\n\t}\n\n\tprivate startCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.start(cron.name);\n\t\t}\n\t}\n\n\tprivate stopCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.stop(cron.name);\n\t\t}\n\t}\n\n\tprivate stopAllCronJobs() {\n\t\tthis._cronManager.stopAll();\n\t}\n\n\tprivate runCronJob(cron: CronJobModel) {\n\t\tlet now = new Date();\n\n\t\tCronJobs.findOneAndUpdate({\n\t\t\t$and: [\n\t\t\t\t{_id: cron._id},\n\t\t\t\t{running: false},\n\t\t\t\t{\n\t\t\t\t\t$or: [\n\t\t\t\t\t\t{next_run: {$exists: false}},\n\t\t\t\t\t\t{next_run: null},\n\t\t\t\t\t\t{next_run: {$lte: now}}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t]\n\t\t}, {$set: {running: true}}).then(async res => {\n\t\t\tif (res) {\n\t\t\t\tlet cronHistory: CronJobHistoryModel = {\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tname: res.name,\n\t\t\t\t\treoccuring: res.repeat,\n\t\t\t\t\ttime_to_run: res.time_to_run,\n\t\t\t\t\tmethod_name: res.method_run,\n\t\t\t\t\tid_cronjob: res._id,\n\t\t\t\t\tserver_restart: true,\n\t\t\t\t\tpassed: true,\n\t\t\t\t\terror: '',\n\t\t\t\t\tdate_scheduled: res.next_run,\n\t\t\t\t\tdate_start: new Date(),\n\t\t\t\t\tdate_end: null,\n\t\t\t\t\tdate_next: null\n\t\t\t\t};\n\t\t\t\t\n\t\t\t\tawait CronJobHistories.create(cronHistory);\n\n\t\t\t\ttry {\n\t\t\t\t\tif (res.method_run_data) {\n\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run, res.method_run_data);\t\n\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (res.method_complete) {\n\t\t\t\t\t\tif (res.method_complete_data) {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete, res.method_complete_data);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcatch (err) {\n\t\t\t\t\tconsole.log(new Date(), 'Cron Error', JSON.stringify(err, null, 2));\n\t\t\t\t\tcronHistory.error = JSON.stringify(err, null, 2);\n\t\t\t\t\tcronHistory.passed = false;\n\t\t\t\t}\n\n\t\t\t\tif (res.repeat) {\n\t\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);\n\t\t\t\t\tcronHistory.date_next = nextDate;\n\t\t\t\t\tCronJobs.updateOne({_id: res._id}, {$set: {running: false, next_run: nextDate}});\n\t\t\t\t} \n\t\t\t\telse {\n\t\t\t\t\tCronJobs.deleteOne({_id: res._id});\n\t\t\t\t}\n\n\t\t\t\tCronJobHistories.updateOne({_id: cronHistory._id}, {$set: {\n\t\t\t\t\tserver_restart: false,\n\t\t\t\t\tpassed: cronHistory.passed,\n\t\t\t\t\terror: cronHistory.error,\n\t\t\t\t\tdate_end: new Date(),\n\t\t\t\t\tdate_next: cronHistory.date_next\n\t\t\t\t}});\n\t\t\t}\n\t\t}, () => {});\n\t}\n}"]}
@@ -548,6 +548,14 @@ var MongoManagerCollection = /** @class */ (function () {
548
548
  stream.on('end', function (ev) {
549
549
  monitor.finish();
550
550
  return ev;
551
+ })
552
+ .on('error', function (ev) {
553
+ monitor.finish();
554
+ return ev;
555
+ })
556
+ .on('close', function (ev) {
557
+ monitor.finish();
558
+ return ev;
551
559
  });
552
560
  return stream;
553
561
  };
@@ -889,6 +897,7 @@ var MongoManagerCollection = /** @class */ (function () {
889
897
  var cursor = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).find(filter, options);
890
898
  cursor.on('close', function (ev) {
891
899
  monitor.finish();
900
+ cursor.removeAllListeners();
892
901
  return ev;
893
902
  });
894
903
  return cursor;
@@ -900,6 +909,14 @@ var MongoManagerCollection = /** @class */ (function () {
900
909
  stream.on('end', function (ev) {
901
910
  monitor.finish();
902
911
  return ev;
912
+ })
913
+ .on('error', function (ev) {
914
+ monitor.finish();
915
+ return ev;
916
+ })
917
+ .on('close', function (ev) {
918
+ monitor.finish();
919
+ return ev;
903
920
  });
904
921
  return stream;
905
922
  };
@@ -1931,23 +1948,13 @@ var MongoManagerCollection = /** @class */ (function () {
1931
1948
  }); });
1932
1949
  };
1933
1950
  MongoManagerCollection.prototype.watchCollection = function (pipeline, options) {
1934
- var _this = this;
1935
1951
  if (pipeline === void 0) { pipeline = []; }
1936
1952
  var stream = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).watch(pipeline, options);
1937
- stream.on('change', function (evData) {
1938
- index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
1939
- return evData;
1940
- });
1941
1953
  return stream;
1942
1954
  };
1943
1955
  MongoManagerCollection.prototype.watchCollectionWithAggregate = function (pipeline, options) {
1944
- var _this = this;
1945
1956
  if (pipeline === void 0) { pipeline = []; }
1946
1957
  var stream = index_1.ResolveIOServer.getMainDB().collection(this.collectionName, this.collectionOptions).watch(pipeline, options);
1947
- stream.on('change', function (evData) {
1948
- index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_this.collectionName);
1949
- return evData;
1950
- });
1951
1958
  return stream;
1952
1959
  };
1953
1960
  var _a;