import APISample from "../common/data/APISample"; import ApiBufferKey from "./ApiBufferKey"; import AgentConfig from "../common/data/AgentConfig"; import SimpleBuffer from "./SimpleBuffer"; import BufferMap from "./BufferMap"; abstract class BufferManagerWorker { abstract init(): boolean; abstract offer(key: K | ApiBufferKey, apiSample: APISample): boolean; abstract canOffer(key: K | ApiBufferKey): boolean; protected readonly bufferMap: BufferMap = new BufferMap(); private readonly agentConfig: AgentConfig | null; private readonly bufferSyncExecutorService: NodeJS.Timer | undefined; protected readonly ctUrl: string | null; getOperatingConfig(): AgentConfig | null { return this.agentConfig; } protected constructor(agentConfig: AgentConfig | null, ctUrl: string | null) { this.agentConfig = agentConfig; this.ctUrl = ctUrl; if (agentConfig) { this.bufferSyncExecutorService = setInterval(() => { this.syncForKeys(); }, agentConfig.getBufferSyncFreqInSec() * 1000); } } shutdown(): boolean { if (this.bufferSyncExecutorService) { clearInterval(this.bufferSyncExecutorService); } this.cleanUpBufferMap(); return true; } private cleanUpBufferMap(): void { this.syncForKeys(); let keys: IterableIterator = this.bufferMap .getBufferMap() .keys(); if (keys) { for (let key of keys) { let buffer: SimpleBuffer | undefined = this.bufferMap.getBuffer(key); if (buffer) { buffer.clear(); } } this.bufferMap.clear(); } } protected getUri(): string { return "/api/v1/data-ingestion/api-sample"; } abstract syncForKey(key: ApiBufferKey): void; private syncForKeys(): void { let keys: IterableIterator = this.bufferMap .getBufferMap() .keys(); if (keys) { for (let key of keys) { this.syncForKey(key); } } } } export default BufferManagerWorker;