Commit 4c79b31e by Walter Huang

Merge branch 'feat-detect-master-if-publish' into 'develop'

Feat detect master if publish See merge request !2
parents 1dc2af4e 515f1e5e
docker build -t reg.i.strikingly.com/falcon:v0.5 .
docker push reg.i.strikingly.com/falcon:v0.5
\ No newline at end of file
docker build -t reg.i.strikingly.com/falcon:v0.7.2 .
docker push reg.i.strikingly.com/falcon:v0.7.2
No preview for this file type
import { spawn } from "bun";
import { spawnSync } from "bun";
import Redis from "ioredis";
// 任务执行入口,自动设置 SLACK_WEBHOOK 并依次运行 gsheet、gsheet:ai 和 notify-slack
async function main() {
......@@ -6,42 +7,131 @@ async function main() {
...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) {
task_trigger = "should_run_tasks";
await redis.del("falcon:should_run_tasks");
} else if (isMonday11AMInBeijing()) {
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
await spawn({
console.log(`[${task_trigger}] 开始执行 detect`);
const detectResult = spawnSync({
cmd: ["bun", "run", "detect"],
env,
stdout: "inherit",
stderr: "inherit"
});
if (detectResult.exitCode !== 0) {
console.error(`[${task_trigger}] detect 执行失败,退出码: ${detectResult.exitCode}`);
await redis.quit();
process.exit(1);
}
console.log(`[${task_trigger}] detect 执行完成`);
// 运行 bun run task:gsheet
await spawn({
console.log(`[${task_trigger}] 开始执行 task:gsheet`);
const gsheetResult = spawnSync({
cmd: ["bun", "run", "task:gsheet"],
env,
stdout: "inherit",
stderr: "inherit"
});
if (gsheetResult.exitCode !== 0) {
console.error(`[${task_trigger}] task:gsheet 执行失败,退出码: ${gsheetResult.exitCode}`);
await redis.quit();
process.exit(1);
}
console.log(`[${task_trigger}] task:gsheet 执行完成`);
// 延迟执行 task:gsheet:ai 避免 Google Sheet 限流
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));
// 运行 bun run task:gsheet:ai
await spawn({
console.log(`[${task_trigger}] 开始执行 task:gsheet:ai`);
const gsheetAiResult = spawnSync({
cmd: ["bun", "run", "task:gsheet:ai"],
env,
stdout: "inherit",
stderr: "inherit"
});
if (gsheetAiResult.exitCode !== 0) {
console.error(`[${task_trigger}] task:gsheet:ai 执行失败,退出码: ${gsheetAiResult.exitCode}`);
await redis.quit();
process.exit(1);
}
console.log(`[${task_trigger}] task:gsheet:ai 执行完成`);
// 运行 bun run notify-slack
await spawn({
console.log(`[${task_trigger}] 开始执行 notify-slack`);
const notifySlackResult = spawnSync({
cmd: ["bun", "run", "notify-slack"],
env,
stdout: "inherit",
stderr: "inherit"
});
if (notifySlackResult.exitCode !== 0) {
console.error(`[${task_trigger}] notify-slack 执行失败,退出码: ${notifySlackResult.exitCode}`);
await redis.quit();
process.exit(1);
}
console.log(`[${task_trigger}] notify-slack 执行完成`);
if (task_trigger === "should_run_tasks") {
console.log(`[${task_trigger}] should_run_tasks 执行完毕,已清理 redis key。`);
} else if (task_trigger === "cron") {
console.log(`[${task_trigger}] cron 任务执行完毕。`);
} else {
console.log(`[${task_trigger}] 手动触发任务执行完毕。`);
}
await redis.quit();
console.log(`[${task_trigger}] 任务执行完毕,退出。`);
process.exit(0);
}
function isMonday11AMInBeijing() {
const now = new Date();
// 获取北京时间的各项
const formatter = new Intl.DateTimeFormat('zh-CN', {
timeZone: 'Asia/Shanghai',
hour12: false,
weekday: 'short',
hour: '2-digit'
});
const parts = formatter.formatToParts(now);
const weekday = parts.find(p => p.type === 'weekday')?.value;
const hour = Number(parts.find(p => p.type === 'hour')?.value);
console.log('[isMonday11AMInBeijing] weekday:', weekday, 'hour:', hour);
// 周一11点
return (weekday === '周一' || weekday === 'Mon') && hour === 11;
}
// 调用入口
......
......@@ -12,6 +12,7 @@
"@slack/web-api": "^7.9.3",
"deep-diff": "^1.0.2",
"googleapis": "^150.0.1",
"ioredis": "^5.3.2",
"jsdom": "^26.1.0"
},
"scripts": {
......
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