cshaptx4869/http-crontab

接口化定时任务管理

v2.0.1 2023-05-07 04:03 UTC

This package is auto-updated.

Last update: 2024-04-07 06:13:24 UTC


README

概述

基于 Workerman + MySQL 的接口化秒级定时任务管理,兼容 Windows 和 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位,则最小时间粒度是分钟]

简单使用

  • 新建 run.php
<?php

require_once "./vendor/autoload.php";

use Fairy\HttpCrontab;

date_default_timezone_set('PRC');

//数据库配置
//启动脚本后会自行创建所需的数据表
//定时器任务执行日志按月自动分表
$dbConfig = [
    'hostname' => '127.0.0.1',
    'hostport' => '3306',
    'username' => 'root',
    'password' => 'root',
    'database' => 'test',
    'charset' => 'utf8mb4'
];

//启动后默认监听 http://127.0.0.1:2345 
//可在new的时候传递第一个参数改变监听地址
(new HttpCrontab())->setDebug(true)
    ->setName('System Crontab')
    ->setDbConfig($dbConfig)
    ->run();
  • 启动服务

集成项目

easyadmin 是基于 ThinkPHP6.1 和 Layui2.7 的快速开发的后台管理系统。

  • 启动服务

$ php think crontab -h
Usage:
  crontab [options] [--] <action>

Arguments:
  action                start|stop|restart|reload|status|connections

Options:
  -d, --daemon          Run the http crontab server in daemon mode.
      --name[=NAME]     Crontab name [default: "Crontab Server"]
      --debug           Print log
  -h, --help            Display this help message
  -V, --version         Display this console version
  -q, --quiet           Do not output any message
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  • UI操作界面

定时器接口说明

PING

基本信息

Path: /crontab/ping

Method: GET

接口描述:

{
  "code": 200,
  "data": "pong",
  "msg": "信息调用成功!"
}

请求参数

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
datastring非必须
msgstring非必须

修改

基本信息

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

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
databoolean非必须
msgstring非必须

列表

基本信息

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":"%*%"} 检索字段操作

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
dataobject非必须
├─ listobject []非必须

item 类型: object

├─ idnumber非必须
├─ titlestring非必须
├─ typenumber非必须
├─ frequencystring非必须
├─ shellstring非必须
├─ running_timesnumber非必须
├─ last_running_timenumber非必须
├─ remarkstring非必须
├─ sortnumber非必须
├─ statusnumber非必须
├─ create_timenumber非必须
├─ update_timenumber非必须
├─ countnumber非必须
msgstring非必须

删除

基本信息

Path: /crontab/delete

Method: POST

接口描述:

{
  "code": 200,
  "data": true,
  "msg": "信息调用成功!"
}

请求参数

Headers

参数名称 参数值 是否必须 示例 备注
Content-Type application/x-www-form-urlencoded

Body

参数名称 参数类型 是否必须 示例 备注
id text 1,2

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
databoolean非必须
msgstring非必须

定时器池

基本信息

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": "信息调用成功!"
}

请求参数

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
dataobject []非必须

item 类型: object

├─ idnumber非必须
├─ shellstring非必须
├─ frequencystring非必须
├─ remarkstring非必须
├─ create_timestring非必须
msgstring非必须

日志

基本信息

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":"="} 检索字段操作

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
dataobject非必须
├─ listobject []非必须

item 类型: object

├─ idnumber必须
├─ sidnumber必须
├─ commandstring必须
├─ outputstring必须
├─ return_varnumber必须
├─ running_timestring必须
├─ create_timenumber必须
├─ update_timenumber必须
├─ countnumber非必须
msgstring非必须

添加

基本信息

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启用]

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
databoolean非必须
msgstring非必须

重启

基本信息

Path: /crontab/reload

Method: POST

接口描述:

{
  "code": 200,
  "data": true,
  "msg": "信息调用成功!"
}

请求参数

Headers

参数名称 参数值 是否必须 示例 备注
Content-Type application/x-www-form-urlencoded

Body

参数名称 参数类型 是否必须 示例 备注
id text 1,2

返回数据

名称类型是否必须默认值备注其他信息
codenumber非必须
databoolean非必须
msgstring非必须