elasticsearch/ts/elasticsearch.classes.logscheduler.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-11-07 10:38:53 +00:00
import { ElasticSearch, IStandardLogParams } from './elasticsearch.classes.elasticsearch';
2018-01-27 18:16:05 +00:00
export class LogScheduler {
2018-11-07 10:38:53 +00:00
elasticLogRef: ElasticSearch<any>;
2018-01-27 18:16:05 +00:00
logsScheduled = false;
logStorage: any[] = [];
2018-11-07 10:38:53 +00:00
constructor(elasticLogRefArg: ElasticSearch<any>) {
2018-01-27 18:16:05 +00:00
this.elasticLogRef = elasticLogRefArg;
}
addFailedLog(objectArg: any | IStandardLogParams) {
this.logStorage.push(objectArg);
this.setRetry();
}
scheduleLog(logObject: any) {
this.logStorage.push(logObject);
}
setRetry() {
setTimeout(() => {
const oldStorage = this.logStorage;
this.logStorage = [];
for (let logObject of oldStorage) {
this.elasticLogRef.log(logObject, true);
}
if (this.logStorage.length === 0) {
2018-08-12 14:42:09 +00:00
console.log('ElasticLog retry success!!!');
2018-01-27 18:16:05 +00:00
this.logsScheduled = false;
} else {
2018-08-12 14:42:09 +00:00
console.log('ElasticLog retry failed');
2018-01-27 18:16:05 +00:00
this.setRetry();
}
}, 5000);
}
deferSend() {
if (!this.logsScheduled) {
2018-08-12 14:42:09 +00:00
console.log('Retry ElasticLog in 5 seconds!');
2018-01-27 18:16:05 +00:00
this.logsScheduled = true;
this.setRetry();
}
}
}