| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- "use strict";
- // oms/services/cron-jobs/index.ts
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.startCronJobs = startCronJobs;
- const node_cron_1 = __importDefault(require("node-cron")); // Import node-cron library
- const database_1 = require("../../src/database");
- // Define the settings array for cron jobs
- // Each element: [name: string, schedule: string, jobModule: CronJobModule]
- const settings = [
- // 假设这些文件将存在于 oms/services/cron-jobs/ 目录下
- ["done-rate", "10 0 * * *", require("./done-rate2")], // 每天凌晨0点10分, 统计作品完成率
- ["daily-activity-detector", "50 0 * * *", require("./daily-activity-detector")], // 每天凌晨0点50分, 检查是否需要生成新的推送消息
- ["message-sender", "*/5 * * * *", require("./message-sender")], // 每5分钟运行一次
- ["active-user-daily-notify", "30 18 * * *", require("./active-user-daily-notify")], // 每天下午6点,开始活跃用户新作品消息推送
- ];
- /**
- * Starts all scheduled cron jobs.
- * Includes database connection and task scheduling.
- * @returns Promise<void>
- */
- async function startCronJobs() {
- console.log("[Cron Jobs] Initializing all scheduled tasks...");
- // 在启动所有定时任务之前,首先建立数据库连接
- await (0, database_1.connectToDatabase)();
- // Iterate through settings and schedule each job
- settings.forEach((setting) => {
- const [name, schedule, job] = setting;
- if (!job || typeof job.run !== "function") {
- // Check if job module and run function exist
- console.error(`[Cron Jobs] Job [${name}] is missing a run() function or is not a valid module. Skipping.`);
- return;
- }
- console.log(`[Cron Jobs] Installing job [${name}] to run at '${schedule}'`);
- node_cron_1.default.schedule(schedule, async () => {
- const startTime = new Date().toLocaleString();
- console.log(`[Cron Jobs] Running job [${name}]@'${schedule}' started @ ${startTime}`);
- try {
- const result = await job.run(); // Execute the job's run function
- console.log(`[Cron Jobs] Job [${name}] completed successfully @ ${new Date().toLocaleString()}. Result:`, result);
- }
- catch (error) {
- console.error(`[Cron Jobs] Job [${name}] failed @ ${new Date().toLocaleString()}. Error:`, error);
- }
- });
- });
- console.log("[Cron Jobs] All cron jobs started.");
- }
- // If this file is run directly (e.g., using `node dist/services/cron-jobs/index.js`)
- if (require.main === module) {
- startCronJobs().catch((error) => {
- console.error("[Cron Jobs] Failed to start cron jobs:", error);
- process.exit(1);
- });
- // Handle graceful shutdown
- process.on("SIGINT", async () => {
- console.log("[Cron Jobs] Shutting down...");
- node_cron_1.default.getTasks().forEach((task) => task.stop()); // Stop all scheduled tasks
- // await disconnectFromDatabase(); // 在退出前断开数据库连接
- console.log("[Cron Jobs] All cron tasks stopped.");
- process.exit(0);
- });
- process.on("SIGTERM", async () => {
- console.log("[Cron Jobs] Shutting down...");
- node_cron_1.default.getTasks().forEach((task) => task.stop());
- // await disconnectFromDatabase(); // 在退出前断开数据库连接
- console.log("[Cron Jobs] All cron tasks stopped.");
- process.exit(0);
- });
- }
|