PHP 实现可视化的秒级定时任务

PHP 秒级定时任务

概述

Workerman + Thinkphp6 实现可视化秒级定时任务。兼容 Windows 和 Linux 系统。

其实就可视化页面部分可以用 TP 框架实现 ,当然也可以用其他方式,比如,纯 HTML + CSS + JS。因为定时任务本身,它不依赖任何框架,且提供了定时任务操作的接口

下载地址

GitHub 下载

系统定时任务使用方法

  1. 进入项目根目录

  2. 拷贝 .example.env 文件为 .env,并配置正确的数据库

  3. 执行命令 php think run -p 8888,运行系统,语法参照 thinkphp6 手册

  4. 执行命令 php crontab.php (windows)php crontab.php start (linux) 启动定时器

  5. 访问后台 http://127.0.0.1:8888/admin

定时器格式说明:

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 非必须      

 

分情破爱始乱弃,流落天涯思别离。 如花似玉负情意,影如白昼暗自迷。 随风浮沉千叶落,行色匆匆鬓已稀。
原文地址:https://www.cnblogs.com/cshaptx4869/p/14679615.html