nextav/src/lib/transcode/heartbeat.ts

25 lines
557 B
TypeScript

export class HeartbeatManager {
private timers = new Map<string, NodeJS.Timeout>();
arm(jobId: string, timeoutMs: number, onTimeout: () => void): void {
this.clear(jobId);
const timer = setTimeout(onTimeout, timeoutMs);
timer.unref();
this.timers.set(jobId, timer);
}
clear(jobId: string): void {
const timer = this.timers.get(jobId);
if (timer) {
clearTimeout(timer);
this.timers.delete(jobId);
}
}
clearAll(): void {
for (const jobId of this.timers.keys()) {
this.clear(jobId);
}
}
}