@resolveio/server-lib 20.0.26 → 20.0.28
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.
- package/client-server-app.js +1 -47
- package/client-server-app.js.map +1 -1
- package/collections/app-status.collection.js +1 -50
- package/collections/app-status.collection.js.map +1 -1
- package/collections/counter.collection.js +1 -53
- package/collections/counter.collection.js.map +1 -1
- package/collections/cron-job-history.collection.js +1 -88
- package/collections/cron-job-history.collection.js.map +1 -1
- package/collections/cron-job.collection.js +1 -84
- package/collections/cron-job.collection.js.map +1 -1
- package/collections/email-history.collection.js +1 -65
- package/collections/email-history.collection.js.map +1 -1
- package/collections/email-verified.collection.js +1 -60
- package/collections/email-verified.collection.js.map +1 -1
- package/collections/file.collection.js +1 -72
- package/collections/file.collection.js.map +1 -1
- package/collections/flag.collection.js +1 -55
- package/collections/flag.collection.js.map +1 -1
- package/collections/log-method-latency.collection.js +1 -63
- package/collections/log-method-latency.collection.js.map +1 -1
- package/collections/log-subscription.collection.js +1 -67
- package/collections/log-subscription.collection.js.map +1 -1
- package/collections/log-ts.collection.js +1 -77
- package/collections/log-ts.collection.js.map +1 -1
- package/collections/log.collection.js +1 -76
- package/collections/log.collection.js.map +1 -1
- package/collections/logged-in-users.collection.js +1 -64
- package/collections/logged-in-users.collection.js.map +1 -1
- package/collections/method-response.collection.js +1 -65
- package/collections/method-response.collection.js.map +1 -1
- package/collections/monitor-cpu.collection.js +1 -62
- package/collections/monitor-cpu.collection.js.map +1 -1
- package/collections/monitor-memory.collection.js +1 -74
- package/collections/monitor-memory.collection.js.map +1 -1
- package/collections/monitor-mongo.collection.js +1 -68
- package/collections/monitor-mongo.collection.js.map +1 -1
- package/collections/notification.collection.js +1 -55
- package/collections/notification.collection.js.map +1 -1
- package/collections/report-builder-dashboard-builder.collection.js +1 -108
- package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
- package/collections/report-builder-library.collection.js +1 -86
- package/collections/report-builder-library.collection.js.map +1 -1
- package/collections/report-builder-report.collection.js +1 -148
- package/collections/report-builder-report.collection.js.map +1 -1
- package/collections/user-group.collection.js +1 -93
- package/collections/user-group.collection.js.map +1 -1
- package/collections/user-guide.collection.js +1 -56
- package/collections/user-guide.collection.js.map +1 -1
- package/collections/user.collection.js +1 -265
- package/collections/user.collection.js.map +1 -1
- package/cron/cron.js +1 -101
- package/cron/cron.js.map +1 -1
- package/fixtures/cron-jobs.js +1 -87
- package/fixtures/cron-jobs.js.map +1 -1
- package/fixtures/init.js +1 -58
- package/fixtures/init.js.map +1 -1
- package/http/auth.js +1 -763
- package/http/auth.js.map +1 -1
- package/http/health.js +1 -11
- package/http/health.js.map +1 -1
- package/http/home.js +1 -65
- package/http/home.js.map +1 -1
- package/index.js +1 -126
- package/index.js.map +1 -1
- package/managers/cron.manager.js +1 -322
- package/managers/cron.manager.js.map +1 -1
- package/managers/method.manager.js +1 -593
- package/managers/method.manager.js.map +1 -1
- package/managers/mongo.manager.js +1 -2259
- package/managers/mongo.manager.js.map +1 -1
- package/managers/monitor.manager.js +1 -350
- package/managers/monitor.manager.js.map +1 -1
- package/managers/subscription.manager.js +1 -1110
- package/managers/subscription.manager.js.map +1 -1
- package/managers/subscription.performance.js +1 -102
- package/managers/subscription.performance.js.map +1 -1
- package/methods/accounts.js +1 -291
- package/methods/accounts.js.map +1 -1
- package/methods/aws.js +1 -616
- package/methods/aws.js.map +1 -1
- package/methods/collections.js +1 -499
- package/methods/collections.js.map +1 -1
- package/methods/counters.js +1 -121
- package/methods/counters.js.map +1 -1
- package/methods/cron-jobs.js +1 -1337
- package/methods/cron-jobs.js.map +1 -1
- package/methods/flags.js +1 -8
- package/methods/flags.js.map +1 -1
- package/methods/logs.js +1 -370
- package/methods/logs.js.map +1 -1
- package/methods/monitor.js +1 -564
- package/methods/monitor.js.map +1 -1
- package/methods/pdf.js +1 -731
- package/methods/pdf.js.map +1 -1
- package/methods/report-builder.js +1 -859
- package/methods/report-builder.js.map +1 -1
- package/methods/support.js +1 -245
- package/methods/support.js.map +1 -1
- package/models/app-status.model.js +1 -3
- package/models/app-status.model.js.map +1 -1
- package/models/billing-logged-in-users.model.js +1 -3
- package/models/billing-logged-in-users.model.js.map +1 -1
- package/models/collection-document.model.js +1 -3
- package/models/collection-document.model.js.map +1 -1
- package/models/counter.model.js +1 -3
- package/models/counter.model.js.map +1 -1
- package/models/cron-job-history.model.js +1 -3
- package/models/cron-job-history.model.js.map +1 -1
- package/models/cron-job.model.js +1 -3
- package/models/cron-job.model.js.map +1 -1
- package/models/dialog.model.js +1 -3
- package/models/dialog.model.js.map +1 -1
- package/models/email-history.model.js +1 -3
- package/models/email-history.model.js.map +1 -1
- package/models/email-verified.model.js +1 -3
- package/models/email-verified.model.js.map +1 -1
- package/models/file.model.js +1 -3
- package/models/file.model.js.map +1 -1
- package/models/flag.model.js +1 -3
- package/models/flag.model.js.map +1 -1
- package/models/log-method-latency.model.js +1 -3
- package/models/log-method-latency.model.js.map +1 -1
- package/models/log-subscription.model.js +1 -3
- package/models/log-subscription.model.js.map +1 -1
- package/models/log.model.js +1 -3
- package/models/log.model.js.map +1 -1
- package/models/logged-in-users.model.js +1 -3
- package/models/logged-in-users.model.js.map +1 -1
- package/models/method-response.model.js +1 -3
- package/models/method-response.model.js.map +1 -1
- package/models/method.model.js +1 -3
- package/models/method.model.js.map +1 -1
- package/models/monitor-cpu.model.js +1 -3
- package/models/monitor-cpu.model.js.map +1 -1
- package/models/monitor-memory.model.js +1 -3
- package/models/monitor-memory.model.js.map +1 -1
- package/models/monitor-mongo.model.js +1 -3
- package/models/monitor-mongo.model.js.map +1 -1
- package/models/notification.model.js +1 -3
- package/models/notification.model.js.map +1 -1
- package/models/pagination.model.js +1 -23
- package/models/pagination.model.js.map +1 -1
- package/models/permission.model.js +1 -3
- package/models/permission.model.js.map +1 -1
- package/models/report-builder-dashboard-builder.model.js +1 -3
- package/models/report-builder-dashboard-builder.model.js.map +1 -1
- package/models/report-builder-library.model.js +1 -3
- package/models/report-builder-library.model.js.map +1 -1
- package/models/report-builder-report.model.js +1 -3
- package/models/report-builder-report.model.js.map +1 -1
- package/models/report-builder.model.js +1 -3
- package/models/report-builder.model.js.map +1 -1
- package/models/select-data-label.model.js +1 -3
- package/models/select-data-label.model.js.map +1 -1
- package/models/server-response.model.js +1 -3
- package/models/server-response.model.js.map +1 -1
- package/models/subscription.model.js +1 -3
- package/models/subscription.model.js.map +1 -1
- package/models/support-ticket.model.js +1 -3
- package/models/support-ticket.model.js.map +1 -1
- package/models/user-group.model.js +1 -3
- package/models/user-group.model.js.map +1 -1
- package/models/user-guide.model.js +1 -3
- package/models/user-guide.model.js.map +1 -1
- package/models/user.model.js +1 -3
- package/models/user.model.js.map +1 -1
- package/package.json +6 -4
- package/public_api.js +1 -74
- package/public_api.js.map +1 -1
- package/publications/app-status.js +1 -16
- package/publications/app-status.js.map +1 -1
- package/publications/cron-jobs.js +1 -32
- package/publications/cron-jobs.js.map +1 -1
- package/publications/files.js +1 -36
- package/publications/files.js.map +1 -1
- package/publications/flags.js +1 -22
- package/publications/flags.js.map +1 -1
- package/publications/logs.js +1 -164
- package/publications/logs.js.map +1 -1
- package/publications/method-responses.js +1 -16
- package/publications/method-responses.js.map +1 -1
- package/publications/notifications.js +1 -16
- package/publications/notifications.js.map +1 -1
- package/publications/report-builder-dashboard-builders.js +1 -42
- package/publications/report-builder-dashboard-builders.js.map +1 -1
- package/publications/report-builder-libraries.js +1 -89
- package/publications/report-builder-libraries.js.map +1 -1
- package/publications/report-builder-reports.js +1 -50
- package/publications/report-builder-reports.js.map +1 -1
- package/publications/super-admin.js +1 -16
- package/publications/super-admin.js.map +1 -1
- package/publications/user-groups.js +1 -16
- package/publications/user-groups.js.map +1 -1
- package/publications/user-guides.js +1 -16
- package/publications/user-guides.js.map +1 -1
- package/server-app.js +1 -770
- package/server-app.js.map +1 -1
- package/util/common.js +1 -546
- package/util/common.js.map +1 -1
- package/util/schema-report-builder.js +1 -452
- package/util/schema-report-builder.js.map +1 -1
package/managers/cron.manager.js
CHANGED
|
@@ -1,323 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.CronManager = void 0;
|
|
40
|
-
var cron_job_collection_1 = require("../collections/cron-job.collection");
|
|
41
|
-
var cron_1 = require("../cron/cron");
|
|
42
|
-
var cron_job_history_collection_1 = require("../collections/cron-job-history.collection");
|
|
43
|
-
var mongo_manager_1 = require("../managers/mongo.manager");
|
|
44
|
-
var CronManager = /** @class */ (function () {
|
|
45
|
-
function CronManager(mainServer) {
|
|
46
|
-
this._jobs = [];
|
|
47
|
-
this._mainServer = mainServer;
|
|
48
|
-
this._cronManager = new cron_1.CronJobManager();
|
|
49
|
-
this.watchCrons();
|
|
50
|
-
}
|
|
51
|
-
CronManager.prototype.watchCrons = function () {
|
|
52
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
-
var _this = this;
|
|
54
|
-
return __generator(this, function (_a) {
|
|
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
|
-
if (job.running) {
|
|
71
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: job._id }, { $set: { running: false } });
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
this._watchCrons$ = cron_job_collection_1.CronJobs.watchCollection([], { fullDocument: 'updateLookup' });
|
|
76
|
-
this._watchCrons$.on('change', function (doc) { return __awaiter(_this, void 0, void 0, function () {
|
|
77
|
-
var oldDoc, job, job;
|
|
78
|
-
return __generator(this, function (_a) {
|
|
79
|
-
if (doc.operationType === 'insert') {
|
|
80
|
-
if (doc.fullDocument) {
|
|
81
|
-
if (!this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
|
|
82
|
-
this._jobs.push(doc.fullDocument);
|
|
83
|
-
}
|
|
84
|
-
this.addCronJob(doc.fullDocument);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
else if (doc.operationType === 'replace' || doc.operationType === 'update') {
|
|
88
|
-
if (doc.fullDocument) {
|
|
89
|
-
oldDoc = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
|
|
90
|
-
if (oldDoc) {
|
|
91
|
-
this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1, doc.fullDocument);
|
|
92
|
-
if (oldDoc.name !== doc.fullDocument.name) {
|
|
93
|
-
this.removeCronJob(oldDoc.name);
|
|
94
|
-
this.addCronJob(doc.fullDocument);
|
|
95
|
-
}
|
|
96
|
-
else if (typeof oldDoc.time_to_run !== typeof doc.fullDocument.time_to_run || (typeof oldDoc.time_to_run === 'string' && oldDoc.time_to_run !== doc.fullDocument.time_to_run) ||
|
|
97
|
-
(oldDoc.time_to_run instanceof Date && oldDoc.time_to_run.getTime() !== doc.fullDocument.time_to_run.getTime()) ||
|
|
98
|
-
oldDoc.timezone !== doc.fullDocument.timezone) {
|
|
99
|
-
this.updateCronJob(doc.fullDocument);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
this._jobs.push(doc.fullDocument);
|
|
104
|
-
this.addCronJob(doc.fullDocument);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
|
|
109
|
-
job = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
|
|
110
|
-
this.removeCronJob(job.name);
|
|
111
|
-
this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else if (doc.operationType === 'delete') {
|
|
116
|
-
if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
|
|
117
|
-
job = this._jobs.filter(function (a) { return a._id === doc.documentKey['_id']; })[0];
|
|
118
|
-
this.removeCronJob(job.name);
|
|
119
|
-
this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return [2 /*return*/];
|
|
123
|
-
});
|
|
124
|
-
}); })
|
|
125
|
-
.on('error', function (err) {
|
|
126
|
-
_this._watchCrons$.removeAllListeners();
|
|
127
|
-
_this._watchCrons$.close();
|
|
128
|
-
_this._watchCrons$ = null;
|
|
129
|
-
_this.watchCrons();
|
|
130
|
-
})
|
|
131
|
-
.on('end', function () {
|
|
132
|
-
_this._watchCrons$.removeAllListeners();
|
|
133
|
-
_this._watchCrons$.close();
|
|
134
|
-
_this._watchCrons$ = null;
|
|
135
|
-
_this.watchCrons();
|
|
136
|
-
})
|
|
137
|
-
.on('close', function () {
|
|
138
|
-
_this._watchCrons$.removeAllListeners();
|
|
139
|
-
_this._watchCrons$ = null;
|
|
140
|
-
_this.watchCrons();
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
return [2 /*return*/];
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
CronManager.prototype.doesCronJobExist = function (cron) {
|
|
149
|
-
return this._cronManager.exists(cron.name);
|
|
150
|
-
};
|
|
151
|
-
CronManager.prototype.doesCronJobNameExist = function (cron_name) {
|
|
152
|
-
return this._cronManager.exists(cron_name);
|
|
153
|
-
};
|
|
154
|
-
CronManager.prototype.addCronJob = function (cron) {
|
|
155
|
-
var _this = this;
|
|
156
|
-
if (!this.doesCronJobExist(cron)) {
|
|
157
|
-
try {
|
|
158
|
-
this._cronManager.add(cron.name, cron.time_to_run, function () { return _this.runCronJob(cron); }, null, true, cron.timezone, null, false);
|
|
159
|
-
var nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);
|
|
160
|
-
if (cron.running || (nextDate && !cron.next_run || nextDate.getTime() !== cron.next_run.getTime())) {
|
|
161
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { running: false, next_run: nextDate || null } });
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
catch (e) {
|
|
165
|
-
console.log('Cron error', e);
|
|
166
|
-
if (cron.time_to_run instanceof Date) {
|
|
167
|
-
this.runCronJob(cron);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
CronManager.prototype.updateCronJob = function (cron) {
|
|
173
|
-
var _this = this;
|
|
174
|
-
if (this.doesCronJobExist(cron)) {
|
|
175
|
-
this._cronManager.update(cron.name, cron.time_to_run, function () { return _this.runCronJob(cron); }, null, true, cron.timezone);
|
|
176
|
-
var nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);
|
|
177
|
-
if (nextDate && (!cron.next_run || nextDate.getTime() !== cron.next_run.getTime())) {
|
|
178
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { next_run: nextDate } });
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
CronManager.prototype.removeCronJob = function (cron_name) {
|
|
183
|
-
if (this.doesCronJobNameExist(cron_name)) {
|
|
184
|
-
this._cronManager.delete(cron_name);
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
CronManager.prototype.startCronJob = function (cron) {
|
|
188
|
-
if (this.doesCronJobExist(cron)) {
|
|
189
|
-
this._cronManager.start(cron.name);
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
CronManager.prototype.stopCronJob = function (cron) {
|
|
193
|
-
if (this.doesCronJobExist(cron)) {
|
|
194
|
-
this._cronManager.stop(cron.name);
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
CronManager.prototype.stopAllCronJobs = function () {
|
|
198
|
-
this._cronManager.stopAll();
|
|
199
|
-
};
|
|
200
|
-
CronManager.prototype.runCronJob = function (cron) {
|
|
201
|
-
var _this = this;
|
|
202
|
-
var now = new Date();
|
|
203
|
-
cron_job_collection_1.CronJobs.findOneAndUpdate({
|
|
204
|
-
$and: [
|
|
205
|
-
{ _id: cron._id },
|
|
206
|
-
{ running: false },
|
|
207
|
-
{
|
|
208
|
-
$or: [
|
|
209
|
-
{ next_run: { $exists: false } },
|
|
210
|
-
{ next_run: null },
|
|
211
|
-
{ next_run: { $lte: now } }
|
|
212
|
-
]
|
|
213
|
-
}
|
|
214
|
-
]
|
|
215
|
-
}, { $set: { running: true } }).then(function (res) { return __awaiter(_this, void 0, void 0, function () {
|
|
216
|
-
var cronHistory_1, timeoutHandle, err_1, nextDate, nextDate;
|
|
217
|
-
var _this = this;
|
|
218
|
-
return __generator(this, function (_a) {
|
|
219
|
-
switch (_a.label) {
|
|
220
|
-
case 0:
|
|
221
|
-
if (!res) return [3 /*break*/, 14];
|
|
222
|
-
cronHistory_1 = {
|
|
223
|
-
_id: (0, mongo_manager_1.objectIdHexString)(),
|
|
224
|
-
name: res.name,
|
|
225
|
-
reoccuring: res.repeat,
|
|
226
|
-
time_to_run: res.time_to_run,
|
|
227
|
-
method_name: res.method_run,
|
|
228
|
-
id_cronjob: res._id,
|
|
229
|
-
server_restart: true,
|
|
230
|
-
passed: true,
|
|
231
|
-
error: '',
|
|
232
|
-
date_scheduled: res.next_run,
|
|
233
|
-
date_start: new Date(),
|
|
234
|
-
date_end: null,
|
|
235
|
-
date_next: null
|
|
236
|
-
};
|
|
237
|
-
return [4 /*yield*/, cron_job_history_collection_1.CronJobHistories.create(cronHistory_1)];
|
|
238
|
-
case 1:
|
|
239
|
-
_a.sent();
|
|
240
|
-
timeoutHandle = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
241
|
-
var nextDate;
|
|
242
|
-
return __generator(this, function (_a) {
|
|
243
|
-
console.log(new Date(), 'Cron Timeout', JSON.stringify(cron, null, 2));
|
|
244
|
-
if (res.repeat) {
|
|
245
|
-
nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
|
|
246
|
-
cronHistory_1.date_next = nextDate;
|
|
247
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } });
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
cron_job_collection_1.CronJobs.deleteOne({ _id: res._id });
|
|
251
|
-
}
|
|
252
|
-
return [2 /*return*/];
|
|
253
|
-
});
|
|
254
|
-
}); }, 5 * 60 * 1000);
|
|
255
|
-
_a.label = 2;
|
|
256
|
-
case 2:
|
|
257
|
-
_a.trys.push([2, 11, 12, 13]);
|
|
258
|
-
if (!res.method_run_data) return [3 /*break*/, 4];
|
|
259
|
-
return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_run, res.method_run_data)];
|
|
260
|
-
case 3:
|
|
261
|
-
_a.sent();
|
|
262
|
-
return [3 /*break*/, 6];
|
|
263
|
-
case 4: return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_run)];
|
|
264
|
-
case 5:
|
|
265
|
-
_a.sent();
|
|
266
|
-
_a.label = 6;
|
|
267
|
-
case 6:
|
|
268
|
-
if (!res.method_complete) return [3 /*break*/, 10];
|
|
269
|
-
if (!res.method_complete_data) return [3 /*break*/, 8];
|
|
270
|
-
return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_complete, res.method_complete_data)];
|
|
271
|
-
case 7:
|
|
272
|
-
_a.sent();
|
|
273
|
-
return [3 /*break*/, 10];
|
|
274
|
-
case 8: return [4 /*yield*/, this._mainServer.getMethodManager().callMethodCron(res.method_complete)];
|
|
275
|
-
case 9:
|
|
276
|
-
_a.sent();
|
|
277
|
-
_a.label = 10;
|
|
278
|
-
case 10: return [3 /*break*/, 13];
|
|
279
|
-
case 11:
|
|
280
|
-
err_1 = _a.sent();
|
|
281
|
-
console.log(new Date(), 'Cron Error', JSON.stringify(err_1, null, 2));
|
|
282
|
-
cronHistory_1.error = JSON.stringify(err_1, null, 2);
|
|
283
|
-
cronHistory_1.passed = false;
|
|
284
|
-
if (res.repeat) {
|
|
285
|
-
nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
|
|
286
|
-
cronHistory_1.date_next = nextDate;
|
|
287
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } });
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
cron_job_collection_1.CronJobs.deleteOne({ _id: res._id });
|
|
291
|
-
}
|
|
292
|
-
return [3 /*break*/, 13];
|
|
293
|
-
case 12:
|
|
294
|
-
// Clear the timeout if the job completes or fails before the timeout
|
|
295
|
-
clearTimeout(timeoutHandle);
|
|
296
|
-
return [7 /*endfinally*/];
|
|
297
|
-
case 13:
|
|
298
|
-
if (res.repeat) {
|
|
299
|
-
nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
|
|
300
|
-
cronHistory_1.date_next = nextDate;
|
|
301
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } });
|
|
302
|
-
}
|
|
303
|
-
else {
|
|
304
|
-
cron_job_collection_1.CronJobs.deleteOne({ _id: res._id });
|
|
305
|
-
}
|
|
306
|
-
cron_job_history_collection_1.CronJobHistories.updateOne({ _id: cronHistory_1._id }, { $set: {
|
|
307
|
-
server_restart: false,
|
|
308
|
-
passed: cronHistory_1.passed,
|
|
309
|
-
error: cronHistory_1.error,
|
|
310
|
-
date_end: new Date(),
|
|
311
|
-
date_next: cronHistory_1.date_next
|
|
312
|
-
} });
|
|
313
|
-
_a.label = 14;
|
|
314
|
-
case 14: return [2 /*return*/];
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
}); }, function () { });
|
|
318
|
-
};
|
|
319
|
-
return CronManager;
|
|
320
|
-
}());
|
|
321
|
-
exports.CronManager = CronManager;
|
|
322
|
-
|
|
1
|
+
"use strict";var __awaiter=this&&this.__awaiter||function(e,s,a,c){return new(a=a||Promise)(function(t,n){function o(e){try{i(c.next(e))}catch(e){n(e)}}function r(e){try{i(c.throw(e))}catch(e){n(e)}}function i(e){var n;e.done?t(e.value):((n=e.value)instanceof a?n:new a(function(e){e(n)})).then(o,r)}i((c=c.apply(e,s||[])).next())})},__generator=this&&this.__generator||function(o,r){var i,s,a,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},_={next:e(0),throw:e(1),return:e(2)};return"function"==typeof Symbol&&(_[Symbol.iterator]=function(){return this}),_;function e(t){return function(e){var n=[t,e];if(i)throw new TypeError("Generator is already executing.");for(;c=_&&n[_=0]?0:c;)try{if(i=1,s&&(a=2&n[0]?s.return:n[0]?s.throw||((a=s.return)&&a.call(s),0):s.next)&&!(a=a.call(s,n[1])).done)return a;switch(s=0,(n=a?[2&n[0],a.value]:n)[0]){case 0:case 1:a=n;break;case 4:return c.label++,{value:n[1],done:!1};case 5:c.label++,s=n[1],n=[0];continue;case 7:n=c.ops.pop(),c.trys.pop();continue;default:if(!(a=0<(a=c.trys).length&&a[a.length-1])&&(6===n[0]||2===n[0])){c=0;continue}if(3===n[0]&&(!a||n[1]>a[0]&&n[1]<a[3]))c.label=n[1];else if(6===n[0]&&c.label<a[1])c.label=a[1],a=n;else{if(!(a&&c.label<a[2])){a[2]&&c.ops.pop(),c.trys.pop();continue}c.label=a[2],c.ops.push(n)}}n=r.call(o,c)}catch(e){n=[6,e],s=0}finally{i=a=0}if(5&n[0])throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}}},cron_job_collection_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.CronManager=void 0,require("../collections/cron-job.collection")),cron_1=require("../cron/cron"),cron_job_history_collection_1=require("../collections/cron-job-history.collection"),mongo_manager_1=require("../managers/mongo.manager"),CronManager=function(){function e(e){this._jobs=[],this._mainServer=e,this._cronManager=new cron_1.CronJobManager,this.watchCrons()}return e.prototype.watchCrons=function(){return __awaiter(this,void 0,void 0,function(){var n=this;return __generator(this,function(e){switch(e.label){case 0:return this._watchCrons$&&!this._watchCrons$.closed&&(this._watchCrons$.removeAllListeners(),this._watchCrons$.close(),this._watchCrons$=null),[4,new Promise(function(e){return setTimeout(e,1e3)})];case 1:return e.sent(),this._watchCrons$&&!this._watchCrons$.closed||(cron_job_collection_1.CronJobs.find().then(function(e){n._jobs=e,n._jobs.forEach(function(e){n.addCronJob(e),e.running&&cron_job_collection_1.CronJobs.updateOne({_id:e._id},{$set:{running:!1}})})}),this._watchCrons$=cron_job_collection_1.CronJobs.watchCollection([],{fullDocument:"updateLookup"}),this._watchCrons$.on("change",function(o){return __awaiter(n,void 0,void 0,function(){var n,t;return __generator(this,function(e){return"insert"===o.operationType?o.fullDocument&&(this._jobs.some(function(e){return e._id===o.documentKey._id})||this._jobs.push(o.fullDocument),this.addCronJob(o.fullDocument)):"replace"===o.operationType||"update"===o.operationType?o.fullDocument?(n=this._jobs.filter(function(e){return e._id===o.documentKey._id})[0])?(this._jobs.splice(this._jobs.map(function(e){return e._id}).indexOf(o.documentKey._id),1,o.fullDocument),n.name!==o.fullDocument.name?(this.removeCronJob(n.name),this.addCronJob(o.fullDocument)):(typeof n.time_to_run!=typeof o.fullDocument.time_to_run||"string"==typeof n.time_to_run&&n.time_to_run!==o.fullDocument.time_to_run||n.time_to_run instanceof Date&&n.time_to_run.getTime()!==o.fullDocument.time_to_run.getTime()||n.timezone!==o.fullDocument.timezone)&&this.updateCronJob(o.fullDocument)):(this._jobs.push(o.fullDocument),this.addCronJob(o.fullDocument)):this._jobs.some(function(e){return e._id===o.documentKey._id})&&(t=this._jobs.filter(function(e){return e._id===o.documentKey._id})[0],this.removeCronJob(t.name),this._jobs.splice(this._jobs.map(function(e){return e._id}).indexOf(o.documentKey._id),1)):"delete"===o.operationType&&this._jobs.some(function(e){return e._id===o.documentKey._id})&&(t=this._jobs.filter(function(e){return e._id===o.documentKey._id})[0],this.removeCronJob(t.name),this._jobs.splice(this._jobs.map(function(e){return e._id}).indexOf(o.documentKey._id),1)),[2]})})}).on("error",function(e){n._watchCrons$.removeAllListeners(),n._watchCrons$.close(),n._watchCrons$=null,n.watchCrons()}).on("end",function(){n._watchCrons$.removeAllListeners(),n._watchCrons$.close(),n._watchCrons$=null,n.watchCrons()}).on("close",function(){n._watchCrons$.removeAllListeners(),n._watchCrons$=null,n.watchCrons()})),[2]}})})},e.prototype.doesCronJobExist=function(e){return this._cronManager.exists(e.name)},e.prototype.doesCronJobNameExist=function(e){return this._cronManager.exists(e)},e.prototype.addCronJob=function(n){var e=this;if(!this.doesCronJobExist(n))try{this._cronManager.add(n.name,n.time_to_run,function(){return e.runCronJob(n)},null,!0,n.timezone,null,!1);var t=new Date(this._cronManager.getJob(n.name).nextDate().ts);(n.running||t&&!n.next_run||t.getTime()!==n.next_run.getTime())&&cron_job_collection_1.CronJobs.updateOne({_id:n._id},{$set:{running:!1,next_run:t||null}})}catch(e){console.log("Cron error",e),n.time_to_run instanceof Date&&this.runCronJob(n)}},e.prototype.updateCronJob=function(e){var n,t=this;this.doesCronJobExist(e)&&(this._cronManager.update(e.name,e.time_to_run,function(){return t.runCronJob(e)},null,!0,e.timezone),!(n=new Date(this._cronManager.getJob(e.name).nextDate().ts))||e.next_run&&n.getTime()===e.next_run.getTime()||cron_job_collection_1.CronJobs.updateOne({_id:e._id},{$set:{next_run:n}}))},e.prototype.removeCronJob=function(e){this.doesCronJobNameExist(e)&&this._cronManager.delete(e)},e.prototype.startCronJob=function(e){this.doesCronJobExist(e)&&this._cronManager.start(e.name)},e.prototype.stopCronJob=function(e){this.doesCronJobExist(e)&&this._cronManager.stop(e.name)},e.prototype.stopAllCronJobs=function(){this._cronManager.stopAll()},e.prototype.runCronJob=function(a){var e=this,n=new Date;cron_job_collection_1.CronJobs.findOneAndUpdate({$and:[{_id:a._id},{running:!1},{$or:[{next_run:{$exists:!1}},{next_run:null},{next_run:{$lte:n}}]}]},{$set:{running:!0}}).then(function(s){return __awaiter(e,void 0,void 0,function(){var t,n,o,r,i=this;return __generator(this,function(e){switch(e.label){case 0:return s?(t={_id:(0,mongo_manager_1.objectIdHexString)(),name:s.name,reoccuring:s.repeat,time_to_run:s.time_to_run,method_name:s.method_run,id_cronjob:s._id,server_restart:!0,passed:!0,error:"",date_scheduled:s.next_run,date_start:new Date,date_end:null,date_next:null},[4,cron_job_history_collection_1.CronJobHistories.create(t)]):[3,14];case 1:e.sent(),n=setTimeout(function(){return __awaiter(i,void 0,void 0,function(){var n;return __generator(this,function(e){return console.log(new Date,"Cron Timeout",JSON.stringify(a,null,2)),s.repeat?(n=new Date(this._cronManager.getJob(s.name).nextDate().ts),t.date_next=n,cron_job_collection_1.CronJobs.updateOne({_id:s._id},{$set:{running:!1,next_run:n}})):cron_job_collection_1.CronJobs.deleteOne({_id:s._id}),[2]})})},3e5),e.label=2;case 2:return(e.trys.push([2,11,12,13]),s.method_run_data)?[4,this._mainServer.getMethodManager().callMethodCron(s.method_run,s.method_run_data)]:[3,4];case 3:return e.sent(),[3,6];case 4:return[4,this._mainServer.getMethodManager().callMethodCron(s.method_run)];case 5:e.sent(),e.label=6;case 6:return s.method_complete?s.method_complete_data?[4,this._mainServer.getMethodManager().callMethodCron(s.method_complete,s.method_complete_data)]:[3,8]:[3,10];case 7:return e.sent(),[3,10];case 8:return[4,this._mainServer.getMethodManager().callMethodCron(s.method_complete)];case 9:e.sent(),e.label=10;case 10:return[3,13];case 11:return o=e.sent(),console.log(new Date,"Cron Error",JSON.stringify(o,null,2)),t.error=JSON.stringify(o,null,2),t.passed=!1,s.repeat?(r=new Date(this._cronManager.getJob(s.name).nextDate().ts),t.date_next=r,cron_job_collection_1.CronJobs.updateOne({_id:s._id},{$set:{running:!1,next_run:r}})):cron_job_collection_1.CronJobs.deleteOne({_id:s._id}),[3,13];case 12:return clearTimeout(n),[7];case 13:s.repeat?(r=new Date(this._cronManager.getJob(s.name).nextDate().ts),t.date_next=r,cron_job_collection_1.CronJobs.updateOne({_id:s._id},{$set:{running:!1,next_run:r}})):cron_job_collection_1.CronJobs.deleteOne({_id:s._id}),cron_job_history_collection_1.CronJobHistories.updateOne({_id:t._id},{$set:{server_restart:!1,passed:t.passed,error:t.error,date_end:new Date,date_next:t.date_next}}),e.label=14;case 14:return[2]}})})},function(){})},e}();exports.CronManager=CronManager;
|
|
323
2
|
//# sourceMappingURL=cron.manager.js.map
|
|
@@ -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,GA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,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAC,CAAC,CAAC;yBACnC;;;wBAGD,qEAAqE;wBACrE,YAAY,CAAC,aAAa,CAAC,CAAC;;;wBAG7B,IAAI,GAAG,CAAC,MAAM,EAAE;4BACX,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;4BAC7E,aAAW,CAAC,SAAS,GAAG,QAAQ,CAAC;4BACjC,8BAAQ,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAC,EAAE,EAAC,IAAI,EAAE,EAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,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,aAAW,CAAC,GAAG,EAAC,EAAE,EAAC,IAAI,EAAE;gCACzD,cAAc,EAAE,KAAK;gCACrB,MAAM,EAAE,aAAW,CAAC,MAAM;gCAC1B,KAAK,EAAE,aAAW,CAAC,KAAK;gCACxB,QAAQ,EAAE,IAAI,IAAI,EAAE;gCACpB,SAAS,EAAE,aAAW,CAAC,SAAS;6BAChC,EAAC,CAAC,CAAC;;;;;aAEL,EAAE,cAAO,CAAC,CAAC,CAAC;IACd,CAAC;IACF,kBAAC;AAAD,CAxRA,AAwRC,IAAA;AAxRY,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-timezone';\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\tif (job.running) {\n\t\t\t\t\t\tCronJobs.updateOne({_id: job._id}, {$set: {running: false}});\n\t\t\t\t\t}\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 (typeof oldDoc.time_to_run !== typeof doc.fullDocument.time_to_run || (typeof oldDoc.time_to_run === 'string' && oldDoc.time_to_run !== doc.fullDocument.time_to_run) ||\n\t\t\t\t\t\t\t\t(oldDoc.time_to_run instanceof Date && oldDoc.time_to_run.getTime() !== (<Date>doc.fullDocument.time_to_run).getTime()) || \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\ttry {\n\t\t\t\tthis._cronManager.add(\n\t\t\t\t\tcron.name,\n\t\t\t\t\tcron.time_to_run,\n\t\t\t\t\t() => this.runCronJob(cron),\n\t\t\t\t\tnull,\n\t\t\t\t\ttrue,\n\t\t\t\t\tcron.timezone,\n\t\t\t\t\tnull, \n\t\t\t\t\tfalse\n\t\t\t\t);\n\n\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);\n\n\t\t\t\tif (cron.running || (nextDate && !cron.next_run || nextDate.getTime() !== cron.next_run.getTime())) {\n\t\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, next_run: nextDate || null}});\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch(e) {\n\t\t\t\tconsole.log('Cron error', e);\n\n\t\t\t\tif (cron.time_to_run instanceof Date) {\n\t\t\t\t\tthis.runCronJob(cron);\n\t\t\t\t}\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 (nextDate && (!cron.next_run || 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\tlet timeoutHandle = setTimeout(async () => {\n\t\t\t\t\tconsole.log(new Date(), 'Cron Timeout', JSON.stringify(cron, null, 2));\n\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\t\t\t\t}, 5 * 60 * 1000); // 5 minutes in milliseconds\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\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\t\t\t\t}\n\t\t\t\tfinally {\n\t\t\t\t\t// Clear the timeout if the job completes or fails before the timeout\n\t\t\t\t\tclearTimeout(timeoutHandle);\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}"]}
|
|
1
|
+
{"version":3,"sources":["../../src/managers/cron.manager.ts"],"names":["cron_job_collection_1","require","cron_1","cron_job_history_collection_1","mongo_manager_1","CronManager","mainServer","this","_jobs","_mainServer","_cronManager","CronJobManager","watchCrons","prototype","_watchCrons$","closed","removeAllListeners","close","Promise","resolve","setTimeout","_a","sent","CronJobs","find","then","res","_this","forEach","job","addCronJob","running","updateOne","_id","$set","watchCollection","fullDocument","on","doc","__awaiter","operationType","some","a","documentKey","push","oldDoc","filter","splice","map","indexOf","name","removeCronJob","time_to_run","Date","getTime","timezone","updateCronJob","err","doesCronJobExist","cron","exists","doesCronJobNameExist","cron_name","add","runCronJob","nextDate","getJob","next_run","e","console","log","update","delete","startCronJob","start","stopCronJob","stop","stopAllCronJobs","stopAll","now","findOneAndUpdate","$and","$or","$exists","$lte","cronHistory_1","objectIdHexString","reoccuring","repeat","method_name","method_run","id_cronjob","server_restart","passed","error","date_scheduled","date_start","date_end","date_next","CronJobHistories","create","timeoutHandle","JSON","stringify","deleteOne","method_run_data","getMethodManager","callMethodCron","method_complete","method_complete_data","err_1","clearTimeout","exports"],"mappings":"44CACAA,uB,kFAAAC,QAAA,oCAAA,GAEAC,OAAAD,QAAA,cAAA,EAGAE,8BAAAF,QAAA,4CAAA,EACAG,gBAAAH,QAAA,2BAAA,EAGAI,YAAA,WAMC,SAAAA,EAAYC,GAFJC,KAAAC,MAAwB,GAG/BD,KAAKE,YAAcH,EACnBC,KAAKG,aAAe,IAAIR,OAAAS,eAExBJ,KAAKK,WAAU,CAChB,CA6QD,OA3QcP,EAAAQ,UAAAD,WAAb,W,qHAOC,OANIL,KAAKO,cAAgB,CAACP,KAAKO,aAAaC,SAC3CR,KAAKO,aAAaE,mBAAkB,EACpCT,KAAKO,aAAaG,MAAK,EACvBV,KAAKO,aAAe,MAGrB,CAAA,EAAM,IAAII,QAAQ,SAAAC,GAAW,OAAAC,WAAWD,EAAS,GAAI,CAAxB,CAAyB,G,cAAtDE,EAAAC,KAAA,EAEKf,KAAKO,cAAgBP,CAAAA,KAAKO,aAAaC,SAC3Cf,sBAAAuB,SAASC,KAAI,EAAGC,KAAK,SAAAC,GACpBC,EAAKnB,MAAQkB,EAEbC,EAAKnB,MAAMoB,QAAQ,SAAAC,G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AAQ,CACZ,EACD7F,CAAA,EAAC,EAxRY2G,QAAA3G,YAAAA","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-timezone';\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\tif (job.running) {\n\t\t\t\t\t\tCronJobs.updateOne({_id: job._id}, {$set: {running: false}});\n\t\t\t\t\t}\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 (typeof oldDoc.time_to_run !== typeof doc.fullDocument.time_to_run || (typeof oldDoc.time_to_run === 'string' && oldDoc.time_to_run !== doc.fullDocument.time_to_run) ||\n\t\t\t\t\t\t\t\t(oldDoc.time_to_run instanceof Date && oldDoc.time_to_run.getTime() !== (<Date>doc.fullDocument.time_to_run).getTime()) || \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\ttry {\n\t\t\t\tthis._cronManager.add(\n\t\t\t\t\tcron.name,\n\t\t\t\t\tcron.time_to_run,\n\t\t\t\t\t() => this.runCronJob(cron),\n\t\t\t\t\tnull,\n\t\t\t\t\ttrue,\n\t\t\t\t\tcron.timezone,\n\t\t\t\t\tnull, \n\t\t\t\t\tfalse\n\t\t\t\t);\n\n\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);\n\n\t\t\t\tif (cron.running || (nextDate && !cron.next_run || nextDate.getTime() !== cron.next_run.getTime())) {\n\t\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, next_run: nextDate || null}});\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch(e) {\n\t\t\t\tconsole.log('Cron error', e);\n\n\t\t\t\tif (cron.time_to_run instanceof Date) {\n\t\t\t\t\tthis.runCronJob(cron);\n\t\t\t\t}\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 (nextDate && (!cron.next_run || 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\tlet timeoutHandle = setTimeout(async () => {\n\t\t\t\t\tconsole.log(new Date(), 'Cron Timeout', JSON.stringify(cron, null, 2));\n\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\t\t\t\t}, 5 * 60 * 1000); // 5 minutes in milliseconds\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\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\t\t\t\t}\n\t\t\t\tfinally {\n\t\t\t\t\t// Clear the timeout if the job completes or fails before the timeout\n\t\t\t\t\tclearTimeout(timeoutHandle);\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}"]}
|