Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
Falcon
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Walter Huang
Falcon
Commits
afdeb96b
Commit
afdeb96b
authored
Jul 03, 2025
by
Haohao Jiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: reconstruct trigger logic
parent
16f482b1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
31 deletions
+50
-31
index.ts
index.ts
+50
-31
No files found.
index.ts
View file @
afdeb96b
...
...
@@ -2,12 +2,42 @@ import { spawn } from "bun";
import
Redis
from
"ioredis"
;
// 任务执行入口,自动设置 SLACK_WEBHOOK 并依次运行 gsheet、gsheet:ai 和 notify-slack
async
function
processDetect
()
{
async
function
main
()
{
const
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
console
.
log
(
`[
${
task_trigger
}
] 开始执行 detect`
);
await
spawn
({
cmd
:
[
"bun"
,
"run"
,
"detect"
],
env
,
...
...
@@ -16,6 +46,7 @@ async function processDetect() {
});
// 运行 bun run task:gsheet
console
.
log
(
`[
${
task_trigger
}
] 开始执行 task:gsheet`
);
await
spawn
({
cmd
:
[
"bun"
,
"run"
,
"task:gsheet"
],
env
,
...
...
@@ -25,10 +56,11 @@ async function processDetect() {
// 延迟执行 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
console
.
log
(
`[
${
task_trigger
}
] 开始执行 task:gsheet:ai`
);
await
spawn
({
cmd
:
[
"bun"
,
"run"
,
"task:gsheet:ai"
],
env
,
...
...
@@ -37,47 +69,34 @@ async function processDetect() {
});
// 运行 bun run notify-slack
console
.
log
(
`[
${
task_trigger
}
] 开始执行 notify-slack`
);
await
spawn
({
cmd
:
[
"bun"
,
"run"
,
"notify-slack"
],
env
,
stdout
:
"inherit"
,
stderr
:
"inherit"
});
}
function
isMonday11AM
()
{
const
now
=
new
Date
();
return
now
.
getDay
()
===
1
&&
now
.
getHours
()
===
11
;
}
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 逻辑,并设置本周标记。"
);
}
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
(
"should_run_tasks 不是 'true',且不是定时任务,跳过。"
);
console
.
log
(
`[
${
task_trigger
}
] 手动触发任务执行完毕。`
);
}
await
redis
.
quit
();
}
function
isMonday11AM
()
{
const
now
=
new
Date
();
return
now
.
getDay
()
===
1
&&
now
.
getHours
()
===
11
;
}
// 调用入口
main
().
catch
(
err
=>
{
main
()
.
then
(()
=>
process
.
exit
(
0
))
.
catch
(
err
=>
{
console
.
error
(
err
);
process
.
exit
(
1
);
});
\ No newline at end of file
});
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment