@resolveio/server-lib 20.7.119 → 20.7.121
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 +51 -1
- package/client-server-app.js.map +1 -1
- package/collections/app-status.collection.js +51 -1
- package/collections/app-status.collection.js.map +1 -1
- package/collections/counter.collection.js +55 -1
- package/collections/counter.collection.js.map +1 -1
- package/collections/cron-job-history.collection.js +136 -1
- package/collections/cron-job-history.collection.js.map +1 -1
- package/collections/cron-job.collection.js +87 -1
- package/collections/cron-job.collection.js.map +1 -1
- package/collections/email-history.collection.js +98 -1
- package/collections/email-history.collection.js.map +1 -1
- package/collections/email-verified.collection.js +60 -1
- package/collections/email-verified.collection.js.map +1 -1
- package/collections/file.collection.js +73 -1
- package/collections/file.collection.js.map +1 -1
- package/collections/flag-update.collection.js +56 -1
- package/collections/flag-update.collection.js.map +1 -1
- package/collections/flag.collection.js +56 -1
- package/collections/flag.collection.js.map +1 -1
- package/collections/log-method-latency.collection.js +72 -1
- package/collections/log-method-latency.collection.js.map +1 -1
- package/collections/log-subscription.collection.js +75 -1
- package/collections/log-subscription.collection.js.map +1 -1
- package/collections/log.collection.js +87 -1
- package/collections/log.collection.js.map +1 -1
- package/collections/logged-in-users.collection.js +66 -1
- package/collections/logged-in-users.collection.js.map +1 -1
- package/collections/monitor-cpu.collection.js +64 -1
- package/collections/monitor-cpu.collection.js.map +1 -1
- package/collections/monitor-function.collection.js +73 -1
- package/collections/monitor-function.collection.js.map +1 -1
- package/collections/monitor-memory.collection.js +76 -1
- package/collections/monitor-memory.collection.js.map +1 -1
- package/collections/monitor-mongo.collection.js +70 -1
- package/collections/monitor-mongo.collection.js.map +1 -1
- package/collections/notification.collection.js +56 -1
- package/collections/notification.collection.js.map +1 -1
- package/collections/report-builder-dashboard-builder.collection.js +108 -1
- package/collections/report-builder-dashboard-builder.collection.js.map +1 -1
- package/collections/report-builder-library.collection.js +86 -1
- package/collections/report-builder-library.collection.js.map +1 -1
- package/collections/report-builder-report.collection.js +148 -1
- package/collections/report-builder-report.collection.js.map +1 -1
- package/collections/user-group.collection.js +88 -1
- package/collections/user-group.collection.js.map +1 -1
- package/collections/user-guide.collection.js +56 -1
- package/collections/user-guide.collection.js.map +1 -1
- package/collections/user.collection.js +265 -1
- package/collections/user.collection.js.map +1 -1
- package/cron/cron.js +97 -1
- package/cron/cron.js.map +1 -1
- package/fixtures/cron-jobs.js +95 -1
- package/fixtures/cron-jobs.js.map +1 -1
- package/fixtures/init.js +78 -1
- package/fixtures/init.js.map +1 -1
- package/http/auth.js +869 -1
- package/http/auth.js.map +1 -1
- package/http/health.js +11 -1
- package/http/health.js.map +1 -1
- package/http/home.js +114 -1
- package/http/home.js.map +1 -1
- package/index.js +18 -1
- package/index.js.map +1 -1
- package/managers/cron.manager.js +461 -1
- package/managers/cron.manager.js.map +1 -1
- package/managers/local-log.manager.js +79 -1
- package/managers/local-log.manager.js.map +1 -1
- package/managers/method.manager.js +1023 -1
- package/managers/method.manager.js.map +1 -1
- package/managers/mongo.manager.js +4223 -1
- package/managers/mongo.manager.js.map +1 -1
- package/managers/monitor.manager.js +534 -1
- package/managers/monitor.manager.js.map +1 -1
- package/managers/subscription.manager.js +1292 -1
- package/managers/subscription.manager.js.map +1 -1
- package/managers/websocket.manager.js +165 -1
- package/managers/websocket.manager.js.map +1 -1
- package/managers/worker-dispatcher.manager.js +335 -1
- package/managers/worker-dispatcher.manager.js.map +1 -1
- package/managers/worker-server.manager.js +292 -1
- package/managers/worker-server.manager.js.map +1 -1
- package/methods/accounts.js +302 -1
- package/methods/accounts.js.map +1 -1
- package/methods/aws.js +748 -1
- package/methods/aws.js.map +1 -1
- package/methods/collections.js +542 -1
- package/methods/collections.js.map +1 -1
- package/methods/counters.js +111 -1
- package/methods/counters.js.map +1 -1
- package/methods/cron-jobs.js +1476 -1
- package/methods/cron-jobs.js.map +1 -1
- package/methods/flag-updates.js +8 -1
- package/methods/flag-updates.js.map +1 -1
- package/methods/flags.js +8 -1
- package/methods/flags.js.map +1 -1
- package/methods/logs.js +417 -1
- package/methods/logs.js.map +1 -1
- package/methods/monitor.js +543 -1
- package/methods/monitor.js.map +1 -1
- package/methods/pdf.js +742 -1
- package/methods/pdf.js.map +1 -1
- package/methods/report-builder.js +840 -1
- package/methods/report-builder.js.map +1 -1
- package/methods/support.js +232 -1
- package/methods/support.js.map +1 -1
- package/models/app-status.model.js +3 -1
- package/models/app-status.model.js.map +1 -1
- package/models/billing-logged-in-users.model.js +3 -1
- package/models/billing-logged-in-users.model.js.map +1 -1
- package/models/collection-document.model.js +3 -1
- package/models/collection-document.model.js.map +1 -1
- package/models/counter.model.js +3 -1
- package/models/counter.model.js.map +1 -1
- package/models/cron-job-history.model.js +3 -1
- package/models/cron-job-history.model.js.map +1 -1
- package/models/cron-job.model.js +3 -1
- package/models/cron-job.model.js.map +1 -1
- package/models/dialog.model.js +3 -1
- package/models/dialog.model.js.map +1 -1
- package/models/email-history.model.js +15 -1
- package/models/email-history.model.js.map +1 -1
- package/models/email-verified.model.js +3 -1
- package/models/email-verified.model.js.map +1 -1
- package/models/file.model.js +3 -1
- package/models/file.model.js.map +1 -1
- package/models/flag-update.model.js +3 -1
- package/models/flag-update.model.js.map +1 -1
- package/models/flag.model.js +3 -1
- package/models/flag.model.js.map +1 -1
- package/models/log-method-latency.model.js +3 -1
- package/models/log-method-latency.model.js.map +1 -1
- package/models/log-subscription.model.js +3 -1
- package/models/log-subscription.model.js.map +1 -1
- package/models/log.model.js +3 -1
- package/models/log.model.js.map +1 -1
- package/models/logged-in-users.model.js +3 -1
- package/models/logged-in-users.model.js.map +1 -1
- package/models/method-response.model.js +3 -1
- package/models/method-response.model.js.map +1 -1
- package/models/method.model.d.ts +0 -1
- package/models/method.model.js +3 -1
- package/models/method.model.js.map +1 -1
- package/models/monitor-cpu.model.js +3 -1
- package/models/monitor-cpu.model.js.map +1 -1
- package/models/monitor-function.model.js +3 -1
- package/models/monitor-function.model.js.map +1 -1
- package/models/monitor-memory.model.js +3 -1
- package/models/monitor-memory.model.js.map +1 -1
- package/models/monitor-mongo.model.js +3 -1
- package/models/monitor-mongo.model.js.map +1 -1
- package/models/notification.model.js +3 -1
- package/models/notification.model.js.map +1 -1
- package/models/pagination.model.js +23 -1
- package/models/pagination.model.js.map +1 -1
- package/models/permission.model.js +3 -1
- package/models/permission.model.js.map +1 -1
- package/models/report-builder-dashboard-builder.model.js +3 -1
- package/models/report-builder-dashboard-builder.model.js.map +1 -1
- package/models/report-builder-library.model.js +3 -1
- package/models/report-builder-library.model.js.map +1 -1
- package/models/report-builder-report.model.js +3 -1
- package/models/report-builder-report.model.js.map +1 -1
- package/models/report-builder.model.js +3 -1
- package/models/report-builder.model.js.map +1 -1
- package/models/select-data-label.model.js +3 -1
- package/models/select-data-label.model.js.map +1 -1
- package/models/server-message.model.js +3 -1
- package/models/server-message.model.js.map +1 -1
- package/models/subscription.model.js +3 -1
- package/models/subscription.model.js.map +1 -1
- package/models/support-ticket.model.js +3 -1
- package/models/support-ticket.model.js.map +1 -1
- package/models/user-group.model.js +3 -1
- package/models/user-group.model.js.map +1 -1
- package/models/user-guide.model.js +3 -1
- package/models/user-guide.model.js.map +1 -1
- package/models/user.model.js +3 -1
- package/models/user.model.js.map +1 -1
- package/package.json +1 -1
- package/public_api.js +77 -1
- package/public_api.js.map +1 -1
- package/publications/app-status.js +16 -1
- package/publications/app-status.js.map +1 -1
- package/publications/cron-jobs.js +32 -1
- package/publications/cron-jobs.js.map +1 -1
- package/publications/files.js +36 -1
- package/publications/files.js.map +1 -1
- package/publications/flags-update.js +22 -1
- package/publications/flags-update.js.map +1 -1
- package/publications/flags.js +22 -1
- package/publications/flags.js.map +1 -1
- package/publications/logs.js +164 -1
- package/publications/logs.js.map +1 -1
- package/publications/notifications.js +16 -1
- package/publications/notifications.js.map +1 -1
- package/publications/report-builder-dashboard-builders.js +42 -1
- package/publications/report-builder-dashboard-builders.js.map +1 -1
- package/publications/report-builder-libraries.js +90 -1
- package/publications/report-builder-libraries.js.map +1 -1
- package/publications/report-builder-reports.js +50 -1
- package/publications/report-builder-reports.js.map +1 -1
- package/publications/super-admin.js +16 -1
- package/publications/super-admin.js.map +1 -1
- package/publications/user-groups.js +16 -1
- package/publications/user-groups.js.map +1 -1
- package/publications/user-guides.js +16 -1
- package/publications/user-guides.js.map +1 -1
- package/resolveio-server-app.js +176 -1
- package/resolveio-server-app.js.map +1 -1
- package/server-app.js +1159 -1
- package/server-app.js.map +1 -1
- package/util/common.js +632 -1
- package/util/common.js.map +1 -1
- package/util/schema-report-builder.js +454 -1
- package/util/schema-report-builder.js.map +1 -1
package/managers/cron.manager.js
CHANGED
|
@@ -1,2 +1,462 @@
|
|
|
1
|
-
"use strict";var __awaiter=this&&this.__awaiter||function(e,r,o,a){return new(o=o||Promise)(function(t,n){function fulfilled(e){try{step(a.next(e))}catch(e){n(e)}}function rejected(e){try{step(a.throw(e))}catch(e){n(e)}}function step(e){var n;e.done?t(e.value):((n=e.value)instanceof o?n:new o(function(e){e(n)})).then(fulfilled,rejected)}step((a=a.apply(e,r||[])).next())})},__generator=this&&this.__generator||function(r,o){var a,i,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},u={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function verb(t){return function(e){var n=[t,e];if(a)throw new TypeError("Generator is already executing.");for(;c=u&&n[u=0]?0:c;)try{if(a=1,i&&(s=2&n[0]?i.return:n[0]?i.throw||((s=i.return)&&s.call(i),0):i.next)&&!(s=s.call(i,n[1])).done)return s;switch(i=0,(n=s?[2&n[0],s.value]:n)[0]){case 0:case 1:s=n;break;case 4:return c.label++,{value:n[1],done:!1};case 5:c.label++,i=n[1],n=[0];continue;case 7:n=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===n[0]||2===n[0])){c=0;continue}if(3===n[0]&&(!s||n[1]>s[0]&&n[1]<s[3]))c.label=n[1];else if(6===n[0]&&c.label<s[1])c.label=s[1],s=n;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(n)}}n=o.call(r,c)}catch(e){n=[6,e],i=0}finally{a=s=0}if(5&n[0])throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}}},__values=this&&this.__values||function(e){var n="function"==typeof Symbol&&Symbol.iterator,t=n&&e[n],r=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&r>=e.length?void 0:e)&&e[r++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")},cron_job_history_collection_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.CronManager=void 0,require("../collections/cron-job-history.collection")),cron_job_collection_1=require("../collections/cron-job.collection"),cron_1=require("../cron/cron"),resolveio_server_app_1=require("../resolveio-server-app"),common_1=require("../util/common"),CronManager=function(){function CronManager(){this._jobs=[]}return CronManager.create=function(){var e=this,n=new CronManager;return setImmediate(function(){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,n.initialize()];case 1:return[2,e.sent()]}})})}),n},CronManager.prototype.initialize=function(){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return this._cronManager=new cron_1.CronJobManager,[4,this.watchCrons()];case 1:return e.sent(),[2]}})})},CronManager.prototype.watchCrons=function(){return __awaiter(this,void 0,void 0,function(){var n,t,r,o,a,i,s=this;return __generator(this,function(e){switch(e.label){case 0:return!this._watchCrons$||this._watchCrons$.closed?[3,2]:(this._watchCrons$.removeAllListeners(),[4,this._watchCrons$.close()]);case 1:e.sent(),this._watchCrons$=null,e.label=2;case 2:return[4,new Promise(function(e){return setTimeout(e,1e3)})];case 3:return e.sent(),this._watchCrons$&&!this._watchCrons$.closed?[3,14]:[4,cron_job_collection_1.CronJobs.find()];case 4:o=e.sent(),this._jobs=o,e.label=5;case 5:e.trys.push([5,11,12,13]),n=__values(this._jobs),t=n.next(),e.label=6;case 6:return t.done?[3,10]:(r=t.value,[4,this.addCronJob(r)]);case 7:return(e.sent(),r.running)?[4,cron_job_collection_1.CronJobs.updateOne({_id:r._id},{$set:{running:!1}})]:[3,9];case 8:e.sent(),e.label=9;case 9:return t=n.next(),[3,6];case 10:return[3,13];case 11:return o=e.sent(),a={error:o},[3,13];case 12:try{t&&!t.done&&(i=n.return)&&i.call(n)}finally{if(a)throw a.error}return[7];case 13:this._watchCrons$=cron_job_collection_1.CronJobs.watchCollection([],{fullDocument:"updateLookup"}),this._watchCrons$.on("change",function(r){return __awaiter(s,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return"insert"!==r.operationType?[3,3]:r.fullDocument?(this._jobs.some(function(e){return e._id===r.documentKey._id})||this._jobs.push(r.fullDocument),[4,this.addCronJob(r.fullDocument)]):[3,2];case 1:e.sent(),e.label=2;case 2:return[3,14];case 3:return"replace"!==r.operationType&&"update"!==r.operationType?[3,13]:r.fullDocument?(n=this._jobs.find(function(e){return e._id===r.documentKey._id}))?(this._jobs.splice(this._jobs.map(function(e){return e._id}).indexOf(r.documentKey._id),1,r.fullDocument),n.name===r.fullDocument.name?[3,5]:(this.removeCronJob(n.name),[4,this.addCronJob(r.fullDocument)])):[3,8]:[3,11];case 4:return e.sent(),[3,7];case 5:return typeof n.time_to_run!=typeof r.fullDocument.time_to_run||"string"==typeof n.time_to_run&&n.time_to_run!==r.fullDocument.time_to_run||n.time_to_run instanceof Date&&n.time_to_run.getTime()!==r.fullDocument.time_to_run.getTime()||n.timezone!==r.fullDocument.timezone?[4,this.updateCronJob(r.fullDocument)]:[3,7];case 6:e.sent(),e.label=7;case 7:return[3,10];case 8:return this._jobs.push(r.fullDocument),[4,this.addCronJob(r.fullDocument)];case 9:e.sent(),e.label=10;case 10:return[3,12];case 11:this._jobs.some(function(e){return e._id===r.documentKey._id})&&(t=this._jobs.find(function(e){return e._id===r.documentKey._id}),this.removeCronJob(t.name),this._jobs.splice(this._jobs.map(function(e){return e._id}).indexOf(r.documentKey._id),1)),e.label=12;case 12:return[3,14];case 13:"delete"===r.operationType&&this._jobs.some(function(e){return e._id===r.documentKey._id})&&(t=this._jobs.find(function(e){return e._id===r.documentKey._id}),this.removeCronJob(t.name),this._jobs.splice(this._jobs.map(function(e){return e._id}).indexOf(r.documentKey._id),1)),e.label=14;case 14:return[2]}})})}).on("error",function(){return __awaiter(s,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,this._watchCrons$.close()];case 1:return e.sent(),[2]}})})}).on("end",function(){return __awaiter(s,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,this._watchCrons$.close()];case 1:return e.sent(),[2]}})})}).on("close",function(){return __awaiter(s,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return this._watchCrons$.removeAllListeners(),this._watchCrons$=null,[4,this.watchCrons()];case 1:return e.sent(),[2]}})})}),e.label=14;case 14:return[2]}})})},CronManager.prototype.doesCronJobExist=function(e){return this._cronManager.exists(e.name)},CronManager.prototype.doesCronJobNameExist=function(e){return this._cronManager.exists(e)},CronManager.prototype.addCronJob=function(r){return __awaiter(this,void 0,void 0,function(){var n,t=this;return __generator(this,function(e){switch(e.label){case 0:if(this.doesCronJobExist(r))return[3,7];e.label=1;case 1:return(e.trys.push([1,4,,7]),this._cronManager.add(r.name,r.time_to_run,function(){return t.runCronJob(r)},null,!0,r.timezone,null,!1),n=new Date(this._cronManager.getJob(r.name).nextDate().ts),r.running||n&&!r.next_run||n.getTime()!==r.next_run.getTime())?[4,cron_job_collection_1.CronJobs.updateOne({_id:r._id},{$set:{running:!1,next_run:n||null}})]:[3,3];case 2:e.sent(),e.label=3;case 3:return[3,7];case 4:return(n=e.sent(),console.log("Cron error",n),r.time_to_run instanceof Date)?[4,this.runCronJob(r)]:[3,6];case 5:e.sent(),e.label=6;case 6:return[3,7];case 7:return[2]}})})},CronManager.prototype.updateCronJob=function(r){return __awaiter(this,void 0,void 0,function(){var n,t=this;return __generator(this,function(e){switch(e.label){case 0:return this.doesCronJobExist(r)?(this._cronManager.update(r.name,r.time_to_run,function(){return t.runCronJob(r)},null,!0,r.timezone),!(n=new Date(this._cronManager.getJob(r.name).nextDate().ts))||r.next_run&&n.getTime()===r.next_run.getTime()?[3,2]:[4,cron_job_collection_1.CronJobs.updateOne({_id:r._id},{$set:{next_run:n}})]):[3,2];case 1:e.sent(),e.label=2;case 2:return[2]}})})},CronManager.prototype.removeCronJob=function(e){this.doesCronJobNameExist(e)&&this._cronManager.delete(e)},CronManager.prototype.startCronJob=function(e){this.doesCronJobExist(e)&&this._cronManager.start(e.name)},CronManager.prototype.stopCronJob=function(e){this.doesCronJobExist(e)&&this._cronManager.stop(e.name)},CronManager.prototype.stopAllCronJobs=function(){this._cronManager.stopAll()},CronManager.prototype.runCronJob=function(s){return __awaiter(this,void 0,void 0,function(){var t,r,n,o,a,i=this;return __generator(this,function(e){switch(e.label){case 0:return o=new Date,[4,cron_job_collection_1.CronJobs.findOneAndUpdate({$and:[{_id:s._id},{running:!1},{$or:[{next_run:{$exists:!1}},{next_run:null},{next_run:{$lte:o}}]}]},{$set:{running:!0}})];case 1:return(t=e.sent())?(r={_id:(0,common_1.objectIdHexString)(),name:t.name,reoccuring:t.repeat,time_to_run:t.time_to_run,method_name:t.method_run,id_cronjob:t._id,server_restart:!0,passed:!0,error:"",date_scheduled:t.next_run,date_start:new Date,date_end:null,date_next:null},[4,cron_job_history_collection_1.CronJobHistories.create(r)]):[3,24];case 2:e.sent(),n=setTimeout(function(){return __awaiter(i,void 0,void 0,function(){var n;return __generator(this,function(e){switch(e.label){case 0:return(console.log(new Date,"Cron Timeout",JSON.stringify(s,null,2)),t.repeat)?(n=new Date(this._cronManager.getJob(t.name).nextDate().ts),r.date_next=n,[4,cron_job_collection_1.CronJobs.updateOne({_id:t._id},{$set:{running:!1,next_run:n}})]):[3,2];case 1:return e.sent(),[3,4];case 2:return[4,cron_job_collection_1.CronJobs.deleteOne({_id:t._id})];case 3:e.sent(),e.label=4;case 4:return[2]}})})},3e5),e.label=3;case 3:return(e.trys.push([3,12,17,18]),t.method_run_data)?[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(t.method_run,t.method_run_data)]:[3,5];case 4:return e.sent(),[3,7];case 5:return[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(t.method_run)];case 6:e.sent(),e.label=7;case 7:return t.method_complete?t.method_complete_data?[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(t.method_complete,t.method_complete_data)]:[3,9]:[3,11];case 8:return e.sent(),[3,11];case 9:return[4,resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(t.method_complete)];case 10:e.sent(),e.label=11;case 11:return[3,18];case 12:return(o=e.sent(),console.log(new Date,"Cron Error",JSON.stringify(o,null,2)),r.error=JSON.stringify(o,null,2),r.passed=!1,t.repeat)?(a=new Date(this._cronManager.getJob(t.name).nextDate().ts),r.date_next=a,[4,cron_job_collection_1.CronJobs.updateOne({_id:t._id},{$set:{running:!1,next_run:a}})]):[3,14];case 13:return e.sent(),[3,16];case 14:return[4,cron_job_collection_1.CronJobs.deleteOne({_id:t._id})];case 15:e.sent(),e.label=16;case 16:return[3,18];case 17:return clearTimeout(n),[7];case 18:return t.repeat?(a=new Date(this._cronManager.getJob(t.name).nextDate().ts),r.date_next=a,[4,cron_job_collection_1.CronJobs.updateOne({_id:t._id},{$set:{running:!1,next_run:a}})]):[3,20];case 19:return e.sent(),[3,22];case 20:return[4,cron_job_collection_1.CronJobs.deleteOne({_id:t._id})];case 21:e.sent(),e.label=22;case 22:return[4,cron_job_history_collection_1.CronJobHistories.updateOne({_id:r._id},{$set:{server_restart:!1,passed:r.passed,error:r.error,date_end:new Date,date_next:r.date_next}})];case 23:e.sent(),e.label=24;case 24:return[2]}})})},CronManager}();exports.CronManager=CronManager;
|
|
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
|
+
var __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.CronManager = void 0;
|
|
51
|
+
var cron_job_history_collection_1 = require("../collections/cron-job-history.collection");
|
|
52
|
+
var cron_job_collection_1 = require("../collections/cron-job.collection");
|
|
53
|
+
var cron_1 = require("../cron/cron");
|
|
54
|
+
var resolveio_server_app_1 = require("../resolveio-server-app");
|
|
55
|
+
var common_1 = require("../util/common");
|
|
56
|
+
var CronManager = /** @class */ (function () {
|
|
57
|
+
function CronManager() {
|
|
58
|
+
this._jobs = [];
|
|
59
|
+
}
|
|
60
|
+
CronManager.create = function () {
|
|
61
|
+
var _this = this;
|
|
62
|
+
var cronManager = new CronManager();
|
|
63
|
+
setImmediate(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0: return [4 /*yield*/, cronManager.initialize()];
|
|
66
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
67
|
+
}
|
|
68
|
+
}); }); });
|
|
69
|
+
return cronManager;
|
|
70
|
+
};
|
|
71
|
+
CronManager.prototype.initialize = function () {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
73
|
+
return __generator(this, function (_a) {
|
|
74
|
+
switch (_a.label) {
|
|
75
|
+
case 0:
|
|
76
|
+
this._cronManager = new cron_1.CronJobManager();
|
|
77
|
+
return [4 /*yield*/, this.watchCrons()];
|
|
78
|
+
case 1:
|
|
79
|
+
_a.sent();
|
|
80
|
+
return [2 /*return*/];
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
CronManager.prototype.watchCrons = function () {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
+
var res, _a, _b, job, e_1_1;
|
|
88
|
+
var e_1, _c;
|
|
89
|
+
var _this = this;
|
|
90
|
+
return __generator(this, function (_d) {
|
|
91
|
+
switch (_d.label) {
|
|
92
|
+
case 0:
|
|
93
|
+
if (!(this._watchCrons$ && !this._watchCrons$.closed)) return [3 /*break*/, 2];
|
|
94
|
+
this._watchCrons$.removeAllListeners();
|
|
95
|
+
return [4 /*yield*/, this._watchCrons$.close()];
|
|
96
|
+
case 1:
|
|
97
|
+
_d.sent();
|
|
98
|
+
this._watchCrons$ = null;
|
|
99
|
+
_d.label = 2;
|
|
100
|
+
case 2:
|
|
101
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
102
|
+
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
|
|
103
|
+
case 3:
|
|
104
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
105
|
+
_d.sent();
|
|
106
|
+
if (!(!this._watchCrons$ || this._watchCrons$.closed)) return [3 /*break*/, 14];
|
|
107
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.find()];
|
|
108
|
+
case 4:
|
|
109
|
+
res = _d.sent();
|
|
110
|
+
this._jobs = res;
|
|
111
|
+
_d.label = 5;
|
|
112
|
+
case 5:
|
|
113
|
+
_d.trys.push([5, 11, 12, 13]);
|
|
114
|
+
_a = __values(this._jobs), _b = _a.next();
|
|
115
|
+
_d.label = 6;
|
|
116
|
+
case 6:
|
|
117
|
+
if (!!_b.done) return [3 /*break*/, 10];
|
|
118
|
+
job = _b.value;
|
|
119
|
+
return [4 /*yield*/, this.addCronJob(job)];
|
|
120
|
+
case 7:
|
|
121
|
+
_d.sent();
|
|
122
|
+
if (!job.running) return [3 /*break*/, 9];
|
|
123
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.updateOne({ _id: job._id }, { $set: { running: false } })];
|
|
124
|
+
case 8:
|
|
125
|
+
_d.sent();
|
|
126
|
+
_d.label = 9;
|
|
127
|
+
case 9:
|
|
128
|
+
_b = _a.next();
|
|
129
|
+
return [3 /*break*/, 6];
|
|
130
|
+
case 10: return [3 /*break*/, 13];
|
|
131
|
+
case 11:
|
|
132
|
+
e_1_1 = _d.sent();
|
|
133
|
+
e_1 = { error: e_1_1 };
|
|
134
|
+
return [3 /*break*/, 13];
|
|
135
|
+
case 12:
|
|
136
|
+
try {
|
|
137
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
138
|
+
}
|
|
139
|
+
finally { if (e_1) throw e_1.error; }
|
|
140
|
+
return [7 /*endfinally*/];
|
|
141
|
+
case 13:
|
|
142
|
+
this._watchCrons$ = cron_job_collection_1.CronJobs.watchCollection([], { fullDocument: 'updateLookup' });
|
|
143
|
+
this._watchCrons$.on('change', function (doc) { return __awaiter(_this, void 0, void 0, function () {
|
|
144
|
+
var oldDoc, job, job;
|
|
145
|
+
return __generator(this, function (_a) {
|
|
146
|
+
switch (_a.label) {
|
|
147
|
+
case 0:
|
|
148
|
+
if (!(doc.operationType === 'insert')) return [3 /*break*/, 3];
|
|
149
|
+
if (!doc.fullDocument) return [3 /*break*/, 2];
|
|
150
|
+
if (!this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
|
|
151
|
+
this._jobs.push(doc.fullDocument);
|
|
152
|
+
}
|
|
153
|
+
return [4 /*yield*/, this.addCronJob(doc.fullDocument)];
|
|
154
|
+
case 1:
|
|
155
|
+
_a.sent();
|
|
156
|
+
_a.label = 2;
|
|
157
|
+
case 2: return [3 /*break*/, 14];
|
|
158
|
+
case 3:
|
|
159
|
+
if (!(doc.operationType === 'replace' || doc.operationType === 'update')) return [3 /*break*/, 13];
|
|
160
|
+
if (!doc.fullDocument) return [3 /*break*/, 11];
|
|
161
|
+
oldDoc = this._jobs.find(function (a) { return a._id === doc.documentKey['_id']; });
|
|
162
|
+
if (!oldDoc) return [3 /*break*/, 8];
|
|
163
|
+
this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1, doc.fullDocument);
|
|
164
|
+
if (!(oldDoc.name !== doc.fullDocument.name)) return [3 /*break*/, 5];
|
|
165
|
+
this.removeCronJob(oldDoc.name);
|
|
166
|
+
return [4 /*yield*/, this.addCronJob(doc.fullDocument)];
|
|
167
|
+
case 4:
|
|
168
|
+
_a.sent();
|
|
169
|
+
return [3 /*break*/, 7];
|
|
170
|
+
case 5:
|
|
171
|
+
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) ||
|
|
172
|
+
(oldDoc.time_to_run instanceof Date && oldDoc.time_to_run.getTime() !== doc.fullDocument.time_to_run.getTime()) ||
|
|
173
|
+
oldDoc.timezone !== doc.fullDocument.timezone)) return [3 /*break*/, 7];
|
|
174
|
+
return [4 /*yield*/, this.updateCronJob(doc.fullDocument)];
|
|
175
|
+
case 6:
|
|
176
|
+
_a.sent();
|
|
177
|
+
_a.label = 7;
|
|
178
|
+
case 7: return [3 /*break*/, 10];
|
|
179
|
+
case 8:
|
|
180
|
+
this._jobs.push(doc.fullDocument);
|
|
181
|
+
return [4 /*yield*/, this.addCronJob(doc.fullDocument)];
|
|
182
|
+
case 9:
|
|
183
|
+
_a.sent();
|
|
184
|
+
_a.label = 10;
|
|
185
|
+
case 10: return [3 /*break*/, 12];
|
|
186
|
+
case 11:
|
|
187
|
+
if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
|
|
188
|
+
job = this._jobs.find(function (a) { return a._id === doc.documentKey['_id']; });
|
|
189
|
+
this.removeCronJob(job.name);
|
|
190
|
+
this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
|
|
191
|
+
}
|
|
192
|
+
_a.label = 12;
|
|
193
|
+
case 12: return [3 /*break*/, 14];
|
|
194
|
+
case 13:
|
|
195
|
+
if (doc.operationType === 'delete') {
|
|
196
|
+
if (this._jobs.some(function (a) { return a._id === doc.documentKey['_id']; })) {
|
|
197
|
+
job = this._jobs.find(function (a) { return a._id === doc.documentKey['_id']; });
|
|
198
|
+
this.removeCronJob(job.name);
|
|
199
|
+
this._jobs.splice(this._jobs.map(function (a) { return a._id; }).indexOf(doc.documentKey['_id']), 1);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
_a.label = 14;
|
|
203
|
+
case 14: return [2 /*return*/];
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}); })
|
|
207
|
+
.on('error', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
208
|
+
return __generator(this, function (_a) {
|
|
209
|
+
switch (_a.label) {
|
|
210
|
+
case 0: return [4 /*yield*/, this._watchCrons$.close()];
|
|
211
|
+
case 1:
|
|
212
|
+
_a.sent();
|
|
213
|
+
return [2 /*return*/];
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
}); })
|
|
217
|
+
.on('end', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
218
|
+
return __generator(this, function (_a) {
|
|
219
|
+
switch (_a.label) {
|
|
220
|
+
case 0: return [4 /*yield*/, this._watchCrons$.close()];
|
|
221
|
+
case 1:
|
|
222
|
+
_a.sent();
|
|
223
|
+
return [2 /*return*/];
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}); })
|
|
227
|
+
.on('close', function () { return __awaiter(_this, void 0, void 0, function () {
|
|
228
|
+
return __generator(this, function (_a) {
|
|
229
|
+
switch (_a.label) {
|
|
230
|
+
case 0:
|
|
231
|
+
this._watchCrons$.removeAllListeners();
|
|
232
|
+
this._watchCrons$ = null;
|
|
233
|
+
return [4 /*yield*/, this.watchCrons()];
|
|
234
|
+
case 1:
|
|
235
|
+
_a.sent();
|
|
236
|
+
return [2 /*return*/];
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}); });
|
|
240
|
+
_d.label = 14;
|
|
241
|
+
case 14: return [2 /*return*/];
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
CronManager.prototype.doesCronJobExist = function (cron) {
|
|
247
|
+
return this._cronManager.exists(cron.name);
|
|
248
|
+
};
|
|
249
|
+
CronManager.prototype.doesCronJobNameExist = function (cron_name) {
|
|
250
|
+
return this._cronManager.exists(cron_name);
|
|
251
|
+
};
|
|
252
|
+
CronManager.prototype.addCronJob = function (cron) {
|
|
253
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
254
|
+
var nextDate, e_2;
|
|
255
|
+
var _this = this;
|
|
256
|
+
return __generator(this, function (_a) {
|
|
257
|
+
switch (_a.label) {
|
|
258
|
+
case 0:
|
|
259
|
+
if (!!this.doesCronJobExist(cron)) return [3 /*break*/, 7];
|
|
260
|
+
_a.label = 1;
|
|
261
|
+
case 1:
|
|
262
|
+
_a.trys.push([1, 4, , 7]);
|
|
263
|
+
this._cronManager.add(cron.name, cron.time_to_run, function () { return _this.runCronJob(cron); }, null, true, cron.timezone, null, false);
|
|
264
|
+
nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);
|
|
265
|
+
if (!(cron.running || (nextDate && !cron.next_run || nextDate.getTime() !== cron.next_run.getTime()))) return [3 /*break*/, 3];
|
|
266
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { running: false, next_run: nextDate || null } })];
|
|
267
|
+
case 2:
|
|
268
|
+
_a.sent();
|
|
269
|
+
_a.label = 3;
|
|
270
|
+
case 3: return [3 /*break*/, 7];
|
|
271
|
+
case 4:
|
|
272
|
+
e_2 = _a.sent();
|
|
273
|
+
console.log('Cron error', e_2);
|
|
274
|
+
if (!(cron.time_to_run instanceof Date)) return [3 /*break*/, 6];
|
|
275
|
+
return [4 /*yield*/, this.runCronJob(cron)];
|
|
276
|
+
case 5:
|
|
277
|
+
_a.sent();
|
|
278
|
+
_a.label = 6;
|
|
279
|
+
case 6: return [3 /*break*/, 7];
|
|
280
|
+
case 7: return [2 /*return*/];
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
CronManager.prototype.updateCronJob = function (cron) {
|
|
286
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
287
|
+
var nextDate;
|
|
288
|
+
var _this = this;
|
|
289
|
+
return __generator(this, function (_a) {
|
|
290
|
+
switch (_a.label) {
|
|
291
|
+
case 0:
|
|
292
|
+
if (!this.doesCronJobExist(cron)) return [3 /*break*/, 2];
|
|
293
|
+
this._cronManager.update(cron.name, cron.time_to_run, function () { return _this.runCronJob(cron); }, null, true, cron.timezone);
|
|
294
|
+
nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);
|
|
295
|
+
if (!(nextDate && (!cron.next_run || nextDate.getTime() !== cron.next_run.getTime()))) return [3 /*break*/, 2];
|
|
296
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { next_run: nextDate } })];
|
|
297
|
+
case 1:
|
|
298
|
+
_a.sent();
|
|
299
|
+
_a.label = 2;
|
|
300
|
+
case 2: return [2 /*return*/];
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
CronManager.prototype.removeCronJob = function (cron_name) {
|
|
306
|
+
if (this.doesCronJobNameExist(cron_name)) {
|
|
307
|
+
this._cronManager.delete(cron_name);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
CronManager.prototype.startCronJob = function (cron) {
|
|
311
|
+
if (this.doesCronJobExist(cron)) {
|
|
312
|
+
this._cronManager.start(cron.name);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
CronManager.prototype.stopCronJob = function (cron) {
|
|
316
|
+
if (this.doesCronJobExist(cron)) {
|
|
317
|
+
this._cronManager.stop(cron.name);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
CronManager.prototype.stopAllCronJobs = function () {
|
|
321
|
+
this._cronManager.stopAll();
|
|
322
|
+
};
|
|
323
|
+
CronManager.prototype.runCronJob = function (cron) {
|
|
324
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
325
|
+
var now, res, cronHistory_1, timeoutHandle, err_1, nextDate, nextDate;
|
|
326
|
+
var _this = this;
|
|
327
|
+
return __generator(this, function (_a) {
|
|
328
|
+
switch (_a.label) {
|
|
329
|
+
case 0:
|
|
330
|
+
now = new Date();
|
|
331
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.findOneAndUpdate({
|
|
332
|
+
$and: [
|
|
333
|
+
{ _id: cron._id },
|
|
334
|
+
{ running: false },
|
|
335
|
+
{
|
|
336
|
+
$or: [
|
|
337
|
+
{ next_run: { $exists: false } },
|
|
338
|
+
{ next_run: null },
|
|
339
|
+
{ next_run: { $lte: now } }
|
|
340
|
+
]
|
|
341
|
+
}
|
|
342
|
+
]
|
|
343
|
+
}, { $set: { running: true } })];
|
|
344
|
+
case 1:
|
|
345
|
+
res = _a.sent();
|
|
346
|
+
if (!res) return [3 /*break*/, 24];
|
|
347
|
+
cronHistory_1 = {
|
|
348
|
+
_id: (0, common_1.objectIdHexString)(),
|
|
349
|
+
name: res.name,
|
|
350
|
+
reoccuring: res.repeat,
|
|
351
|
+
time_to_run: res.time_to_run,
|
|
352
|
+
method_name: res.method_run,
|
|
353
|
+
id_cronjob: res._id,
|
|
354
|
+
server_restart: true,
|
|
355
|
+
passed: true,
|
|
356
|
+
error: '',
|
|
357
|
+
date_scheduled: res.next_run,
|
|
358
|
+
date_start: new Date(),
|
|
359
|
+
date_end: null,
|
|
360
|
+
date_next: null
|
|
361
|
+
};
|
|
362
|
+
return [4 /*yield*/, cron_job_history_collection_1.CronJobHistories.create(cronHistory_1)];
|
|
363
|
+
case 2:
|
|
364
|
+
_a.sent();
|
|
365
|
+
timeoutHandle = setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
366
|
+
var nextDate;
|
|
367
|
+
return __generator(this, function (_a) {
|
|
368
|
+
switch (_a.label) {
|
|
369
|
+
case 0:
|
|
370
|
+
console.log(new Date(), 'Cron Timeout', JSON.stringify(cron, null, 2));
|
|
371
|
+
if (!res.repeat) return [3 /*break*/, 2];
|
|
372
|
+
nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
|
|
373
|
+
cronHistory_1.date_next = nextDate;
|
|
374
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } })];
|
|
375
|
+
case 1:
|
|
376
|
+
_a.sent();
|
|
377
|
+
return [3 /*break*/, 4];
|
|
378
|
+
case 2: return [4 /*yield*/, cron_job_collection_1.CronJobs.deleteOne({ _id: res._id })];
|
|
379
|
+
case 3:
|
|
380
|
+
_a.sent();
|
|
381
|
+
_a.label = 4;
|
|
382
|
+
case 4: return [2 /*return*/];
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
}); }, 5 * 60 * 1000);
|
|
386
|
+
_a.label = 3;
|
|
387
|
+
case 3:
|
|
388
|
+
_a.trys.push([3, 12, 17, 18]);
|
|
389
|
+
if (!res.method_run_data) return [3 /*break*/, 5];
|
|
390
|
+
return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_run, res.method_run_data)];
|
|
391
|
+
case 4:
|
|
392
|
+
_a.sent();
|
|
393
|
+
return [3 /*break*/, 7];
|
|
394
|
+
case 5: return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_run)];
|
|
395
|
+
case 6:
|
|
396
|
+
_a.sent();
|
|
397
|
+
_a.label = 7;
|
|
398
|
+
case 7:
|
|
399
|
+
if (!res.method_complete) return [3 /*break*/, 11];
|
|
400
|
+
if (!res.method_complete_data) return [3 /*break*/, 9];
|
|
401
|
+
return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_complete, res.method_complete_data)];
|
|
402
|
+
case 8:
|
|
403
|
+
_a.sent();
|
|
404
|
+
return [3 /*break*/, 11];
|
|
405
|
+
case 9: return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_complete)];
|
|
406
|
+
case 10:
|
|
407
|
+
_a.sent();
|
|
408
|
+
_a.label = 11;
|
|
409
|
+
case 11: return [3 /*break*/, 18];
|
|
410
|
+
case 12:
|
|
411
|
+
err_1 = _a.sent();
|
|
412
|
+
console.log(new Date(), 'Cron Error', JSON.stringify(err_1, null, 2));
|
|
413
|
+
cronHistory_1.error = JSON.stringify(err_1, null, 2);
|
|
414
|
+
cronHistory_1.passed = false;
|
|
415
|
+
if (!res.repeat) return [3 /*break*/, 14];
|
|
416
|
+
nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
|
|
417
|
+
cronHistory_1.date_next = nextDate;
|
|
418
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } })];
|
|
419
|
+
case 13:
|
|
420
|
+
_a.sent();
|
|
421
|
+
return [3 /*break*/, 16];
|
|
422
|
+
case 14: return [4 /*yield*/, cron_job_collection_1.CronJobs.deleteOne({ _id: res._id })];
|
|
423
|
+
case 15:
|
|
424
|
+
_a.sent();
|
|
425
|
+
_a.label = 16;
|
|
426
|
+
case 16: return [3 /*break*/, 18];
|
|
427
|
+
case 17:
|
|
428
|
+
// Clear the timeout if the job completes or fails before the timeout
|
|
429
|
+
clearTimeout(timeoutHandle);
|
|
430
|
+
return [7 /*endfinally*/];
|
|
431
|
+
case 18:
|
|
432
|
+
if (!res.repeat) return [3 /*break*/, 20];
|
|
433
|
+
nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);
|
|
434
|
+
cronHistory_1.date_next = nextDate;
|
|
435
|
+
return [4 /*yield*/, cron_job_collection_1.CronJobs.updateOne({ _id: res._id }, { $set: { running: false, next_run: nextDate } })];
|
|
436
|
+
case 19:
|
|
437
|
+
_a.sent();
|
|
438
|
+
return [3 /*break*/, 22];
|
|
439
|
+
case 20: return [4 /*yield*/, cron_job_collection_1.CronJobs.deleteOne({ _id: res._id })];
|
|
440
|
+
case 21:
|
|
441
|
+
_a.sent();
|
|
442
|
+
_a.label = 22;
|
|
443
|
+
case 22: return [4 /*yield*/, cron_job_history_collection_1.CronJobHistories.updateOne({ _id: cronHistory_1._id }, { $set: {
|
|
444
|
+
server_restart: false,
|
|
445
|
+
passed: cronHistory_1.passed,
|
|
446
|
+
error: cronHistory_1.error,
|
|
447
|
+
date_end: new Date(),
|
|
448
|
+
date_next: cronHistory_1.date_next
|
|
449
|
+
} })];
|
|
450
|
+
case 23:
|
|
451
|
+
_a.sent();
|
|
452
|
+
_a.label = 24;
|
|
453
|
+
case 24: return [2 /*return*/];
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
};
|
|
458
|
+
return CronManager;
|
|
459
|
+
}());
|
|
460
|
+
exports.CronManager = CronManager;
|
|
461
|
+
|
|
2
462
|
//# sourceMappingURL=cron.manager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/managers/cron.manager.ts"],"names":["cron_job_history_collection_1","require","cron_job_collection_1","cron_1","resolveio_server_app_1","common_1","CronManager","this","_jobs","create","_this","cronManager","setImmediate","__awaiter","__generator","_a","initialize","sent","prototype","_cronManager","CronJobManager","watchCrons","_watchCrons$","closed","removeAllListeners","close","_d","Promise","resolve","setTimeout","CronJobs","find","res","__values","_b","next","job","value","addCronJob","running","updateOne","_id","$set","watchCollection","fullDocument","on","doc","operationType","some","a","documentKey","push","oldDoc","splice","map","indexOf","name","removeCronJob","time_to_run","Date","getTime","timezone","updateCronJob","doesCronJobExist","cron","exists","doesCronJobNameExist","cron_name","add","runCronJob","nextDate","getJob","next_run","console","log","e_2","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","timeoutHandle","JSON","stringify","deleteOne","method_run_data","ResolveIOServer","getMainServer","getMethodManager","callMethodCron","method_complete","method_complete_data","err_1","clearTimeout","exports"],"mappings":"wwDACAA,+B,kFAAAC,QAAA,4CAAA,GACAC,sBAAAD,QAAA,oCAAA,EACAE,OAAAF,QAAA,cAAA,EAGAG,uBAAAH,QAAA,yBAAA,EACAI,SAAAJ,QAAA,gBAAA,EAEAK,YAAA,WAKC,SAAAA,cAFQC,KAAAC,MAAwB,EAEjB,CAkRhB,OAhRQF,YAAAG,OAAP,WAAA,IAAAC,EAAAH,KACOI,EAAc,IAAIL,YAExB,OADAM,aAAa,WAAA,OAAAC,UAAAH,EAAA,KAAA,EAAA,KAAA,EAAA,WAAA,OAAAI,YAAAP,KAAA,SAAAQ,G,uBAAY,MAAA,CAAA,EAAMJ,EAAYK,WAAU,G,OAA5B,MAAA,CAAA,EAAAD,EAAAE,KAAA,E,M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AAS,CAAA,EAAOiC,SAAUF,CAAQ,CAAC,CAAC,IAHlF,CAAA,EAAA,I,eAGHvD,EAAAE,KAAA,E,eAGA,MAAA,CAAA,EAAMf,sBAAA4B,SAAS8E,UAAU,CAACnE,IAAKT,EAAIS,GAAG,CAAC,G,QAAvC1B,EAAAE,KAAA,E,mBAGD,MAAA,CAAA,EAAMjB,8BAAAwG,iBAAiBhE,UAAU,CAACC,IAAKiD,EAAYjD,GAAG,EAAG,CAACC,KAAM,CAC/DuD,eAAgB,CAAA,EAChBC,OAAQR,EAAYQ,OACpBC,MAAOT,EAAYS,MACnBG,SAAU,IAAI3C,KACd4C,UAAWb,EAAYa,S,CACvB,CAAC,G,QANFxF,EAAAE,KAAA,E,mCASHX,WAAA,EAAC,EAvRYgH,QAAAhH,YAAAA","file":"cron.manager.js","sourcesContent":["import { ChangeStream, ChangeStreamDocument } from 'mongodb';\nimport { CronJobHistories } from '../collections/cron-job-history.collection';\nimport { CronJobs } from '../collections/cron-job.collection';\nimport { CronJobManager } from '../cron/cron';\nimport { CronJobHistoryModel } from '../models/cron-job-history.model';\nimport { CronJobModel } from '../models/cron-job.model';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\n\nexport class CronManager {\n\tprivate _cronManager: CronJobManager;\n\tprivate _watchCrons$: ChangeStream;\n\tprivate _jobs: CronJobModel[] = [];\n\n\tconstructor() {}\n\n\tstatic create() {\n\t\tconst cronManager = new CronManager();\n\t\tsetImmediate(async () => await cronManager.initialize());\n\t\treturn cronManager;\n\t}\n\n\tprivate async initialize() {\n\t\tthis._cronManager = new CronJobManager();\n\t\tawait this.watchCrons();\n\t}\n\n\tprivate async watchCrons() {\n\t\tif (this._watchCrons$ && !this._watchCrons$.closed) {\n\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\tawait this._watchCrons$.close();\n\t\t\tthis._watchCrons$ = null;\n\t\t}\n\t\n\t\t// eslint-disable-next-line no-restricted-syntax\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\tlet res = await CronJobs.find();\n\t\t\tthis._jobs = res;\n\n\t\t\tfor (let job of this._jobs) {\n\t\t\t\tawait this.addCronJob(job);\n\t\t\t\tif (job.running) {\n\t\t\t\t\tawait CronJobs.updateOne({_id: job._id}, {$set: {running: false}});\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\tawait this.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.find(a => a._id === <any>doc.documentKey['_id']);\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\tawait this.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\tawait this.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\tawait this.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.find(a => a._id === <any>doc.documentKey['_id']);\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.find(a => a._id === <any>doc.documentKey['_id']);\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', async () => {\n\t\t\t\tawait this._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('end', async () => {\n\t\t\t\tawait this._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('close', async () => {\n\t\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tawait this.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 async 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\tawait CronJobs.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\tawait this.runCronJob(cron);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async 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\tawait CronJobs.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 async runCronJob(cron: CronJobModel) {\n\t\tlet now = new Date();\n\n\t\tlet res = await CronJobs.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}});\n\t\t\t\n\t\tif (res) {\n\t\t\tlet cronHistory: CronJobHistoryModel = {\n\t\t\t\t_id: objectIdHexString(),\n\t\t\t\tname: res.name,\n\t\t\t\treoccuring: res.repeat,\n\t\t\t\ttime_to_run: res.time_to_run,\n\t\t\t\tmethod_name: res.method_run,\n\t\t\t\tid_cronjob: res._id,\n\t\t\t\tserver_restart: true,\n\t\t\t\tpassed: true,\n\t\t\t\terror: '',\n\t\t\t\tdate_scheduled: res.next_run,\n\t\t\t\tdate_start: new Date(),\n\t\t\t\tdate_end: null,\n\t\t\t\tdate_next: null\n\t\t\t};\n\t\t\t\n\t\t\tawait CronJobHistories.create(cronHistory);\n\n\t\t\tlet timeoutHandle = setTimeout(async () => {\n\t\t\t\tconsole.log(new Date(), 'Cron Timeout', JSON.stringify(cron, null, 2));\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\tawait CronJobs.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\tawait CronJobs.deleteOne({_id: res._id});\n\t\t\t\t}\n\t\t\t}, 5 * 60 * 1000); // 5 minutes in milliseconds\n\n\t\t\ttry {\n\t\t\t\tif (res.method_run_data) {\n\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_run, res.method_run_data);\t\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_run);\n\t\t\t\t}\n\n\t\t\t\tif (res.method_complete) {\n\t\t\t\t\tif (res.method_complete_data) {\n\t\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_complete, res.method_complete_data);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_complete);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (err) {\n\t\t\t\tconsole.log(new Date(), 'Cron Error', JSON.stringify(err, null, 2));\n\t\t\t\tcronHistory.error = JSON.stringify(err, null, 2);\n\t\t\t\tcronHistory.passed = false;\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\tawait CronJobs.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\tawait CronJobs.deleteOne({_id: res._id});\n\t\t\t\t}\n\t\t\t}\n\t\t\tfinally {\n\t\t\t\t// Clear the timeout if the job completes or fails before the timeout\n\t\t\t\tclearTimeout(timeoutHandle);\n\t\t\t}\n\n\t\t\tif (res.repeat) {\n\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);\n\t\t\t\tcronHistory.date_next = nextDate;\n\t\t\t\tawait CronJobs.updateOne({_id: res._id}, {$set: {running: false, next_run: nextDate}});\n\t\t\t} \n\t\t\telse {\n\t\t\t\tawait CronJobs.deleteOne({_id: res._id});\n\t\t\t}\n\n\t\t\tawait CronJobHistories.updateOne({_id: cronHistory._id}, {$set: {\n\t\t\t\tserver_restart: false,\n\t\t\t\tpassed: cronHistory.passed,\n\t\t\t\terror: cronHistory.error,\n\t\t\t\tdate_end: new Date(),\n\t\t\t\tdate_next: cronHistory.date_next\n\t\t\t}});\n\t\t}\n\t}\n}"]}
|
|
1
|
+
{"version":3,"sources":["../../src/managers/cron.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,0FAA8E;AAC9E,0EAA8D;AAC9D,qCAA8C;AAG9C,gEAA0D;AAC1D,yCAAmD;AAEnD;IAKC;QAFQ,UAAK,GAAmB,EAAE,CAAC;IAEpB,CAAC;IAET,kBAAM,GAAb;QAAA,iBAIC;QAHA,IAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;QACtC,YAAY,CAAC;;wBAAY,qBAAM,WAAW,CAAC,UAAU,EAAE,EAAA;wBAA9B,sBAAA,SAA8B,EAAA;;iBAAA,CAAC,CAAC;QACzD,OAAO,WAAW,CAAC;IACpB,CAAC;IAEa,gCAAU,GAAxB;;;;;wBACC,IAAI,CAAC,YAAY,GAAG,IAAI,qBAAc,EAAE,CAAC;wBACzC,qBAAM,IAAI,CAAC,UAAU,EAAE,EAAA;;wBAAvB,SAAuB,CAAC;;;;;KACxB;IAEa,gCAAU,GAAxB;;;;;;;;6BACK,CAAA,IAAI,CAAC,YAAY,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,CAAA,EAA9C,wBAA8C;wBACjD,IAAI,CAAC,YAAY,CAAC,kBAAkB,EAAE,CAAC;wBACvC,qBAAM,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,EAAA;;wBAA/B,SAA+B,CAAC;wBAChC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;;;oBAG1B,gDAAgD;oBAChD,q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AG,IAAI,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;wBAC7E,aAAW,CAAC,SAAS,GAAG,QAAQ,CAAC;wBACjC,qBAAM,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,EAAA;;wBAAtF,SAAsF,CAAC;;6BAGvF,qBAAM,8BAAQ,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAC,CAAC,EAAA;;wBAAxC,SAAwC,CAAC;;6BAG1C,qBAAM,8CAAgB,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,aAAW,CAAC,GAAG,EAAC,EAAE,EAAC,IAAI,EAAE;gCAC/D,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,EAAA;;wBANH,SAMG,CAAC;;;;;;KAEL;IACF,kBAAC;AAAD,CAvRA,AAuRC,IAAA;AAvRY,kCAAW","file":"cron.manager.js","sourcesContent":["import { ChangeStream, ChangeStreamDocument } from 'mongodb';\nimport { CronJobHistories } from '../collections/cron-job-history.collection';\nimport { CronJobs } from '../collections/cron-job.collection';\nimport { CronJobManager } from '../cron/cron';\nimport { CronJobHistoryModel } from '../models/cron-job-history.model';\nimport { CronJobModel } from '../models/cron-job.model';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\n\nexport class CronManager {\n\tprivate _cronManager: CronJobManager;\n\tprivate _watchCrons$: ChangeStream;\n\tprivate _jobs: CronJobModel[] = [];\n\n\tconstructor() {}\n\n\tstatic create() {\n\t\tconst cronManager = new CronManager();\n\t\tsetImmediate(async () => await cronManager.initialize());\n\t\treturn cronManager;\n\t}\n\n\tprivate async initialize() {\n\t\tthis._cronManager = new CronJobManager();\n\t\tawait this.watchCrons();\n\t}\n\n\tprivate async watchCrons() {\n\t\tif (this._watchCrons$ && !this._watchCrons$.closed) {\n\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\tawait this._watchCrons$.close();\n\t\t\tthis._watchCrons$ = null;\n\t\t}\n\t\n\t\t// eslint-disable-next-line no-restricted-syntax\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\tlet res = await CronJobs.find();\n\t\t\tthis._jobs = res;\n\n\t\t\tfor (let job of this._jobs) {\n\t\t\t\tawait this.addCronJob(job);\n\t\t\t\tif (job.running) {\n\t\t\t\t\tawait CronJobs.updateOne({_id: job._id}, {$set: {running: false}});\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\tawait this.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.find(a => a._id === <any>doc.documentKey['_id']);\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\tawait this.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\tawait this.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\tawait this.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.find(a => a._id === <any>doc.documentKey['_id']);\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.find(a => a._id === <any>doc.documentKey['_id']);\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', async () => {\n\t\t\t\tawait this._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('end', async () => {\n\t\t\t\tawait this._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('close', async () => {\n\t\t\t\tthis._watchCrons$.removeAllListeners();\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tawait this.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 async 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\tawait CronJobs.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\tawait this.runCronJob(cron);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async 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\tawait CronJobs.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 async runCronJob(cron: CronJobModel) {\n\t\tlet now = new Date();\n\n\t\tlet res = await CronJobs.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}});\n\t\t\t\n\t\tif (res) {\n\t\t\tlet cronHistory: CronJobHistoryModel = {\n\t\t\t\t_id: objectIdHexString(),\n\t\t\t\tname: res.name,\n\t\t\t\treoccuring: res.repeat,\n\t\t\t\ttime_to_run: res.time_to_run,\n\t\t\t\tmethod_name: res.method_run,\n\t\t\t\tid_cronjob: res._id,\n\t\t\t\tserver_restart: true,\n\t\t\t\tpassed: true,\n\t\t\t\terror: '',\n\t\t\t\tdate_scheduled: res.next_run,\n\t\t\t\tdate_start: new Date(),\n\t\t\t\tdate_end: null,\n\t\t\t\tdate_next: null\n\t\t\t};\n\t\t\t\n\t\t\tawait CronJobHistories.create(cronHistory);\n\n\t\t\tlet timeoutHandle = setTimeout(async () => {\n\t\t\t\tconsole.log(new Date(), 'Cron Timeout', JSON.stringify(cron, null, 2));\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\tawait CronJobs.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\tawait CronJobs.deleteOne({_id: res._id});\n\t\t\t\t}\n\t\t\t}, 5 * 60 * 1000); // 5 minutes in milliseconds\n\n\t\t\ttry {\n\t\t\t\tif (res.method_run_data) {\n\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_run, res.method_run_data);\t\n\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_run);\n\t\t\t\t}\n\n\t\t\t\tif (res.method_complete) {\n\t\t\t\t\tif (res.method_complete_data) {\n\t\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_complete, res.method_complete_data);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tawait ResolveIOServer.getMainServer().getMethodManager().callMethodCron(res.method_complete);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (err) {\n\t\t\t\tconsole.log(new Date(), 'Cron Error', JSON.stringify(err, null, 2));\n\t\t\t\tcronHistory.error = JSON.stringify(err, null, 2);\n\t\t\t\tcronHistory.passed = false;\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\tawait CronJobs.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\tawait CronJobs.deleteOne({_id: res._id});\n\t\t\t\t}\n\t\t\t}\n\t\t\tfinally {\n\t\t\t\t// Clear the timeout if the job completes or fails before the timeout\n\t\t\t\tclearTimeout(timeoutHandle);\n\t\t\t}\n\n\t\t\tif (res.repeat) {\n\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);\n\t\t\t\tcronHistory.date_next = nextDate;\n\t\t\t\tawait CronJobs.updateOne({_id: res._id}, {$set: {running: false, next_run: nextDate}});\n\t\t\t} \n\t\t\telse {\n\t\t\t\tawait CronJobs.deleteOne({_id: res._id});\n\t\t\t}\n\n\t\t\tawait CronJobHistories.updateOne({_id: cronHistory._id}, {$set: {\n\t\t\t\tserver_restart: false,\n\t\t\t\tpassed: cronHistory.passed,\n\t\t\t\terror: cronHistory.error,\n\t\t\t\tdate_end: new Date(),\n\t\t\t\tdate_next: cronHistory.date_next\n\t\t\t}});\n\t\t}\n\t}\n}"]}
|