mirror of
https://github.com/ostris/ai-toolkit.git
synced 2026-01-26 16:39:47 +00:00
35 lines
807 B
TypeScript
35 lines
807 B
TypeScript
import processQueue from './actions/processQueue';
|
|
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() {
|
|
await processQueue();
|
|
}
|
|
}
|
|
|
|
// it automatically starts the loop
|
|
const cronWorker = new CronWorker();
|
|
console.log('Cron worker started with interval:', cronWorker.interval, 'ms');
|