PHP 秒級定時任務
概述
Workerman + Thinkphp6 實現可視化秒級定時任務。兼容 Windows 和 Linux 系統。
其實就可視化頁面部分可以用 TP 框架實現 ,當然也可以用其他方式,比如,純 HTML + CSS + JS。因為定時任務本身,它不依賴任何框架,且提供了定時任務操作的接口。
下載地址
GitHub 下載
系統定時任務使用方法
-
-
拷貝
.example.env
文件為.env
,並配置正確的數據庫 -
執行命令
php think run -p 8888
,運行系統,語法參照 thinkphp6 手冊 -
執行命令
php crontab.php
(windows) 或php crontab.php start
(linux) 啟動定時器 -
訪問后台
定時器格式說明:
0 1 2 3 4 5
| | | | | |
| | | | | +------ day of week (0 - 6) (Sunday=0)
| | | | +------ month (1 - 12)
| | | +-------- day of month (1 - 31)
| | +---------- hour (0 - 23)
| +------------ min (0 - 59)
+-------------- sec (0-59)[可省略,如果沒有0位,則最小時間粒度是分鍾]
效果展示
定時器操作 API
PING
基本信息
Path: /crontab/ping
Method: GET
接口描述:
{
"code": 200,
"data": "pong",
"msg": "信息調用成功!"
}
請求參數
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | string | 非必須 | |||
msg | string | 非必須 |
修改
基本信息
Path: /crontab/modify
Method: POST
接口描述:
{
"code": 200,
"data": true,
"msg": "信息調用成功!"
}
請求參數
Headers
參數名稱 | 參數值 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
Content-Type | application/x-www-form-urlencoded | 是 |
Body
參數名稱 | 參數類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
id | text | 是 | 1 | |
field | text | 是 | status | 字段[status; sort; remark; title] |
value | text | 是 | 1 | 值 |
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | boolean | 非必須 | |||
msg | string | 非必須 |
列表
基本信息
Path: /crontab/index
Method: GET
接口描述:
{
"code": 200,
"data": {
"list": [
{
"id": 1,
"title": "輸出 tp 版本",
"type": 0,
"frequency": "*/3 * * * * *",
"shell": "php think version",
"running_times": 3,
"last_running_time": 1625636646,
"remark": "沒3秒執行",
"sort": 0,
"status": 1,
"create_time": 1625636609,
"update_time": 1625636609
}
],
"count": 1
},
"msg": "信息調用成功!"
}
請求參數
Query
參數名稱 | 是否必須 | 示例 | 備注 |
---|---|---|---|
page | 是 | 1 | 頁碼 |
limit | 是 | 15 | 每頁條數 |
filter | 否 | {"title":"輸出 tp 版本"} | 檢索字段值 |
op | 否 | {"title":"%*%"} | 檢索字段操作 |
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | object | 非必須 | |||
├─ list | object [] | 非必須 | item 類型: object |
||
├─ id | number | 非必須 | |||
├─ title | string | 非必須 | |||
├─ type | number | 非必須 | |||
├─ frequency | string | 非必須 | |||
├─ shell | string | 非必須 | |||
├─ running_times | number | 非必須 | |||
├─ last_running_time | number | 非必須 | |||
├─ remark | string | 非必須 | |||
├─ sort | number | 非必須 | |||
├─ status | number | 非必須 | |||
├─ create_time | number | 非必須 | |||
├─ update_time | number | 非必須 | |||
├─ count | number | 非必須 | |||
msg | string | 非必須 |
刪除
基本信息
Path: /crontab/delete
Method: POST
接口描述:
{
"code": 200,
"data": true,
"msg": "信息調用成功!"
}
請求參數
Headers
參數名稱 | 參數值 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
Content-Type | application/x-www-form-urlencoded | 是 |
Body
參數名稱 | 參數類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
id | text | 是 | 1,2 |
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | boolean | 非必須 | |||
msg | string | 非必須 |
定時器池
基本信息
Path: /crontab/pool
Method: GET
接口描述:
{
"code": 200,
"data": [
{
"id": 1,
"shell": "php think version",
"frequency": "*/3 * * * * *",
"remark": "沒3秒執行",
"create_time": "2021-07-07 13:43:29"
}
],
"msg": "信息調用成功!"
}
請求參數
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | object [] | 非必須 | item 類型: object |
||
├─ id | number | 非必須 | |||
├─ shell | string | 非必須 | |||
├─ frequency | string | 非必須 | |||
├─ remark | string | 非必須 | |||
├─ create_time | string | 非必須 | |||
msg | string | 非必須 |
日志
基本信息
Path: /crontab/flow
Method: GET
接口描述:
{
"code": 200,
"data": {
"list": [
{
"id": 12,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.115895",
"create_time": 1625636673,
"update_time": 1625636673
},
{
"id": 11,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.104641",
"create_time": 1625636670,
"update_time": 1625636670
},
{
"id": 10,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.106585",
"create_time": 1625636667,
"update_time": 1625636667
},
{
"id": 9,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.10808",
"create_time": 1625636664,
"update_time": 1625636664
},
{
"id": 8,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.107653",
"create_time": 1625636661,
"update_time": 1625636661
},
{
"id": 7,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.105938",
"create_time": 1625636658,
"update_time": 1625636658
},
{
"id": 6,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.10461",
"create_time": 1625636655,
"update_time": 1625636655
},
{
"id": 5,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.109786",
"create_time": 1625636652,
"update_time": 1625636652
},
{
"id": 4,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.115853",
"create_time": 1625636649,
"update_time": 1625636649
},
{
"id": 3,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.16941",
"create_time": 1625636646,
"update_time": 1625636646
},
{
"id": 2,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.109524",
"create_time": 1625636643,
"update_time": 1625636643
},
{
"id": 1,
"sid": 1,
"command": "php think version",
"output": "v6.0.7",
"return_var": 0,
"running_time": "0.108445",
"create_time": 1625636640,
"update_time": 1625636640
}
],
"count": 12
},
"msg": "信息調用成功!"
}
請求參數
Query
參數名稱 | 是否必須 | 示例 | 備注 |
---|---|---|---|
page | 是 | 1 | 頁碼 |
limit | 是 | 15 | 每頁條數 |
filter | 否 | {"sid":"1"} | 檢索字段值 |
op | 否 | {"sid":"="} | 檢索字段操作 |
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | object | 非必須 | |||
├─ list | object [] | 非必須 | item 類型: object |
||
├─ id | number | 必須 | |||
├─ sid | number | 必須 | |||
├─ command | string | 必須 | |||
├─ output | string | 必須 | |||
├─ return_var | number | 必須 | |||
├─ running_time | string | 必須 | |||
├─ create_time | number | 必須 | |||
├─ update_time | number | 必須 | |||
├─ count | number | 非必須 | |||
msg | string | 非必須 |
添加
基本信息
Path: /crontab/add
Method: POST
接口描述:
{
"code": 200,
"data": true,
"msg": "信息調用成功!"
}
請求參數
Headers
參數名稱 | 參數值 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
Content-Type | application/x-www-form-urlencoded | 是 |
Body
參數名稱 | 參數類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
title | text | 是 | 輸出 tp 版本 | 任務標題 |
type | text | 是 | 0 | 任務類型[0請求url; 1執行sql; 2執行shell] |
frequency | text | 是 | */3 * * * * * | 任務頻率 |
shell | text | 是 | php think version | 任務腳本 |
remark | text | 是 | 沒3秒執行 | 備注 |
sort | text | 是 | 0 | 排序 |
status | text | 是 | 1 | 狀態[0禁用; 1啟用] |
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | boolean | 非必須 | |||
msg | string | 非必須 |
重啟
基本信息
Path: /crontab/reload
Method: POST
接口描述:
{
"code": 200,
"data": true,
"msg": "信息調用成功!"
}
請求參數
Headers
參數名稱 | 參數值 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
Content-Type | application/x-www-form-urlencoded | 是 |
Body
參數名稱 | 參數類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
id | text | 是 | 1,2 |
返回數據
名稱 | 類型 | 是否必須 | 默認值 | 備注 | 其他信息 |
---|---|---|---|---|---|
code | number | 非必須 | |||
data | boolean | 非必須 | |||
msg | string | 非必須 |