webman crontab任務管理組件(多類型)

dev-lv
版本
2023-09-25
版本更新時間
1687
安裝
19
star
簡介
基于 webman 的動態(tài)設置定時任務管理
重要提示:windows用戶 啟動webman 使用 windows.php
>=1.1版本增加 任務分類 / 運行次數(shù)(是否單次) 數(shù)據(jù)庫字段有調(diào)整 升級請注意
必須開啟依賴注入
>=1.46版本 command任務 支持后臺運行(不阻塞),class 任務采用異步進程處理 (不阻塞)
安裝
composer require yzh52521/webman-task
laravel orm 安裝
composer require yzh52521/webman-task dev-lv
需要依賴
"workerman/crontab":"^1.0",
"guzzlehttp/guzzle": "^7.0"
定時器格式說明:
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位,則最小時間粒度是分鐘]
使用
簡單使用
$param = [
'method' => 'crontabIndex',//計劃任務列表
'args' => ['limit' => 10, 'page' => 1]//參數(shù)
];
$result= yzh52521\Task\Client::instance()->request($param);
return json($result);
任務分類
-
url 任務可以指定一個url地址來請求,沒有什么可解釋的。
-
eval 任務可以直接寫入 php 代碼,但代碼請不要帶 <?php 和 ?>,具體查閱 eval 函數(shù)
-
Class 任務必須指定帶有 命名空間的類名,并且實現(xiàn)一個 public 屬性的方法:execute 方法返回值為 bool / string 類型 (>=1.1.8 版本支持自定義方法) 示例:$target="app\\common\\crontab\\ClearLogCrontab@custom"
-
Command 任務請先按照 webman 官方文檔定義好執(zhí)行命令,在新增任務,輸入定義的 命令 即可 例如: version
-
Shell 任務 在新增任務,輸入定義的 shell命令 即可 例如:ps -ef | grep php
計劃任務列表
方法名
method: crontabIndex
請求參數(shù)
args
參數(shù)名稱 | 是否必須 | 示例 | 備注 |
---|---|---|---|
page | 是 | 1 | 頁碼 |
limit | 是 | 15 | 每頁條數(shù) |
返回數(shù)據(jù)
{
"code": 200,
"msg": "ok",
"data": {
"total": 4,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"data": [
{
"id": 6,
"title": "class任務 每月1號清理所有日志",
"type": 2,
"rule": "0 0 1 * *",
"target": "app\\common\\crontab\\ClearLogCrontab",
"parameter": "",
"running_times": 71,
"last_running_time": 1651121710,
"remark": "",
"sort": 0,
"status": 1,
"create_time": 1651114277,
"update_time": 1651114277,
"singleton": 1
},
{
"id": 5,
"title": "eavl任務 輸出 hello world",
"type": 4,
"rule": "* * * * *",
"target": "echo 'hello world';",
"parameter": "",
"running_times": 25,
"last_running_time": 1651121701,
"remark": "",
"sort": 0,
"status": 1,
"create_time": 1651113561,
"update_time": 1651113561,
"singleton": 0
},
{
"id": 3,
"title": "url任務 打開 workerman 網(wǎng)站",
"type": 3,
"rule": "*/20 * * * * *",
"target": "http://www.wtbis.cn/",
"parameter": "",
"running_times": 39,
"last_running_time": 1651121700,
"remark": "請求workerman網(wǎng)站",
"sort": 0,
"status": 1,
"create_time": 1651112925,
"update_time": 1651112925,
"singleton": 0
},
{
"id": 1,
"title": "command任務 輸出 webman 版本",
"type": 1,
"rule": "*/20 * * * * *",
"target": "php webman version",
"parameter": null,
"running_times": 112,
"last_running_time": 1651121700,
"remark": "20秒",
"sort": 0,
"status": 1,
"create_time": 1651047480,
"update_time": 1651047480,
"singleton": 1
}
]
}
}
計劃任務日志列表
method: crontabLog
請求參數(shù)
args
參數(shù)名稱 | 是否必須 | 示例 | 備注 |
---|---|---|---|
page | 是 | 1 | 頁碼 |
limit | 是 | 15 | 每頁條數(shù) |
crontab_id | 是 | 1 | 計劃任務ID |
返回數(shù)據(jù)
{
"code": 200,
"data": {
"list": [
{
"id": 257,
"crontab_id": 1,
"target": "php webman version",
"parameter": "{}",
"exception": "Webman-framework v1.3.11",
"return_code": 0,
"running_time": "0.834571",
"create_time": 1651123800,
"update_time": 1651123800
},
}
添加任務
method: crontabCreate
請求參數(shù)
args
參數(shù)名稱 | 參數(shù)類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
title | text | 是 | 輸出 webman 版本 | 任務標題 |
type | text | 是 | 1 | 任務類型 (1 command, 2 class, 3 url, 4 eval,5 shell) |
rule | text | 是 | */3 * * * * * | 任務執(zhí)行表達式 |
target | text | 是 | version | 調(diào)用任務字符串 |
parameter | text | 否 | {} | 調(diào)用任務參數(shù)(url和eval無效) |
remark | text | 是 | 每3秒執(zhí)行 | 備注 |
sort | text | 是 | 0 | 排序 |
status | text | 是 | 1 | 狀態(tài)[0禁用; 1啟用] |
singleton | text | 否 | 1 | 是否單次執(zhí)行 [0 是 1 不是] |
測試事例
$param = [
'method' => 'crontabCreate',
'args' => [
'title' => '輸出 webman 版本',
'type' =>1,
'rule' => '*/30 * * * * *',
'target' => 'version',
'status' => 1,
'remark' => '每30秒執(zhí)行',
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
return json($result);
返回數(shù)據(jù)
{
"code": 200,
"data":{
"code":true
},
"msg": "ok!"
}
重啟任務
method: crontabReload
請求參數(shù)
args
參數(shù)名稱 | 參數(shù)類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
id | text | 是 | 1,2 | 計劃任務ID 多個逗號隔開 |
測試事例
$param = [
'method' => 'crontabReload',
'args' => [
'id' =>1,2
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
return json($result);
返回數(shù)據(jù)
{
"code": 200,
"data":{
"code":true
},
"msg": "ok!"
}
修改任務
method: crontabUpdate
請求參數(shù)
args
參數(shù)名稱 | 參數(shù)類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
id | text | 是 | 1 | |
title | text | 否 | 輸出 webman 版本 | 任務標題 |
type | text | 是 | 1 | 任務類型 (1 command, 2 class, 3 url, 4 eval,5 shell) |
rule | text | 否 | */3 * * * * * | 任務執(zhí)行表達式 |
target | text | 否 | version | 任務腳本 |
parameter | text | 否 | {} | 調(diào)用任務參數(shù)(url和eval無效) |
remark | text | 否 | 每3秒執(zhí)行 | 備注 |
sort | text | 否 | 0 | 排序 |
status | text | 否 | 1 | 狀態(tài)[0禁用; 1啟用] |
singleton | text | 否 | 1 | 是否單次執(zhí)行 [0 是 1 不是] |
測試用例
$request = [
'method' => 'crontabUpdate',
'args' => [
'id' => 1,
'rule' => '*/30 * * * * *',
'target' => 'php webman config',
'status' => 1,
'remark' => '30秒',
]
];
$result = \yzh52521\Task\Client::instance()->request($request);
return json($result);
返回數(shù)據(jù)
{
"code": 200,
"data":{
"code":true
},
"msg": "ok!"
}
刪除任務
method: crontabDelete
請求參數(shù)
args
參數(shù)名稱 | 參數(shù)類型 | 是否必須 | 示例 | 備注 |
---|---|---|---|---|
id | text | 是 | 1,2 | 計劃任務ID 多個逗號隔開 |
測試事例
$param = [
'method' => 'crontabDelete',
'args' => [
'id' =>1,2
]
];
$result = \yzh52521\Task\Client::instance()->request($param);
return json($result);
返回數(shù)據(jù)
{
"code": 200,
"data":{
"code":true
},
"msg": "ok!"
}
效果圖(可選)
- 定時任務列表
- 定時任務執(zhí)行記錄
- 定時任務添加