webman crontab任务管理组件(多类型)

v1.48 版本
2023-09-25 版本更新时间
1238 安装
15 star

简介

基于 webman 的动态设置定时任务管理

重要提示:windows用户 启动webman 使用 windows.php
>=1.1版本增加 任务分类 / 运行次数(是否单次) 数据库字段有调整 升级请注意
必须开启依赖注入
>=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]//参数
    ];
   $result= yzh52521\Task\Client::instance()->request($param);
   return json($result);

任务分类

  • url 任务可以指定一个url地址来请求,没有什么可解释的。

  • eval 任务可以直接写入 php 代码,但代码请不要带 <?php 和 ?>,具体查阅 eval 函数

  • Class 任务必须指定带有 命名空间的类名,并且实现一个 public 属性的方法:execute 方法返回值为 bool / string 类型 (>=1.1.8 版本支持自定义方法) 示例:$target="app\\common\\crontab\\ClearLogCrontab@custom"

  • Command 任务请先按照 webman 官方文档定义好执行命令,在新增任务,输入定义的 命令 即可 例如: version

  • Shell 任务 在新增任务,输入定义的 shell命令 即可 例如:ps -ef | grep php

计划任务列表

方法名

method: crontabIndex

请求参数

args

参数名称 是否必须 示例 备注
page 1 页码
limit 15 每页条数

返回数据

{
"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 网站",
"type": 3,
"rule": "*/20 * * * * *",
"target": "https://www.workerman.net/",
"parameter": "",
"running_times": 39,
"last_running_time": 1651121700,
"remark": "请求workerman网站",
"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

请求参数

args

参数名称 是否必须 示例 备注
page 1 页码
limit 15 每页条数
crontab_id 1 计划任务ID

返回数据

{
"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

请求参数

args

参数名称 参数类型 是否必须 示例 备注
title text 输出 webman 版本 任务标题
type text 1 任务类型 (1 command, 2 class, 3 url, 4 eval,5 shell)
rule text */3 * * * * * 任务执行表达式
target text version 调用任务字符串
parameter text {} 调用任务参数(url和eval无效)
remark text 每3秒执行 备注
sort text 0 排序
status text 1 状态[0禁用; 1启用]
singleton text 1 是否单次执行 [0 是 1 不是]

测试事例

     $param = [
            'method' => 'crontabCreate',
            'args'   => [
                'title'     => '输出 webman 版本',
                'type' =>1,
                'rule' => '*/30 * * * * *',
                'target'     => 'version',
                'status'    => 1,
                'remark'    => '每30秒执行',
            ]
        ];
        $result  = \yzh52521\Task\Client::instance()->request($param);
        return json($result);

返回数据

{
 "code": 200,
 "data":{
        "code":true
 },
 "msg": "ok!"
}

重启任务

method: crontabReload

请求参数

args

参数名称 参数类型 是否必须 示例 备注
id text 1,2 计划任务ID 多个逗号隔开

测试事例

     $param = [
            'method' => 'crontabReload',
            'args'   => [
                'id'     =>1,2
            ]
        ];
        $result  = \yzh52521\Task\Client::instance()->request($param);
        return json($result);

返回数据

{
 "code": 200,
 "data":{
        "code":true
 },
 "msg": "ok!"
}

修改任务

method: crontabUpdate

请求参数

args

参数名称 参数类型 是否必须 示例 备注
id text 1
title text 输出 webman 版本 任务标题
type text 1 任务类型 (1 command, 2 class, 3 url, 4 eval,5 shell)
rule text */3 * * * * * 任务执行表达式
target text version 任务脚本
parameter text {} 调用任务参数(url和eval无效)
remark text 每3秒执行 备注
sort text 0 排序
status text 1 状态[0禁用; 1启用]
singleton text 1 是否单次执行 [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);

返回数据

{
 "code": 200,
 "data":{
        "code":true
 },
 "msg": "ok!"
}

删除任务

method: crontabDelete

请求参数

args

参数名称 参数类型 是否必须 示例 备注
id text 1,2 计划任务ID 多个逗号隔开

测试事例

     $param = [
            'method' => 'crontabDelete',
            'args'   => [
                'id'     =>1,2
            ]
        ];
        $result  = \yzh52521\Task\Client::instance()->request($param);
        return json($result);

返回数据

{
 "code": 200,
 "data":{
        "code":true
 },
 "msg": "ok!"
}

效果图(可选)

  • 定時任务列表

  • 定时任务执行记录

  • 定时任务添加