refactor(timer): 使用 CurlMultiHandler 替代 Workerman,统一为非阻塞模式

- 删除 runParallel() 方法和所有 Workerman 引用(死代码)
- 重写 runLoop() 为 Guzzle CurlMultiHandler 非阻塞事件循环
- 新增 pending 数组追踪进行中的请求,handler.tick() 非阻塞推进
- 自适应 sleep 策略(有任务 50ms,空闲 200ms)
- 简化 config/timer.php:移除 mode,适配 Guzzle 参数
- 更新 SKILL.md:移除 parallel 描述,修正 --quit 文档 bug
- 验证发现:--quiet 是 ThinkPHP 全局选项,不需要在 configure() 注册
- 验证发现:方法名不能用 run(),与 ThinkPHP Command::run() 签名冲突
This commit is contained in:
augushong
2026-06-02 21:19:53 +08:00
parent c4fbd60bbc
commit 76b23d4c70
3 changed files with 81 additions and 144 deletions

View File

@@ -1,6 +1,6 @@
---
---
name: "ulthon-timer"
description: "内置秒级定时器php think timer的使用与扩展规范用于新增/调整定时任务site/call、并发分片、TimerController 防刷、timer.mode normal/parallel)。"
description: "内置秒级定时器php think timer的使用与扩展规范用于新增/调整定时任务site/call、并发分片、TimerController 防刷)。"
---
# timer内置秒级定时器
@@ -21,7 +21,7 @@ description: "内置秒级定时器php think timer的使用与扩展规范
- [Timer.php](../../../app/common/command/Timer.php) / [TimerBase.php](../../../extend/base/common/command/TimerBase.php)
- [TimerService.php](../../../app/common/service/TimerService.php) / [TimerServiceBase.php](../../../extend/base/common/service/TimerServiceBase.php)
- [TimerController.php](../../../app/common/controller/TimerController.php) / [TimerControllerBase.php](../../../extend/base/common/controller/TimerControllerBase.php)
- 运行模式配置:[timer.php](../../../config/timer.php)
- 运行配置:[timer.php](../../../config/timer.php)
## 新增定时任务(默认规则)
@@ -182,7 +182,7 @@ return [
- 常规运行:`php think timer`
- 只跑一轮(便于验证):`php think timer --temp`
- 无任务时不输出“no request”`php think timer --quit`
- 无任务时不输出“no request”`php think timer --quiet`
### 本地调试(指定请求 Host
@@ -192,20 +192,21 @@ site 任务会按站点域名发起请求,默认从 `sysconfig('site','site_do
### 运行模式
配置在 [timer.php](../../../config/timer.php)
配置在 [timer.php](../../../config/timer.php)。定时器使用 Guzzle CurlMultiHandler 实现非阻塞异步事件循环
- `normal`:单进程循环 + Guzzle async默认
- `parallel`Workerman 多进程模式(并发更高,相关连接参数在 `timer.php` 中)
- `site` 类型任务通过 curl multi 并行发送 HTTP 请求,真正非阻塞
- `call` 类型任务在主循环中同步执行
- `pending` 数组追踪进行中的请求,`handler->tick()` 非阻塞推进
- 自适应 sleep 策略50ms/200ms避免 CPU 空转
### 配置项说明
| 配置键 | 默认值 | 说明 |
|--------|--------|------|
| `mode` | `normal` | 运行模式(`normal` / `parallel` |
| `max_conn_per_addr` | `128` | 每个域名最多维持的并发连接数(仅多进程模式生效) |
| `keepalive_timeout` | `86400` | 连接不通讯自动关闭时间(秒) |
| `connect_timeout` | `86400` | 连接超时时间(秒) |
| `connect_timeout` | `30` | 连接超时时间(秒 |
| `timeout` | `86400` | 请求响应超时时间(秒) |
| `max_handles` | `100` | curl multi 最大并发句柄数 |
| `select_timeout` | `0.001` | curl_multi_select 超时(秒) |
| `clear_log_days` | `3` | ClearLog 任务清理 debug_log 表的保留天数,支持从 `.env``TIMER_CLEAR_LOG_DAYS` 覆盖 |
## 常见坑位(快速自检)