elasticsearch/ts/elasticlog.classes.logscheduler.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-08-12 14:42:09 +00:00
import { ElasticLog, IStandardLogParams } from './elasticlog.classes.elasticlog';
2018-01-27 18:16:05 +00:00
export class LogScheduler {
elasticLogRef: ElasticLog<any>;
logsScheduled = false;
logStorage: any[] = [];
constructor(elasticLogRefArg: ElasticLog<any>) {
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();
}
}
}