mirror of
https://github.com/ostris/ai-toolkit.git
synced 2026-01-30 02:19:47 +00:00
32 lines
727 B
TypeScript
32 lines
727 B
TypeScript
class CronWorker {
|
|
interval: number;
|
|
is_running: boolean;
|
|
intervalId: NodeJS.Timeout;
|
|
constructor() {
|
|
this.interval = 1000; // Default interval of 1 second
|
|
this.is_running = false;
|
|
this.intervalId = setInterval(() => {
|
|
this.run();
|
|
}, this.interval);
|
|
}
|
|
async run() {
|
|
if (this.is_running) {
|
|
return;
|
|
}
|
|
this.is_running = true;
|
|
try {
|
|
// Loop logic here
|
|
await this.loop();
|
|
} catch (error) {
|
|
console.error('Error in cron worker loop:', error);
|
|
}
|
|
this.is_running = false;
|
|
}
|
|
|
|
async loop() {}
|
|
}
|
|
|
|
// it automatically starts the loop
|
|
const cronWorker = new CronWorker();
|
|
console.log('Cron worker started with interval:', cronWorker.interval, 'ms');
|