2023-07-04 07:13:14 +00:00
|
|
|
import { ElasticSearch, type IStandardLogParams } from './elasticsearch.classes.elasticsearch.js';
|
2018-11-09 22:15:11 +00:00
|
|
|
|
|
|
|
export class ElasticScheduler {
|
|
|
|
elasticSearchRef: ElasticSearch<any>;
|
|
|
|
docsScheduled = false;
|
|
|
|
docsStorage: any[] = [];
|
|
|
|
|
|
|
|
constructor(elasticLogRefArg: ElasticSearch<any>) {
|
|
|
|
this.elasticSearchRef = elasticLogRefArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public addFailedDoc(objectArg: any | IStandardLogParams) {
|
|
|
|
this.docsStorage.push(objectArg);
|
|
|
|
this.setRetry();
|
|
|
|
}
|
|
|
|
public scheduleDoc(logObject: any) {
|
|
|
|
this.docsStorage.push(logObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setRetry() {
|
|
|
|
setTimeout(() => {
|
|
|
|
const oldStorage = this.docsStorage;
|
|
|
|
this.docsStorage = [];
|
|
|
|
for (let logObject of oldStorage) {
|
|
|
|
this.elasticSearchRef.log(logObject, true);
|
|
|
|
}
|
|
|
|
if (this.docsStorage.length === 0) {
|
|
|
|
console.log('ElasticLog retry success!!!');
|
|
|
|
this.docsScheduled = false;
|
|
|
|
} else {
|
|
|
|
console.log('ElasticLog retry failed');
|
|
|
|
this.setRetry();
|
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
public deferSend() {
|
|
|
|
if (!this.docsScheduled) {
|
|
|
|
console.log('Retry ElasticLog in 5 seconds!');
|
|
|
|
this.docsScheduled = true;
|
|
|
|
this.setRetry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|