qinglong/back
Copilot 90ddf0fb57
Fix race condition preventing scheduled tasks from executing in clustered mode (#2819)
* Initial plan

* Fix race condition causing scheduled tasks not to run

Added synchronization to ensure gRPC worker is ready before HTTP worker starts. This prevents the race condition where autosave_crontab() tries to register cron jobs before the gRPC server is ready to accept them.

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

* Add timeout protection for gRPC worker initialization

Added 30-second timeout to prevent system hang if gRPC worker fails to start. This provides better error handling and prevents indefinite waiting.

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

* Refactor worker ready logic and improve restart handling

- Extracted waitForWorkerReady() method for better code reusability
- Improved worker restart logic to wait for gRPC worker readiness
- This addresses code review feedback for better maintainability

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

* Improve logging accuracy for worker restarts

- Fixed log messages to accurately reflect worker state
- Added proper logging after gRPC worker is confirmed ready
- Improved HTTP worker restart logging with PID
- Addresses code review feedback for better clarity

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

* Revert unnecessary pnpm-lock.yaml changes

The pnpm-lock.yaml was unintentionally updated when installing dependencies for testing. No package dependencies were actually changed - only existing code was modified in back/app.ts. Reverting to original state.

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

* Re-register cron jobs when gRPC worker restarts

When the gRPC worker restarts, the scheduled tasks need to be re-added to the new gRPC server instance. This fix:

1. Tracks the HTTP worker reference in the master process
2. Sends a 'reregister-crons' message to the HTTP worker after gRPC restarts
3. HTTP worker calls autosave_crontab() to re-register all cron jobs with the new gRPC server

This ensures scheduled tasks continue to work after a gRPC worker restart.

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
2025-11-12 00:59:22 +08:00
..
api 升级 cron-parser 2025-11-11 00:37:03 +08:00
config 移除 nginx 2025-11-02 19:29:59 +08:00
data 修复日志目录逻辑 2025-11-09 21:42:45 +08:00
interface 修改定时规则类型 2025-02-21 01:35:08 +08:00
loaders 升级 cron-parser 2025-11-11 00:37:03 +08:00
middlewares 修改服务启动逻辑 2025-05-07 09:30:00 +08:00
protos QLAPI.systemNotify 支持自定义通知类型和参数 2025-06-24 02:00:51 +08:00
schedule 修复模块注入 2025-10-26 22:32:03 +08:00
services 升级 cron-parser 2025-11-11 00:37:03 +08:00
shared 修复创建脚本可能失败 2025-05-20 01:00:08 +08:00
types 修改服务启动逻辑 2025-05-07 09:30:00 +08:00
validation 升级 cron-parser 2025-11-11 00:37:03 +08:00
app.ts Fix race condition preventing scheduled tasks from executing in clustered mode (#2819) 2025-11-12 00:59:22 +08:00
token.ts 修改服务启动逻辑 2025-05-07 09:30:00 +08:00
tsconfig.json 修改服务启动逻辑 2025-05-07 09:30:00 +08:00