@resolveio/server-lib 12.7.16 → 12.7.18
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
|
@@ -127,20 +127,22 @@ var CronManager = /** @class */ (function () {
|
|
|
127
127
|
};
|
|
128
128
|
CronManager.prototype.addCronJob = function (cron) {
|
|
129
129
|
var _this = this;
|
|
130
|
-
if (cron.running) {
|
|
131
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { running: false, next_run: null } });
|
|
132
|
-
}
|
|
133
|
-
else if (cron.next_run) {
|
|
134
|
-
cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { next_run: null } });
|
|
135
|
-
}
|
|
136
130
|
if (!this.doesCronJobExist(cron)) {
|
|
137
131
|
this._cronManager.add(cron.name, cron.time_to_run, function () { return _this.runCronJob(cron); }, null, true, cron.timezone, null, false);
|
|
132
|
+
var nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);
|
|
133
|
+
if (cron.running || !cron.next_run || nextDate.getTime() !== cron.next_run.getTime()) {
|
|
134
|
+
cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { running: false, next_run: nextDate } });
|
|
135
|
+
}
|
|
138
136
|
}
|
|
139
137
|
};
|
|
140
138
|
CronManager.prototype.updateCronJob = function (cron) {
|
|
141
139
|
var _this = this;
|
|
142
140
|
if (this.doesCronJobExist(cron)) {
|
|
143
141
|
this._cronManager.update(cron.name, cron.time_to_run, function () { return _this.runCronJob(cron); }, null, true, cron.timezone);
|
|
142
|
+
var nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts']);
|
|
143
|
+
if (cron.running || !cron.next_run || nextDate.getTime() !== cron.next_run.getTime()) {
|
|
144
|
+
cron_job_collection_1.CronJobs.updateOne({ _id: cron._id }, { $set: { running: false, next_run: nextDate } });
|
|
145
|
+
}
|
|
144
146
|
}
|
|
145
147
|
};
|
|
146
148
|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GAAG,CAAC,GAAG,EAAC,CAAC,CAAC;6BACnC;4BAED,8CAAgB,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,WAAW,CAAC,GAAG,EAAC,EAAE,EAAC,IAAI,EAAE;oCACzD,cAAc,EAAE,KAAK;oCACrB,MAAM,EAAE,WAAW,CAAC,MAAM;oCAC1B,KAAK,EAAE,WAAW,CAAC,KAAK;oCACxB,QAAQ,EAAE,IAAI,IAAI,EAAE;oCACpB,SAAS,EAAE,WAAW,CAAC,SAAS;iCAChC,EAAC,CAAC,CAAC;;;;;iBAEL,EAAE,cAAO,CAAC,CAAC,CAAC;SACb;IACF,CAAC;IACF,kBAAC;AAAD,CA1NA,AA0NC,IAAA;AA1NY,kCAAW","file":"cron.manager.js","sourcesContent":["import { CronJobModel } from '../models/cron-job.model';\nimport { CronJobs } from '../collections/cron-job.collection';\nimport ResolveIOMainServer from '../server-app';\nimport { CronJobManager } from '../cron/cron';\nimport * as moment from 'moment';\nimport { ChangeStream, ChangeStreamDocument } from 'mongodb';\nimport { CronJobHistories } from '../collections/cron-job-history.collection';\nimport { objectIdHexString } from '../managers/mongo.manager';\nimport { CronJobHistoryModel } from '../models/cron-job-history.model';\n\nexport class CronManager {\n\tprivate _cronManager: CronJobManager;\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _watchCrons$: ChangeStream;\n\tprivate _jobs: CronJobModel[] = [];\n\n\tconstructor(mainServer) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._cronManager = new CronJobManager();\n\n\t\tthis.watchCrons();\n\t}\n\n\tpublic async watchCrons() {\n\t\tif (!this._watchCrons$ || this._watchCrons$.closed) {\n\t\t\tCronJobs.find().then(res => {\n\t\t\t\tthis._jobs = res;\n\n\t\t\t\tthis._jobs.forEach(job => {\n\t\t\t\t\tthis.addCronJob(job);\n\t\t\t\t});\n\t\t\t});\n\n\t\t\tthis._watchCrons$ = CronJobs.watchCollection([], {fullDocument: 'updateLookup'});\n\n\t\t\tthis._watchCrons$.on('change', async (doc: ChangeStreamDocument<CronJobModel>) => {\n\t\t\t\tif (doc.operationType === 'insert') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tif (!this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'replace' || doc.operationType === 'update') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tif (this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tdoc.fullDocument.next_run = null;\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1, doc.fullDocument);\n\t\t\t\t\t\t\tthis.updateCronJob(doc.fullDocument);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'delete') {\n\t\t\t\t\tif (this._jobs.some(a => a._id === (<any>doc).documentKey['_id'])) {\n\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t\t.on('error', (err) => {\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('end', () => {\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('close', () => {\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tthis.watchCrons();\n\t\t\t});\n\t\t}\n\t\telse {\n\t\t\tthis._watchCrons$.close();\n\t\t}\n\t}\n\n\tprivate doesCronJobExist(cron: CronJobModel) {\n\t\treturn this._cronManager.exists(cron.name);\n\t}\n\n\tprivate doesCronJobNameExist(cron_name: string) {\n\t\treturn this._cronManager.exists(cron_name);\n\t}\n\n\tprivate addCronJob(cron: CronJobModel) {\n\t\tif (cron.running) {\n\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, next_run: null}});\n\t\t}\n\t\telse if (cron.next_run) {\n\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {next_run: null}});\n\t\t}\n\t\t\n\t\tif (!this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.add(\n\t\t\t\tcron.name,\n\t\t\t\tcron.time_to_run,\n\t\t\t\t() => this.runCronJob(cron),\n\t\t\t\tnull,\n\t\t\t\ttrue,\n\t\t\t\tcron.timezone,\n\t\t\t\tnull, \n\t\t\t\tfalse\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate updateCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.update(cron.name, cron.time_to_run, () => this.runCronJob(cron), null, true, cron.timezone);\n\t\t}\n\t}\n\n\tprivate removeCronJob(cron_name: string) {\n\t\tif (this.doesCronJobNameExist(cron_name)) {\n\t\t\tthis._cronManager.delete(cron_name);\n\t\t}\n\t}\n\n\tprivate startCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.start(cron.name);\n\t\t}\n\t}\n\n\tprivate stopCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.stop(cron.name);\n\t\t}\n\t}\n\n\tprivate stopAllCronJobs() {\n\t\tthis._cronManager.stopAll();\n\t}\n\n\tprivate runCronJob(cron: CronJobModel) {\n\t\tlet now = new Date();\n\n\t\tif (!cron.next_run || now.getTime() >= cron.next_run.getTime()) {\n\n\t\t\tCronJobs.findOneAndUpdate({\n\t\t\t\t$and: [\n\t\t\t\t\t{_id: cron._id},\n\t\t\t\t\t{running: false},\n\t\t\t\t\t{\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{next_run: {$exists: false}},\n\t\t\t\t\t\t\t{next_run: null},\n\t\t\t\t\t\t\t{next_run: {$lte: now}}\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}, {$set: {running: true}}).then(async res => {\n\t\t\t\tif (res) {\n\t\t\t\t\tlet cronHistory: CronJobHistoryModel = {\n\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\tname: res.name,\n\t\t\t\t\t\treoccuring: res.repeat,\n\t\t\t\t\t\ttime_to_run: res.time_to_run,\n\t\t\t\t\t\tmethod_name: res.method_run,\n\t\t\t\t\t\tid_cronjob: res._id,\n\t\t\t\t\t\tserver_restart: true,\n\t\t\t\t\t\tpassed: true,\n\t\t\t\t\t\terror: '',\n\t\t\t\t\t\tdate_scheduled: cron.next_run,\n\t\t\t\t\t\tdate_start: new Date(),\n\t\t\t\t\t\tdate_end: null,\n\t\t\t\t\t\tdate_next: null\n\t\t\t\t\t};\n\t\t\t\t\t\n\t\t\t\t\tawait CronJobHistories.create(cronHistory);\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tif (res.method_run_data) {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run, res.method_run_data);\t\n\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (res.method_complete) {\n\t\t\t\t\t\t\tif (res.method_complete_data) {\n\t\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete, res.method_complete_data);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tcatch (err) {\n\t\t\t\t\t\tconsole.log(new Date(), 'Cron Error', JSON.stringify(err, null, 2));\n\t\t\t\t\t\tcronHistory.error = JSON.stringify(err, null, 2);\n\t\t\t\t\t\tcronHistory.passed = false;\n\t\t\t\t\t}\n\t\n\t\t\t\t\tif (res.repeat) {\n\t\t\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);\n\t\t\t\t\t\tcronHistory.date_next = nextDate;\n\t\t\t\t\t\tCronJobs.updateOne({_id: res._id}, {$set: {running: false, next_run: nextDate}});\n\t\t\t\t\t} \n\t\t\t\t\telse {\n\t\t\t\t\t\tCronJobs.deleteOne({_id: res._id});\n\t\t\t\t\t}\n\n\t\t\t\t\tCronJobHistories.updateOne({_id: cronHistory._id}, {$set: {\n\t\t\t\t\t\tserver_restart: false,\n\t\t\t\t\t\tpassed: cronHistory.passed,\n\t\t\t\t\t\terror: cronHistory.error,\n\t\t\t\t\t\tdate_end: new Date(),\n\t\t\t\t\t\tdate_next: cronHistory.date_next\n\t\t\t\t\t}});\n\t\t\t\t}\n\t\t\t}, () => {});\n\t\t}\n\t}\n}"]}
|
|
1
|
+
{"version":3,"sources":["../../src/managers/cron.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,0EAA8D;AAE9D,qCAA8C;AAG9C,0FAA8E;AAC9E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C,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;gCAC7E,WAAW,CAAC,SAAS,GAAG,QAAQ,CAAC;gCACjC,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;6BACjF;iCACI;gCACJ,8BAAQ,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,GAAG,CAAC,GAAG,EAAC,CAAC,CAAC;6BACnC;4BAED,8CAAgB,CAAC,SAAS,CAAC,EAAC,GAAG,EAAE,WAAW,CAAC,GAAG,EAAC,EAAE,EAAC,IAAI,EAAE;oCACzD,cAAc,EAAE,KAAK;oCACrB,MAAM,EAAE,WAAW,CAAC,MAAM;oCAC1B,KAAK,EAAE,WAAW,CAAC,KAAK;oCACxB,QAAQ,EAAE,IAAI,IAAI,EAAE;oCACpB,SAAS,EAAE,WAAW,CAAC,SAAS;iCAChC,EAAC,CAAC,CAAC;;;;;iBAEL,EAAE,cAAO,CAAC,CAAC,CAAC;SACb;IACF,CAAC;IACF,kBAAC;AAAD,CA/NA,AA+NC,IAAA;AA/NY,kCAAW","file":"cron.manager.js","sourcesContent":["import { CronJobModel } from '../models/cron-job.model';\nimport { CronJobs } from '../collections/cron-job.collection';\nimport ResolveIOMainServer from '../server-app';\nimport { CronJobManager } from '../cron/cron';\nimport * as moment from 'moment';\nimport { ChangeStream, ChangeStreamDocument } from 'mongodb';\nimport { CronJobHistories } from '../collections/cron-job-history.collection';\nimport { objectIdHexString } from '../managers/mongo.manager';\nimport { CronJobHistoryModel } from '../models/cron-job-history.model';\n\nexport class CronManager {\n\tprivate _cronManager: CronJobManager;\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _watchCrons$: ChangeStream;\n\tprivate _jobs: CronJobModel[] = [];\n\n\tconstructor(mainServer) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._cronManager = new CronJobManager();\n\n\t\tthis.watchCrons();\n\t}\n\n\tpublic async watchCrons() {\n\t\tif (!this._watchCrons$ || this._watchCrons$.closed) {\n\t\t\tCronJobs.find().then(res => {\n\t\t\t\tthis._jobs = res;\n\n\t\t\t\tthis._jobs.forEach(job => {\n\t\t\t\t\tthis.addCronJob(job);\n\t\t\t\t});\n\t\t\t});\n\n\t\t\tthis._watchCrons$ = CronJobs.watchCollection([], {fullDocument: 'updateLookup'});\n\n\t\t\tthis._watchCrons$.on('change', async (doc: ChangeStreamDocument<CronJobModel>) => {\n\t\t\t\tif (doc.operationType === 'insert') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tif (!this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'replace' || doc.operationType === 'update') {\n\t\t\t\t\tif (doc.fullDocument) {\n\t\t\t\t\t\tif (this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tdoc.fullDocument.next_run = null;\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1, doc.fullDocument);\n\t\t\t\t\t\t\tthis.updateCronJob(doc.fullDocument);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tthis._jobs.push(doc.fullDocument);\n\t\t\t\t\t\t\tthis.addCronJob(doc.fullDocument);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (this._jobs.some(a => a._id === <any>doc.documentKey['_id'])) {\n\t\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (doc.operationType === 'delete') {\n\t\t\t\t\tif (this._jobs.some(a => a._id === (<any>doc).documentKey['_id'])) {\n\t\t\t\t\t\tlet job = this._jobs.filter(a => a._id === <any>doc.documentKey['_id'])[0];\n\t\t\t\t\t\tthis.removeCronJob(job.name);\n\t\t\t\t\t\tthis._jobs.splice(this._jobs.map(a => a._id).indexOf(<any>doc.documentKey['_id']), 1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t\t.on('error', (err) => {\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('end', () => {\n\t\t\t\tthis._watchCrons$.close();\n\t\t\t})\n\t\t\t.on('close', () => {\n\t\t\t\tthis._watchCrons$ = null;\n\t\t\t\tthis.watchCrons();\n\t\t\t});\n\t\t}\n\t\telse {\n\t\t\tthis._watchCrons$.close();\n\t\t}\n\t}\n\n\tprivate doesCronJobExist(cron: CronJobModel) {\n\t\treturn this._cronManager.exists(cron.name);\n\t}\n\n\tprivate doesCronJobNameExist(cron_name: string) {\n\t\treturn this._cronManager.exists(cron_name);\n\t}\n\n\tprivate addCronJob(cron: CronJobModel) {\n\t\tif (!this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.add(\n\t\t\t\tcron.name,\n\t\t\t\tcron.time_to_run,\n\t\t\t\t() => this.runCronJob(cron),\n\t\t\t\tnull,\n\t\t\t\ttrue,\n\t\t\t\tcron.timezone,\n\t\t\t\tnull, \n\t\t\t\tfalse\n\t\t\t);\n\n\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts'])\n\n\t\t\tif (cron.running || !cron.next_run || nextDate.getTime() !== cron.next_run.getTime()) {\n\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, next_run: nextDate}});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate updateCronJob(cron: CronJobModel) {\n\t\tif (this.doesCronJobExist(cron)) {\n\t\t\tthis._cronManager.update(cron.name, cron.time_to_run, () => this.runCronJob(cron), null, true, cron.timezone);\n\t\t\t\n\t\t\tlet nextDate = new Date(this._cronManager.getJob(cron.name).nextDate()['ts'])\n\n\t\t\tif (cron.running || !cron.next_run || nextDate.getTime() !== cron.next_run.getTime()) {\n\t\t\t\tCronJobs.updateOne({_id: cron._id}, {$set: {running: false, 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\tif (!cron.next_run || now.getTime() >= cron.next_run.getTime()) {\n\n\t\t\tCronJobs.findOneAndUpdate({\n\t\t\t\t$and: [\n\t\t\t\t\t{_id: cron._id},\n\t\t\t\t\t{running: false},\n\t\t\t\t\t{\n\t\t\t\t\t\t$or: [\n\t\t\t\t\t\t\t{next_run: {$exists: false}},\n\t\t\t\t\t\t\t{next_run: null},\n\t\t\t\t\t\t\t{next_run: {$lte: now}}\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}, {$set: {running: true}}).then(async res => {\n\t\t\t\tif (res) {\n\t\t\t\t\tlet cronHistory: CronJobHistoryModel = {\n\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\tname: res.name,\n\t\t\t\t\t\treoccuring: res.repeat,\n\t\t\t\t\t\ttime_to_run: res.time_to_run,\n\t\t\t\t\t\tmethod_name: res.method_run,\n\t\t\t\t\t\tid_cronjob: res._id,\n\t\t\t\t\t\tserver_restart: true,\n\t\t\t\t\t\tpassed: true,\n\t\t\t\t\t\terror: '',\n\t\t\t\t\t\tdate_scheduled: cron.next_run,\n\t\t\t\t\t\tdate_start: new Date(),\n\t\t\t\t\t\tdate_end: null,\n\t\t\t\t\t\tdate_next: null\n\t\t\t\t\t};\n\t\t\t\t\t\n\t\t\t\t\tawait CronJobHistories.create(cronHistory);\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tif (res.method_run_data) {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run, res.method_run_data);\t\n\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_run);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (res.method_complete) {\n\t\t\t\t\t\t\tif (res.method_complete_data) {\n\t\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete, res.method_complete_data);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse {\n\t\t\t\t\t\t\t\tawait this._mainServer.getMethodManager().callMethodCron(res.method_complete);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tcatch (err) {\n\t\t\t\t\t\tconsole.log(new Date(), 'Cron Error', JSON.stringify(err, null, 2));\n\t\t\t\t\t\tcronHistory.error = JSON.stringify(err, null, 2);\n\t\t\t\t\t\tcronHistory.passed = false;\n\t\t\t\t\t}\n\t\n\t\t\t\t\tif (res.repeat) {\n\t\t\t\t\t\tlet nextDate = new Date(this._cronManager.getJob(res.name).nextDate()['ts']);\n\t\t\t\t\t\tcronHistory.date_next = nextDate;\n\t\t\t\t\t\tCronJobs.updateOne({_id: res._id}, {$set: {running: false, next_run: nextDate}});\n\t\t\t\t\t} \n\t\t\t\t\telse {\n\t\t\t\t\t\tCronJobs.deleteOne({_id: res._id});\n\t\t\t\t\t}\n\n\t\t\t\t\tCronJobHistories.updateOne({_id: cronHistory._id}, {$set: {\n\t\t\t\t\t\tserver_restart: false,\n\t\t\t\t\t\tpassed: cronHistory.passed,\n\t\t\t\t\t\terror: cronHistory.error,\n\t\t\t\t\t\tdate_end: new Date(),\n\t\t\t\t\t\tdate_next: cronHistory.date_next\n\t\t\t\t\t}});\n\t\t\t\t}\n\t\t\t}, () => {});\n\t\t}\n\t}\n}"]}
|
|
@@ -232,7 +232,7 @@ var MonitorManager = /** @class */ (function () {
|
|
|
232
232
|
switch (_a.label) {
|
|
233
233
|
case 0:
|
|
234
234
|
if (!this._monitorData.length) return [3 /*break*/, 4];
|
|
235
|
-
monitorData = this._monitorData.splice(0, Math.min(
|
|
235
|
+
monitorData = this._monitorData.splice(0, Math.min(2500, this._monitorData.length));
|
|
236
236
|
_a.label = 1;
|
|
237
237
|
case 1:
|
|
238
238
|
_a.trys.push([1, 3, , 4]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,+BAA0B;AAC1B,gFAAoE;AACpE,sFAA0E;AAC1E,oFAAwE;AACxE,kCAA2C;AAE3C,iDAAoD;AACpD,IAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEzB;IAMC,wBAAY,UAAU,EAAE,YAAY;QAH5B,sBAAiB,GAAG,EAAE,CAAC;QACvB,iBAAY,GAAG,EAAE,CAAC;QAGzB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,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sD,GAAG,IAAI,CAAC,QAAQ,GAAG,OAAO,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SAC/e;IACF,CAAC;IACF,sBAAC;AAAD,CA7CA,AA6CC,IAAA;AA7CY,0CAAe","file":"monitor.manager.js","sourcesContent":["import ResolveIOMainServer from '../server-app';\nimport axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../index';\nimport { deepCopy } from '../util/common';\nimport { objectIdHexString } from './mongo.manager';\nconst os = require('os');\n\nexport class MonitorManager {\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _serverConfig;\n\tprivate _instanceHostname = '';\n\tprivate _monitorData = [];\n\n\tconstructor(mainServer, serverConfig) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._serverConfig = serverConfig;\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\t//Create function to get CPU information\n\tcpuAverage() {\n\t\t//Initialise sum of idle and time of cores and fetch CPU info\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\t\n\t\t//Loop through CPU cores\n\t\tfor(var i = 0, len = cpus.length; i < len; i++) {\n\t\t\t//Select CPU core\n\t\t\tvar cpu = cpus[i];\n\t\t\n\t\t\t//Total up the time in the cores tick\n\t\t\tfor(let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\n\t\t\t//Total up the idle time of the core\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\t\n\t\t//Return the average Idle and Tick times\n\t\treturn {idle: totalIdle / cpus.length, total: totalTick / cpus.length};\n\t}\n\n\tasync setupIntervals() {\n\t\tif (this._serverConfig['ROOT_URL'] !== 'https://resolveio.com' && this._serverConfig['ROOT_URL'] !== 'http://localhost:4200') {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number' ? (<any>process.env.APP_VERSION).toString() : process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\t\t\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\n\t\t\t//Grab second Measure\n\t\t\tvar endMeasure = this.cpuAverage(); \n\n\t\t\t//Calculate the difference in idle and total time between the measures\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\n\t\t\t//Calculate the average percentage CPU usage\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\t\t\t\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0],\n\t\t\t\t\ttype: 'cpu'\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\ttype: 'memory'\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\taddMongoTracker(data: MongoMonitorModel) {\n\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tMonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tif (this._serverConfig['ROOT_URL'] !== 'https://airdropxrp.io' && this._serverConfig['ROOT_URL'] !== 'https://xrptradereport.com') {\n\t\t\t\t\tthis._monitorData.push({\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big',\n\t\t\t\t\t\ttype: 'mongo'\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\t\n\t}\n\n\tsetupEventLoop() {\n\t\tsetInterval(async () => {\n\t\t\tif (this._monitorData.length) {\n\t\t\t\tlet monitorData = this._monitorData.splice(0, Math.min(10, this._monitorData.length));\n\n\t\t\t\ttry {\n\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/monitor', monitorData);\n\t\t\t\t}\n\t\t\t\tcatch (e) {}\n\t\t\t}\n\t\t}, 1000);\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date, \n\tmethod: string,\n\tcollection: string,\n\tduration: number,\n\tquery: string\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: {\n\t\t[id_socket: string]: MonitorFunction;\n\t} = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tlet newMonitorFunction = new MonitorFunction(this._functionCnt++, functionType, functionName, user, id_socket, data);\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tfinishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.filter(a => a.id === id)[0];\n\t\tif (monitor) {\n\t\t\tmonitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(this._functions.map(a => a.id).indexOf(id), 1);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\t\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType = 'Cron Method' | 'Internal Method' | 'Method' | 'User Specific Publication' | 'Publication';\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tprivate _maxDiff = 0;\n\tprivate _functionType: MonitorFunctionType;\n\tprivate _functionName: string;\n\tprivate _user: string;\n\tprivate _data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor(id: number, functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis._functionType = functionType;\n\t\tthis._functionName = functionName;\n\t\tthis._user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis._data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\t\t\tif (diff > this._maxDiff) {\n\t\t\t\tthis._maxDiff = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this._maxDiff >= 5000) {\n\t\t\tResolveIOServer.getMainServer().getMethodManager().sendEmail('dev@resolveio.com', ResolveIOServer.getMainServer().getServerConfig()['CLIENT_NAME'] + ' - Slow ' + this._functionType + ' Detected', '', (this._user ? 'User: ' + this._user + '\\n' : '') + this._functionType + ': ' + this._functionName + '\\n' + 'During this function the event loop did not run for ' + this._maxDiff + ' ms (' + this._maxDiff / 1000 + ' sec)' + (this._data ? '\\n\\nData: ' + JSON.stringify(this._data, null, 2) : ''));\n\t\t}\n\t}\n}"]}
|
|
1
|
+
{"version":3,"sources":["../../src/managers/monitor.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,+BAA0B;AAC1B,gFAAoE;AACpE,sFAA0E;AAC1E,oFAAwE;AACxE,kCAA2C;AAE3C,iDAAoD;AACpD,IAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEzB;IAMC,wBAAY,UAAU,EAAE,YAAY;QAH5B,sBAAiB,GAAG,EAAE,CAAC;QACvB,iBAAY,GAAG,EAAE,CAAC;QAGzB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAE3D,IAAI,CAAC,cAAc,EAAE,CAAC;QACtB,IAAI,CAAC,cAAc,EAAE,CAAC;IACvB,CAAC;IAED,wCAAwC;IACxC,mCAAU,GAAV;QACC,6DAA6D;QAC7D,IAAI,SAAS,GAAG,CAAC,EAAE,SAAS,GAAG,CAAC,CAAC;QACjC,IAAI,IAAI,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC;QAErB,wBAAwB;QACxB,KAAI,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;YAC/C,iBAAiB;YACjB,IAAI,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;YAElB,qCAAqC;YACrC,KAAI,IAAI,IAAI,IAAI,GAAG,CAAC,KAAK,EAAE;gBAC1B,SAAS,IAAI,GAA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AAb;QACC,IAAI,IAAI,CAAC,MAAM,EAAE;YAChB,aAAa,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC3B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;SACnB;QAED,IAAI,CAAC,OAAO,GAAG,IAAI,IAAI,EAAE,CAAC;QAE1B,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,EAAE;YAC1B,uBAAe,CAAC,aAAa,EAAE,CAAC,gBAAgB,EAAE,CAAC,SAAS,CAAC,mBAAmB,EAAE,uBAAe,CAAC,aAAa,EAAE,CAAC,eAAe,EAAE,CAAC,aAAa,CAAC,GAAG,UAAU,GAAG,IAAI,CAAC,aAAa,GAAG,WAAW,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,aAAa,GAAG,IAAI,GAAG,IAAI,CAAC,aAAa,GAAG,IAAI,GAAG,sDAAsD,GAAG,IAAI,CAAC,QAAQ,GAAG,OAAO,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SAC/e;IACF,CAAC;IACF,sBAAC;AAAD,CA7CA,AA6CC,IAAA;AA7CY,0CAAe","file":"monitor.manager.js","sourcesContent":["import ResolveIOMainServer from '../server-app';\nimport axios from 'axios';\nimport { MonitorCPUs } from '../collections/monitor-cpu.collection';\nimport { MonitorMemorys } from '../collections/monitor-memory.collection';\nimport { MonitorMongos } from '../collections/monitor-mongo.collection';\nimport { ResolveIOServer } from '../index';\nimport { deepCopy } from '../util/common';\nimport { objectIdHexString } from './mongo.manager';\nconst os = require('os');\n\nexport class MonitorManager {\n\tprivate _mainServer: ResolveIOMainServer;\n\tprivate _serverConfig;\n\tprivate _instanceHostname = '';\n\tprivate _monitorData = [];\n\n\tconstructor(mainServer, serverConfig) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._serverConfig = serverConfig;\n\t\tthis._instanceHostname = os.hostname().replace(/\\./g, '-');\n\n\t\tthis.setupIntervals();\n\t\tthis.setupEventLoop();\n\t}\n\n\t//Create function to get CPU information\n\tcpuAverage() {\n\t\t//Initialise sum of idle and time of cores and fetch CPU info\n\t\tvar totalIdle = 0, totalTick = 0;\n\t\tvar cpus = os.cpus();\n\t\n\t\t//Loop through CPU cores\n\t\tfor(var i = 0, len = cpus.length; i < len; i++) {\n\t\t\t//Select CPU core\n\t\t\tvar cpu = cpus[i];\n\t\t\n\t\t\t//Total up the time in the cores tick\n\t\t\tfor(let type in cpu.times) {\n\t\t\t\ttotalTick += cpu.times[type];\n\t\t\t}\n\t\t\n\t\t\t//Total up the idle time of the core\n\t\t\ttotalIdle += cpu.times.idle;\n\t\t}\n\t\n\t\t//Return the average Idle and Tick times\n\t\treturn {idle: totalIdle / cpus.length, total: totalTick / cpus.length};\n\t}\n\n\tasync setupIntervals() {\n\t\tif (this._serverConfig['ROOT_URL'] !== 'https://resolveio.com' && this._serverConfig['ROOT_URL'] !== 'http://localhost:4200') {\n\t\t\ttry {\n\t\t\t\tlet instanceId = await axios.get('http://169.254.169.254/latest/meta-data/instance-id');\n\n\t\t\t\tsetInterval(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/health', {\n\t\t\t\t\t\t\ttype: 'application',\n\t\t\t\t\t\t\tid_aws_instance: instanceId.data,\n\t\t\t\t\t\t\tversion: typeof process.env.APP_VERSION === 'number' ? (<any>process.env.APP_VERSION).toString() : process.env.APP_VERSION,\n\t\t\t\t\t\t\tversion_key: process.env.APP_VERSION_KEY\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcatch(e) {}\n\t\t\t\t}, 10000);\n\t\t\t}\n\t\t\tcatch(errHealth) {}\n\t\t}\n\n\t\tlet lastCPU = this.cpuAverage();\n\t\t\n\t\tsetInterval(() => {\n\t\t\tlet now = new Date();\n\n\t\t\t//Grab second Measure\n\t\t\tvar endMeasure = this.cpuAverage(); \n\n\t\t\t//Calculate the difference in idle and total time between the measures\n\t\t\tvar idleDifference = endMeasure.idle - lastCPU.idle;\n\t\t\tvar totalDifference = endMeasure.total - lastCPU.total;\n\n\t\t\t//Calculate the average percentage CPU usage\n\t\t\tvar percentageCPU = 1 - idleDifference / totalDifference;\n\t\t\t\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorCPUs.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0]\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tapp: percentageCPU,\n\t\t\t\t\tsystem: os.loadavg()[0],\n\t\t\t\t\ttype: 'cpu'\n\t\t\t\t})\n\t\t\t}\n\n\t\t\tlastCPU = endMeasure;\n\n\t\t\tconst memUsage = process.memoryUsage();\n\n\t\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\t\tMonitorMemorys.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem()\n\t\t\t\t});\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthis._monitorData.push({\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: now,\n\t\t\t\t\tphysical_total: memUsage.heapTotal,\n\t\t\t\t\tphysical_used: memUsage.heapUsed,\n\t\t\t\t\tphysical_free: os.freemem(),\n\t\t\t\t\tvirtual: memUsage.rss,\n\t\t\t\t\tprivate: memUsage.external,\n\t\t\t\t\tphysical: os.totalmem(),\n\t\t\t\t\ttype: 'memory'\n\t\t\t\t});\n\t\t\t}\n\t\t}, 3000);\n\t}\n\n\taddMongoTracker(data: MongoMonitorModel) {\n\t\tif (this._serverConfig['ROOT_URL'] === 'https://resolveio.com' || this._serverConfig['ROOT_URL'] === 'http://localhost:4200') {\n\t\t\tif (!data.collection.startsWith('monitor-')) {\n\t\t\t\tMonitorMongos.create({\n\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t},\n\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\tmethod: data.method,\n\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\tduration: data.duration,\n\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big'\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tif (this._instanceHostname) {\n\t\t\t\tif (this._serverConfig['ROOT_URL'] !== 'https://airdropxrp.io' && this._serverConfig['ROOT_URL'] !== 'https://xrptradereport.com') {\n\t\t\t\t\tthis._monitorData.push({\n\t\t\t\t\t\tmetadata: {\n\t\t\t\t\t\t\tinstance: this._instanceHostname,\n\t\t\t\t\t\t\tclient: this._serverConfig['CLIENT_NAME']\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdate: new Date(data.date),\n\t\t\t\t\t\tmethod: data.method,\n\t\t\t\t\t\tdoc_collection: data.collection,\n\t\t\t\t\t\tduration: data.duration,\n\t\t\t\t\t\tquery: Buffer.byteLength(data.query, 'utf8') < 15800000 ? data.query : 'Too Big',\n\t\t\t\t\t\ttype: 'mongo'\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\t\n\t}\n\n\tsetupEventLoop() {\n\t\tsetInterval(async () => {\n\t\t\tif (this._monitorData.length) {\n\t\t\t\tlet monitorData = this._monitorData.splice(0, Math.min(2500, this._monitorData.length));\n\n\t\t\t\ttry {\n\t\t\t\t\tawait axios.post('https://backend.resolveio.com/api/monitor', monitorData);\n\t\t\t\t}\n\t\t\t\tcatch (e) {}\n\t\t\t}\n\t\t}, 1000);\n\t}\n}\n\nexport interface MongoMonitorModel {\n\tdate: Date, \n\tmethod: string,\n\tcollection: string,\n\tduration: number,\n\tquery: string\n}\n\nexport class MonitorMongo {\n\tprivate _startTime = 0;\n\tprivate _method = '';\n\tprivate _collection = '';\n\tprivate _query = '';\n\n\tconstructor(method: string, collection: string, query: string) {\n\t\tthis._startTime = Date.now();\n\t\tthis._method = method;\n\t\tthis._collection = collection;\n\t\tthis._query = query;\n\t}\n\n\tpublic finish() {\n\t\tlet endTime = Date.now();\n\n\t\tlet mongoMonitor: MongoMonitorModel = {\n\t\t\tdate: new Date(endTime),\n\t\t\tmethod: this._method,\n\t\t\tcollection: this._collection,\n\t\t\tquery: this._query,\n\t\t\tduration: endTime - this._startTime\n\t\t};\n\n\t\tResolveIOServer.getMainServer().getMonitorManager().addMongoTracker(mongoMonitor);\n\t}\n}\n\nexport class MonitorManagerFunction {\n\tprivate _functionCnt = 0;\n\tprivate _functions: MonitorFunction[] = [];\n\tprivate _functionLastCompletedWS: {\n\t\t[id_socket: string]: MonitorFunction;\n\t} = {};\n\tprivate _functionLastCompleted: MonitorFunction;\n\n\tconstructor() {}\n\n\tstartMonitorFunction(functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tlet newMonitorFunction = new MonitorFunction(this._functionCnt++, functionType, functionName, user, id_socket, data);\n\t\tthis._functions.push(newMonitorFunction);\n\t\treturn newMonitorFunction.id;\n\t}\n\n\tfinishMonitorFunction(id: number) {\n\t\tlet monitor = this._functions.filter(a => a.id === id)[0];\n\t\tif (monitor) {\n\t\t\tmonitor.finish();\n\t\t\tthis._functionLastCompleted = monitor;\n\t\t\tthis._functionLastCompletedWS[monitor.id_socket] = monitor;\n\t\t\tthis._functions.splice(this._functions.map(a => a.id).indexOf(id), 1);\n\t\t}\n\t}\n\n\tgetActiveMonitorFunctions() {\n\t\treturn this._functions;\n\t}\n\n\tgetLastCompletedMonitorFunction() {\n\t\treturn this._functionLastCompleted;\n\t}\n\t\n\tgetLastCompletedMonitorFunctionWS() {\n\t\treturn this._functionLastCompletedWS;\n\t}\n}\n\nexport type MonitorFunctionType = 'Cron Method' | 'Internal Method' | 'Method' | 'User Specific Publication' | 'Publication';\nexport class MonitorFunction {\n\tprivate _timer: NodeJS.Timeout = null;\n\tprivate _maxDiff = 0;\n\tprivate _functionType: MonitorFunctionType;\n\tprivate _functionName: string;\n\tprivate _user: string;\n\tprivate _data: any;\n\tpublic startTime: Date = null;\n\tpublic endTime: Date = null;\n\tpublic lastTime: Date = null;\n\tpublic id_socket = '';\n\tpublic id = 0;\n\n\tconstructor(id: number, functionType: MonitorFunctionType, functionName: string, user: string, id_socket: string, data: any) {\n\t\tthis.id = id;\n\t\tthis.startTime = new Date();\n\t\tthis._functionType = functionType;\n\t\tthis._functionName = functionName;\n\t\tthis._user = user;\n\t\tthis.id_socket = id_socket;\n\t\tthis._data = data;\n\t\tthis.lastTime = new Date();\n\n\t\tthis._timer = setInterval(() => {\n\t\t\tlet diff = Date.now() - this.lastTime.getTime();\n\t\t\tif (diff > this._maxDiff) {\n\t\t\t\tthis._maxDiff = diff;\n\t\t\t}\n\n\t\t\tthis.lastTime = new Date();\n\t\t}, 1000);\n\t}\n\n\tpublic finish() {\n\t\tif (this._timer) {\n\t\t\tclearInterval(this._timer);\n\t\t\tthis._timer = null;\n\t\t}\n\n\t\tthis.endTime = new Date();\n\n\t\tif (this._maxDiff >= 5000) {\n\t\t\tResolveIOServer.getMainServer().getMethodManager().sendEmail('dev@resolveio.com', ResolveIOServer.getMainServer().getServerConfig()['CLIENT_NAME'] + ' - Slow ' + this._functionType + ' Detected', '', (this._user ? 'User: ' + this._user + '\\n' : '') + this._functionType + ': ' + this._functionName + '\\n' + 'During this function the event loop did not run for ' + this._maxDiff + ' ms (' + this._maxDiff / 1000 + ' sec)' + (this._data ? '\\n\\nData: ' + JSON.stringify(this._data, null, 2) : ''));\n\t\t}\n\t}\n}"]}
|