64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
import { ElsSmartlogDestination, type IStandardLogParams } from './els.classes.smartlogdestination.js';
|
|
|
|
export class ElasticScheduler {
|
|
elasticSearchRef: ElsSmartlogDestination<any>;
|
|
docsScheduled = false;
|
|
docsStorage: any[] = [];
|
|
|
|
// maximum size of the buffer
|
|
maxBufferSize = 500;
|
|
|
|
constructor(elasticLogRefArg: ElsSmartlogDestination<any>) {
|
|
this.elasticSearchRef = elasticLogRefArg;
|
|
}
|
|
|
|
public addFailedDoc(objectArg: any | IStandardLogParams) {
|
|
this.addToStorage(objectArg);
|
|
this.setRetry();
|
|
}
|
|
|
|
public scheduleDoc(logObject: any) {
|
|
this.addToStorage(logObject);
|
|
}
|
|
|
|
private addToStorage(logObject: any) {
|
|
this.docsStorage.push(logObject);
|
|
|
|
// if buffer is full, send logs immediately
|
|
if (this.docsStorage.length >= this.maxBufferSize) {
|
|
this.flushLogsToElasticSearch();
|
|
}
|
|
}
|
|
|
|
private flushLogsToElasticSearch() {
|
|
const oldStorage = this.docsStorage;
|
|
this.docsStorage = [];
|
|
|
|
for (let logObject of oldStorage) {
|
|
this.elasticSearchRef.log(logObject, true);
|
|
}
|
|
}
|
|
|
|
public setRetry() {
|
|
setTimeout(() => {
|
|
this.flushLogsToElasticSearch();
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|