@resolveio/server-lib 12.0.42 → 12.0.43
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/managers/cron.manager.js
CHANGED
|
@@ -39,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
39
39
|
exports.CronManager = void 0;
|
|
40
40
|
var cron_job_collection_1 = require("../collections/cron-job.collection");
|
|
41
41
|
var CronJobManager = require('../cron/cron');
|
|
42
|
+
var moment = require("moment");
|
|
42
43
|
var CronManager = /** @class */ (function () {
|
|
43
44
|
function CronManager(mainServer) {
|
|
44
45
|
this._mainServer = mainServer;
|
|
@@ -144,7 +145,6 @@ var CronManager = /** @class */ (function () {
|
|
|
144
145
|
var _this = this;
|
|
145
146
|
var now = new Date();
|
|
146
147
|
if (!cron.next_run || now.getTime() >= cron.next_run.getTime()) {
|
|
147
|
-
var nextDate = this._cronManager.getJob(cron.name).nextDates().second(0).millisecond(0).toDate();
|
|
148
148
|
cron_job_collection_1.CronJobs.findOneAndUpdate({
|
|
149
149
|
$and: [
|
|
150
150
|
{ _id: cron._id },
|
|
@@ -157,7 +157,8 @@ var CronManager = /** @class */ (function () {
|
|
|
157
157
|
]
|
|
158
158
|
}
|
|
159
159
|
]
|
|
160
|
-
}, { $set: { running: true
|
|
160
|
+
}, { $set: { running: true } }).then(function (res) { return __awaiter(_this, void 0, void 0, function () {
|
|
161
|
+
var nextDate, oneMin;
|
|
161
162
|
return __generator(this, function (_a) {
|
|
162
163
|
switch (_a.label) {
|
|
163
164
|
case 0:
|
|
@@ -184,7 +185,14 @@ var CronManager = /** @class */ (function () {
|
|
|
184
185
|
_a.label = 8;
|
|
185
186
|
case 8:
|
|
186
187
|
if (res.repeat) {
|
|
187
|
-
|
|
188
|
+
nextDate = this._cronManager.getJob(res.name).nextDates().second(0).millisecond(0).toDate();
|
|
189
|
+
oneMin = moment().startOf('minute').add(1, 'minute').toDate();
|
|
190
|
+
cron_job_collection_1.CronJobs.updateOne({
|
|
191
|
+
$and: [
|
|
192
|
+
{ _id: res._id },
|
|
193
|
+
{ running: true }
|
|
194
|
+
]
|
|
195
|
+
}, { $set: { running: false, next_run: nextDate || oneMin } });
|
|
188
196
|
}
|
|
189
197
|
else {
|
|
190
198
|
cron_job_collection_1.CronJobs.deleteOne({ _id: res._id });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/managers/cron.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,0EAA8D;AAE9D,IAAI,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;
|
|
1
|
+
{"version":3,"sources":["../../src/managers/cron.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,0EAA8D;AAE9D,IAAI,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;AAC7C,+BAAiC;AAEjC;IAKC,qBAAY,UAAU;QACrB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAE9B,IAAI,CAAC,YAAY,GAAG,IAAI,cAAc,EAAE,CAAC;QACzC,IAAI,CAAC,YAAY,EAAE,CAAC;IACrB,CAAC;IAEa,kCAAY,GAA1B;;;;;;4BACY,qBAAM,8BAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,EAAA;;wBAA9B,IAAI,GAAG,SAAuB;wBAElC,IAAI,CAAC,OAAO,CAAC,UAAA,GAAG;4BACf,KAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;wBACtB,CAAC,CAAC,CAAC;;;;;KACH;IAEO,sCAAgB,GAAxB,UAAyB,IAAkB;QAC1C,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IAEO,0CAAoB,GAA5B,UAA6B,SAAiB;QAC7C,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,SAAS,CA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GAAmB,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,SAAS,EAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;gCAE7G,MAAM,GAAG,MAAM,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,MAAM,EAAE,CAAC;gCAElE,8BAAQ,CAAC,SAAS,CAAC;oCAClB,IAAI,EAAE;wCACL,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAC;wCACd,EAAC,OAAO,EAAE,IAAI,EAAC;qCACf;iCACD,EAAE,EAAC,IAAI,EAAE,EAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,IAAI,MAAM,EAAC,EAAC,CAAC,CAAC;6BAC3D;iCACI;gCACJ,8BAAQ,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAC,CAAC,CAAC;6BACnC;;;;;iBAEF,EAAE,cAAO,CAAC,CAAC,CAAC;SACb;IACF,CAAC;IACF,kBAAC;AAAD,CArKA,AAqKC,IAAA;AArKY,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';\nlet CronJobManager = require('../cron/cron');\nimport * as moment from 'moment';\n\nexport class CronManager {\n\n\tprivate _cronManager;\n\tprivate _mainServer: ResolveIOMainServer;\n\n\tconstructor(mainServer) {\n\t\tthis._mainServer = mainServer;\n\n\t\tthis._cronManager = new CronJobManager();\n\t\tthis.loadCronJobs();\n\t}\n\n\tprivate async loadCronJobs() {\n\t\tlet jobs = await CronJobs.find({});\n\n\t\tjobs.forEach(job => {\n\t\t\tthis.addCronJob(job);\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 (cron.running) {\n\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false}});\n\t\t}\n\t\t\n\t\tif (!this.doesCronJobExist(cron)) {\n\t\t\tif (cron.timezone) {\n\n\t\t\t}\n\t\t\telse {\n\t\t\t\tlet cronOptions = {\n\t\t\t\t\tstart: true,\n\t\t\t\t\t// onComplete: () => {\n\t\t\t\t\t// \tthis.runCronJobCompleted(cron);\n\t\t\t\t\t// },\n\t\t\t\t\t// timeZone: cron.timezone\n\t\t\t\t};\n\n\t\t\t\tif (cron.timezone) {\n\t\t\t\t\tcronOptions['timeZone'] = cron.timezone;\n\t\t\t\t}\n\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() => {\n\t\t\t\t\t\tthis.runCronJob(cron);\n\t\t\t\t\t},\n\t\t\t\t\tcronOptions\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);\n\t\t}\n\t}\n\n\tprivate removeCronJob(cron_name: string) {\n\t\tif (this.doesCronJobNameExist(cron_name)) {\n\t\t\tthis._cronManager.deleteJob(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.start(cron.name);\n\t\t}\n\t}\n\n\tprivate stopAllCronJobs() {\n\t\tthis._cronManager.stopAll();\n\t}\n\n\tpublic async checkCronJobsForUpdates() {\n\t\tlet jobs = await CronJobs.find({});\n\n\t\t// Remove deleted cron jobs\n\t\tthis._cronManager.getJobs().forEach(cronJob => {\n\t\t\tif (jobs.map(a => a.name).indexOf(cronJob) === -1) {\n\t\t\t\tthis.removeCronJob(cronJob);\n\t\t\t}\n\t\t});\n\n\t\t// Add new cron jobs\n\t\tjobs.forEach(cronJob => {\n\t\t\tthis.addCronJob(cronJob);\n\t\t});\n\n\t\t// Update cronjobs\n\t\tjobs.forEach(cronJob => {\n\t\t\tthis.updateCronJob(cronJob);\n\t\t});\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\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\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\t\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\n\t\t\t\t\tif (res.repeat) {\n\t\t\t\t\t\tlet nextDate = (<moment.Moment>this._cronManager.getJob(res.name).nextDates()).second(0).millisecond(0).toDate();\n\n\t\t\t\t\t\tlet oneMin = moment().startOf('minute').add(1, 'minute').toDate();\n\n\t\t\t\t\t\tCronJobs.updateOne({\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{_id: res._id},\n\t\t\t\t\t\t\t\t{running: true}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}, {$set: {running: false, next_run: nextDate || oneMin}});\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}, () => {});\n\t\t}\n\t}\n}"]}
|