1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/services/scheduler-service.ts

36 lines
909 B
TypeScript
Raw Normal View History

2023-01-11 16:15:53 +01:00
import { Logger, LogProvider } from '../logger';
export default class SchedulerService {
private intervalIds: NodeJS.Timer[] = [];
private logger: Logger;
constructor(getLogger: LogProvider) {
this.logger = getLogger('/services/scheduler-service.ts');
}
async schedule(
scheduledFunction: () => void,
timeMs: number,
): Promise<void> {
2023-01-11 16:15:53 +01:00
this.intervalIds.push(
setInterval(async () => {
try {
await scheduledFunction();
} catch (e) {
this.logger.error('scheduled job failed', e);
}
}, timeMs).unref(),
);
try {
await scheduledFunction();
} catch (e) {
this.logger.error('scheduled job failed', e);
}
2023-01-11 16:15:53 +01:00
}
stop(): void {
this.intervalIds.forEach(clearInterval);
}
}