Commit afdeb96b by Haohao Jiang

fix: reconstruct trigger logic

parent 16f482b1
...@@ -2,12 +2,42 @@ import { spawn } from "bun"; ...@@ -2,12 +2,42 @@ import { spawn } from "bun";
import Redis from "ioredis"; import Redis from "ioredis";
// 任务执行入口,自动设置 SLACK_WEBHOOK 并依次运行 gsheet、gsheet:ai 和 notify-slack // 任务执行入口,自动设置 SLACK_WEBHOOK 并依次运行 gsheet、gsheet:ai 和 notify-slack
async function processDetect() { async function main() {
const env = { const env = {
...process.env ...process.env
}; };
const redis = new Redis(Bun.env.REDIS_URL);
let task_trigger = "manual";
const shouldRun = await redis.get("falcon:should_run_tasks");
if (shouldRun === "true") {
task_trigger = "should_run_tasks";
await redis.del("falcon:should_run_tasks");
} else if (isMonday11AM()) {
const mondayKey = "falcon:monday_11am_executed";
const alreadyExecuted = await redis.get(mondayKey);
if (alreadyExecuted) {
task_trigger = "cron";
console.log(`[${task_trigger}] 本周一11点任务已执行,跳过。`);
await redis.quit();
process.exit(0);
} else {
task_trigger = "cron";
// 设置标记,24小时后自动过期
await redis.set(mondayKey, "true", "EX", 60 * 60 * 24);
console.log(`[${task_trigger}] 定时任务触发,已设置本周标记。`);
}
} else {
task_trigger = "manual";
console.log(`[${task_trigger}] should_run_tasks 不是 'true',且不是定时任务,跳过。`);
await redis.quit();
process.exit(0);
}
// 运行 bun run detect // 运行 bun run detect
console.log(`[${task_trigger}] 开始执行 detect`);
await spawn({ await spawn({
cmd: ["bun", "run", "detect"], cmd: ["bun", "run", "detect"],
env, env,
...@@ -16,6 +46,7 @@ async function processDetect() { ...@@ -16,6 +46,7 @@ async function processDetect() {
}); });
// 运行 bun run task:gsheet // 运行 bun run task:gsheet
console.log(`[${task_trigger}] 开始执行 task:gsheet`);
await spawn({ await spawn({
cmd: ["bun", "run", "task:gsheet"], cmd: ["bun", "run", "task:gsheet"],
env, env,
...@@ -25,10 +56,11 @@ async function processDetect() { ...@@ -25,10 +56,11 @@ async function processDetect() {
// 延迟执行 task:gsheet:ai 避免 Google Sheet 限流 // 延迟执行 task:gsheet:ai 避免 Google Sheet 限流
const delayMs = parseInt(process.env.GSHEET_AI_DELAY_MS ?? "60000", 10); const delayMs = parseInt(process.env.GSHEET_AI_DELAY_MS ?? "60000", 10);
console.log(`Waiting ${delayMs}ms before running task:gsheet:ai to avoid rate limiting`); console.log(`[${task_trigger}] Waiting ${delayMs}ms before running task:gsheet:ai to avoid rate limiting`);
await new Promise<void>(resolve => setTimeout(resolve, delayMs)); await new Promise<void>(resolve => setTimeout(resolve, delayMs));
// 运行 bun run task:gsheet:ai // 运行 bun run task:gsheet:ai
console.log(`[${task_trigger}] 开始执行 task:gsheet:ai`);
await spawn({ await spawn({
cmd: ["bun", "run", "task:gsheet:ai"], cmd: ["bun", "run", "task:gsheet:ai"],
env, env,
...@@ -37,47 +69,34 @@ async function processDetect() { ...@@ -37,47 +69,34 @@ async function processDetect() {
}); });
// 运行 bun run notify-slack // 运行 bun run notify-slack
console.log(`[${task_trigger}] 开始执行 notify-slack`);
await spawn({ await spawn({
cmd: ["bun", "run", "notify-slack"], cmd: ["bun", "run", "notify-slack"],
env, env,
stdout: "inherit", stdout: "inherit",
stderr: "inherit" stderr: "inherit"
}); });
}
function isMonday11AM() { if (task_trigger === "should_run_tasks") {
const now = new Date(); console.log(`[${task_trigger}] should_run_tasks 执行完毕,已清理 redis key。`);
return now.getDay() === 1 && now.getHours() === 11; } else if (task_trigger === "cron") {
} console.log(`[${task_trigger}] cron 任务执行完毕。`);
async function main() {
const redis = new Redis(Bun.env.REDIS_URL);
const shouldRun = await redis.get("falcon:should_run_tasks");
if (shouldRun === "true") {
await processDetect();
await redis.del("falcon:should_run_tasks");
console.log("should_run_tasks 执行完毕,已清理 redis key。");
} else if (isMonday11AM()) {
const mondayKey = "falcon:monday_11am_executed";
const alreadyExecuted = await redis.get(mondayKey);
if (alreadyExecuted) {
console.log("本周一11点任务已执行,跳过。");
} else {
await processDetect();
// 设置标记,24小时后自动过期
await redis.set(mondayKey, "true", "EX", 60 * 60 * 24);
console.log("定时任务触发,已执行 main 逻辑,并设置本周标记。");
}
} else { } else {
console.log("should_run_tasks 不是 'true',且不是定时任务,跳过。"); console.log(`[${task_trigger}] 手动触发任务执行完毕。`);
} }
await redis.quit(); await redis.quit();
} }
function isMonday11AM() {
const now = new Date();
return now.getDay() === 1 && now.getHours() === 11;
}
// 调用入口 // 调用入口
main().catch(err => { main()
console.error(err); .then(() => process.exit(0))
process.exit(1); .catch(err => {
}); console.error(err);
\ No newline at end of file process.exit(1);
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment