PHPackages                             chep6915/hyperf-scheduled-task - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. chep6915/hyperf-scheduled-task

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

chep6915/hyperf-scheduled-task
==============================

Hyperf 定時任務基礎表結構

017↓78.3%PHP

Since Jul 6Pushed 1mo agoCompare

[ Source](https://github.com/chep6915/hyperf-scheduled-task)[ Packagist](https://packagist.org/packages/chep6915/hyperf-scheduled-task)[ RSS](/packages/chep6915-hyperf-scheduled-task/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Hyperf Scheduled Task
=====================

[](#hyperf-scheduled-task)

 [![PHP Version](https://camo.githubusercontent.com/45d36955804bf3f4f17097b05a7f41a28e578dc24e0d3ad0d21ae9d9762f44c6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312b2d626c75652e737667)](https://camo.githubusercontent.com/45d36955804bf3f4f17097b05a7f41a28e578dc24e0d3ad0d21ae9d9762f44c6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312b2d626c75652e737667) [![Hyperf Version](https://camo.githubusercontent.com/f56b7b66a2d24a466a484701d0a7ea89718a907eb314c49214c302e625c5aa05/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4879706572662d332e302b2d627269676874677265656e2e737667)](https://camo.githubusercontent.com/f56b7b66a2d24a466a484701d0a7ea89718a907eb314c49214c302e625c5aa05/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4879706572662d332e302b2d627269676874677265656e2e737667) [![License](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)

一個為 Hyperf 框架設計的**高性能定時任務排程系統**，採用 Producer-Consumer 架構，支援秒級精度的任務調度。

📋 目錄
----

[](#-目錄)

- [功能特色](#%E5%8A%9F%E8%83%BD%E7%89%B9%E8%89%B2)
- [系統需求](#%E7%B3%BB%E7%B5%B1%E9%9C%80%E6%B1%82)
- [安裝方式](#%E5%AE%89%E8%A3%9D%E6%96%B9%E5%BC%8F)
- [快速開始](#%E5%BF%AB%E9%80%9F%E9%96%8B%E5%A7%8B)
- [使用指南](#%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97)
- [進階配置](#%E9%80%B2%E9%9A%8E%E9%85%8D%E7%BD%AE)
- [架構說明](#%E6%9E%B6%E6%A7%8B%E8%AA%AA%E6%98%8E)
- [資料表結構](#%E8%B3%87%E6%96%99%E8%A1%A8%E7%B5%90%E6%A7%8B)
- [常見問題](#%E5%B8%B8%E8%A6%8B%E5%95%8F%E9%A1%8C)
- [注意事項](#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A0%85)

✨ 功能特色
------

[](#-功能特色)

- ✅ **Producer-Consumer 架構**：自動預產生未來 4 小時的待執行任務，提升執行效率
- ✅ **秒級精度排程**：支援 6 欄位 Cron 表達式（含秒），最小粒度 1 秒
- ✅ **完整狀態管理**：Pending → Running → Success/Failed 完整生命週期追蹤
- ✅ **樂觀鎖機制**：防止多進程環境下任務重複執行
- ✅ **執行時間記錄**：自動記錄任務開始、結束時間及執行耗時
- ✅ **結果日誌保存**：完整保存任務執行結果或錯誤訊息
- ✅ **手動產生命令**：支援指定時間手動產生任務紀錄，方便測試和補單
- ✅ **依賴注入支援**：任務類別支援建構函數注入，整合 Hyperf 容器
- ✅ **防重複設計**：唯一索引確保同一任務在同一秒不會重複產生
- ✅ **異常處理機制**：完善的錯誤捕獲和狀態更新

📦 系統需求
------

[](#-系統需求)

- PHP &gt;= 8.1
- Hyperf &gt;= 3.0
- MySQL &gt;= 5.7 或 MariaDB &gt;= 10.2
- Swoole &gt;= 5.0

核心組件
----

[](#核心組件)

組件類型說明`scheduled_tasks`資料表排程任務設定表，儲存任務類別和 Cron 表達式`task_execution_logs`資料表任務執行記錄表，追蹤每次執行狀態`ScheduledTaskProducerProcess`常駐進程每 30 秒預產生未來 4 小時的 Pending 任務`ScheduledTaskConsumerProcess`常駐進程每秒掃描並執行到期的 Pending 任務`GeneratePendingRecordsCommand`CLI 命令手動產生任務紀錄，支援指定時間`TaskExecutionStatus`Enum任務狀態枚舉類別---

安裝方式
----

[](#安裝方式)

### 1. 安裝套件

[](#1-安裝套件)

```
composer require chep6915/hyperf-scheduled-task

## ⚙️ 進階配置

### 並發數量調整

預設並發數為 **10 個協程**，可以根據需求調整。

### 資料庫連接池配置

如果出現 `Connection pool exhausted` 錯誤，需要調整連接池大小。

編輯 `config/autoload/databases.php`：

```php
'pool' => [
    'min_connections' => 5,
    'max_connections' => 50,  // 確保大於並發數
    'wait_timeout' => 3.0,
],
```

**重要**：`max_connections` 應該大於 Consumer 的並發數。

詳細配置請參考：[DATABASE\_POOL\_CONFIG.md](DATABASE_POOL_CONFIG.md)

---

🚨 常見問題
------

[](#-常見問題)

### Q1: 出現 "Connection pool exhausted" 錯誤

[](#q1-出現-connection-pool-exhausted-錯誤)

**解決方案**：

1. 增加連接池大小（推薦）
2. 降低 Consumer 並發數到 5-10

### Q2: 如何調整並發數

[](#q2-如何調整並發數)

並發數預設為 10，在使用時建議：

- 輕量任務：10-20 並發
- 中度任務：5-10 並發
- 重度任務：3-5 並發

---

📄 License
---------

[](#-license)

MIT

###  Health Score

21

—

LowBetter than 17% of packages

Maintenance60

Regular maintenance activity

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/2386057e8225dda5aee16019bdd9521f282f021148fc1b33e6d4f892e0293d2e?d=identicon)[chep6915](/maintainers/chep6915)

---

Top Contributors

[![chep6915](https://avatars.githubusercontent.com/u/30277205?v=4)](https://github.com/chep6915 "chep6915 (28 commits)")

### Embed Badge

![Health badge](/badges/chep6915-hyperf-scheduled-task/health.svg)

```
[![Health](https://phpackages.com/badges/chep6915-hyperf-scheduled-task/health.svg)](https://phpackages.com/packages/chep6915-hyperf-scheduled-task)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
