mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 23:06:06 +08:00
28 lines
755 B
TypeScript
28 lines
755 B
TypeScript
import { ServerUnaryCall, sendUnaryData } from '@grpc/grpc-js';
|
|
import { AddCronRequest, AddCronResponse } from '../protos/cron';
|
|
import nodeSchedule from 'node-schedule';
|
|
import { scheduleStacks } from './data';
|
|
import { runCron } from '../shared/runCron';
|
|
|
|
const addCron = (
|
|
call: ServerUnaryCall<AddCronRequest, AddCronResponse>,
|
|
callback: sendUnaryData<AddCronResponse>,
|
|
) => {
|
|
for (const item of call.request.crons) {
|
|
const { id, schedule, command } = item;
|
|
if (scheduleStacks.has(id)) {
|
|
scheduleStacks.get(id)?.cancel();
|
|
}
|
|
scheduleStacks.set(
|
|
id,
|
|
nodeSchedule.scheduleJob(id, schedule, async () => {
|
|
runCron(`ID=${id} ${command}`)
|
|
}),
|
|
);
|
|
}
|
|
|
|
callback(null, null);
|
|
};
|
|
|
|
export { addCron };
|