index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // oms/services/cron-jobs/index.ts
  2. import cron from "node-cron"; // Import node-cron library
  3. import { initializeClients, clickhouseService } from "../../src/services/clients"; // 从新的文件导入
  4. // Define an interface for a cron job module
  5. interface CronJobModule {
  6. run: () => Promise<any>; // Expecting a run function that returns a Promise
  7. }
  8. // Define the settings array for cron jobs
  9. // Each element: [name: string, schedule: string, jobModule: CronJobModule]
  10. const settings: [string, string, CronJobModule][] = [
  11. // 假设这些文件将存在于 oms/services/cron-jobs/ 目录下
  12. ["done-rate", "10 0 * * *", require("./done-rate2") as CronJobModule], // 每天凌晨0点10分, 统计作品完成率
  13. ["daily-activity-detector", "50 0 * * *", require("./daily-activity-detector") as CronJobModule], // 每天凌晨0点50分, 检查是否需要生成新的推送消息
  14. // ["message-sender", "*/5 * * * *", require("./message-sender") as CronJobModule], // 每5分钟运行一次, 已经单独剥离出去了(message-seender-service),定时任务这里取消了
  15. // ["active-user-daily-notify", "30 18 * * *", require("./active-user-daily-notify") as CronJobModule], // 每天下午6点,开始活跃用户新作品消息推送
  16. // ["fcm-notify", "30 18 * * *", require("./fcm-notify") as CronJobModule], // 每天下午6点,基于原来的active-user-daily-notify,增加schedule推送,AB测试
  17. // ["daily-notify-at-morning", "0 19 * * *", require("./notify/daily-notify-at-morning") as CronJobModule], // 每天下午7点,对应巴西时间早上8点推送一轮
  18. // ["daily-notify-at-midday", "0 23 * * *", require("./notify/daily-notify-at-midday") as CronJobModule], // 每天晚上11点,对应巴西时间中午12点推送一轮
  19. // ["daily-notify-at-evening", "0 3 * * *", require("./notify/daily-notify-at-evening") as CronJobModule], // 每天凌晨3点,对应巴西时间下午4点
  20. // ["daily-notify-at-afternoon", "0 7 * * *", require("./notify/daily-notify-at-afternoon") as CronJobModule], // 每天上午7点,对应巴西时间晚上8点
  21. ["local-timezone-notify", "5 17 * * *", require("./notify/local-timezone-notify") as CronJobModule], // 每天下午6点,开始制定当天的推送计划
  22. ];
  23. /**
  24. * Starts all scheduled cron jobs.
  25. * Includes database connection and task scheduling.
  26. * @returns Promise<void>
  27. */
  28. export async function startCronJobs(): Promise<void> {
  29. console.log("[Cron Jobs] Initializing all scheduled tasks...");
  30. // 先初始化客户端服务
  31. await initializeClients();
  32. // Iterate through settings and schedule each job
  33. settings.forEach((setting) => {
  34. const [name, schedule, job] = setting;
  35. if (!job || typeof job.run !== "function") {
  36. // Check if job module and run function exist
  37. console.error(`[Cron Jobs] Job [${name}] is missing a run() function or is not a valid module. Skipping.`);
  38. return;
  39. }
  40. console.log(`[Cron Jobs] Installing job [${name}] to run at '${schedule}'`);
  41. cron.schedule(schedule, async () => {
  42. const startTime = new Date().toLocaleString();
  43. console.log(`[Cron Jobs] Running job [${name}]@'${schedule}' started @ ${startTime}`);
  44. try {
  45. const result = await job.run(); // Execute the job's run function
  46. console.log(`[Cron Jobs] Job [${name}] completed successfully @ ${new Date().toLocaleString()}. Result:`, result);
  47. } catch (error) {
  48. console.error(`[Cron Jobs] Job [${name}] failed @ ${new Date().toLocaleString()}. Error:`, error);
  49. }
  50. });
  51. });
  52. console.log("[Cron Jobs] All cron jobs started.");
  53. }
  54. // If this file is run directly (e.g., using `node dist/services/cron-jobs/index.js`)
  55. if (require.main === module) {
  56. startCronJobs().catch((error) => {
  57. console.error("[Cron Jobs] Failed to start cron jobs:", error);
  58. process.exit(1);
  59. });
  60. // Handle graceful shutdown
  61. process.on("SIGINT", async () => {
  62. console.log("[Cron Jobs] Shutting down...");
  63. cron.getTasks().forEach((task) => task.stop()); // Stop all scheduled tasks
  64. // await disconnectFromDatabase(); // 在退出前断开数据库连接
  65. console.log("[Cron Jobs] All cron tasks stopped.");
  66. process.exit(0);
  67. });
  68. process.on("SIGTERM", async () => {
  69. console.log("[Cron Jobs] Shutting down...");
  70. cron.getTasks().forEach((task) => task.stop());
  71. // await disconnectFromDatabase(); // 在退出前断开数据库连接
  72. console.log("[Cron Jobs] All cron tasks stopped.");
  73. process.exit(0);
  74. });
  75. }