1 Star 0 Fork 51

nhwlzx / cron-manager

forked from 一个作词家 / cron-manager 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

cronManager

简介

cronManager是一个纯PHP实现的定时任务管理工具,api简单清晰,采用的是多进程模型,进程通信采用的是消息队列,任务监控也提供了简单的命令,方便易用

特性

  • 多进程模型

  • 支持守护进程

  • 平滑重启

  • 提供各种命令监控任务运行状态

  • 支持一个任务由多个进程同时,以不同的标识运行一个任务

环境要求

  1. liunx
  2. pcntl扩展开启
  3. php 5.4以上
  4. composer
  • 手动安装记得要先 composer update一下哦!

使用介绍

核心方法 CronManager::taskInterval($name, $command, $callable, $ticks = [])

参数1 $name 定时任务名称

参数2 $command

传入string则表示用key@value的形式表示

  1. s@n 表示每n秒运行一次
  2. i@n 表示每n分钟运行一次
  3. h@n 表示每n小时运行一次
  4. at@nn:nn 表示指定每天的nn:nn执行 例如每天凌晨 at@00:00

传入array则表示依次运行数组里的每一个指定日期,要求每一个元素都可以被strtotime函数解析,否则运行不了 如: ['2017-09-09 08:00','2017-09-09 08:00']

参数3 $callable 回调函数,也就是定时任务业务逻辑

参数4 $ticks 用于单任务多进程时标识

快速入门示例


//test.php

require __DIR__ . '/../vendor/autoload.php';

$manager = new SuperCronManager\CronManager();

// 设置worker数
$manager->workerNum = 5;

// 设置输出重定向,守护进程模式才生效
$manager->output = './test.log';

$manager->taskInterval('每秒钟运行一次', 's@1', function(){
	echo "Hello crontabManager\n";
});
$manager->taskInterval('每分钟运行一次', 'i@1', function(){
	echo "Hello crontabManager\n";
});
$manager->taskInterval('每小时运行一次', 'h@1', function(){
	echo "Hello crontabManager\n";
});
$manager->taskInterval('每天凌晨运行一次', 'at@00:00', function(){
	echo "Hello crontabManager\n";
});
$manager->taskInterval('任务分片', 's@1', function($str){
	echo "$str\n";
},[1,2]);

$manager->taskInterval('分片测试', ['2017-12-20 23:28','2017-12-20 23:30'], function($index){
	echo "ticks $index\n";
});

$manager->run();

命令使用示例

  • 检测扩展是否启动,缺少扩展将无法使用。 建议第一次运行先使用此命令查看扩展情况

php test.php check

+----------+--------+------+------+
| name     | status | desc | help |
+----------+--------+------+------+
| php>=5.4 | [OK]   |      |      |
| pcntl    | [OK]   |      |      |
| posix    | [OK]   |      |      |
| sysvmsg  | [OK]   |      |      |
| sysvsem  | [OK]   |      |      |
| sysvshm  | [OK]   |      |      |
+----------+--------+------+------+
  • 启动

php test.php

+------------+---------------------+
| pid        | 19629               |
+------------+---------------------+
| output     | /dev/null           |
+------------+---------------------+
| task_num   | 7                   |
+------------+---------------------+
| worker_num | 5                   |
+------------+---------------------+
| start_time | 2017-12-09 13:59:15 |
+------------+---------------------+
  • 以守护进程方式启动(无任何提示表示成功

php test.php -d

  • 查看任务状态

php test.php status

+------------+---------------------+
| pid        |19690                |
+------------+---------------------+
| output     | ./test.log          |
+------------+---------------------+
| task_num   | 4                   |
+------------+---------------------+
| worker_num | 5                   |
+------------+---------------------+
| start_time | 2017-12-09 14:03:44 |
+------------+---------------------+
+----+------------------+------------------+--------+-------+---------------------+---------------------+
| id | name             | tag              | status | count | last_time           | next_time           |
+----+------------------+------------------+--------+-------+---------------------+---------------------+
| 0  | 每秒钟运行一次   | s@1              | 正常   | 3     | 2017-12-20 23:26:42 | 2017-12-20 23:26:43 |
| 1  | 每分钟运行一次   | i@1              | 正常   | 0     | -                   | 2017-12-20 23:27:39 |
| 2  | 每小时运行一次   | h@1              | 正常   | 0     | -                   | 2017-12-21 00:26:39 |
| 3  | 每天凌晨运行一次 | at@00:00         | 正常   | 0     | -                   | 2017-12-21 00:00:00 |
| 4  | 任务分片         | s@1              | 正常   | 3     | 2017-12-20 23:26:42 | 2017-12-20 23:26:43 |
| 5  | 任务分片         | s@1              | 正常   | 3     | 2017-12-20 23:26:42 | 2017-12-20 23:26:43 |
| 6  | 分片测试         | 2017-12-20 23:28 | 正常   | 0     | -                   | 2017-12-20 23:28:00 |
+----+------------------+------------------+--------+-------+---------------------+---------------------+
  • 查看worker状态

php test.php worker


+------------+---------------------+
| pid        | 19690               |
+------------+---------------------+
| output     | ./test.log          |
+------------+---------------------+
| task_num   | 4                   |
+------------+---------------------+
| worker_num | 5                   |
+------------+---------------------+
| start_time | 2017-12-09 14:03:44 |
+------------+---------------------+
+-------+------------+----------+---------------------+
| pid   | exec_count | memory   | start_time          |
+-------+------------+----------+---------------------+
| 19691 | 16         | 0.57(mb) | 2017-12-09 14:03:44 |
| 19692 | 19         | 0.57(mb) | 2017-12-09 14:03:44 |
| 19693 | 12         | 0.57(mb) | 2017-12-09 14:03:44 |
| 19694 | 12         | 0.57(mb) | 2017-12-09 14:03:44 |
| 19695 | 5          | 0.57(mb) | 2017-12-09 14:03:44 |
+-------+------------+----------+---------------------+
  • 查看服务日志(这个有点LOW,就是直接读日志文件输出)

php test.php log

2017-12-09 14:03:44 PID:19690 [debug] master启动
The MIT License (MIT) Copyright (c) 2017 一个作词家 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个纯PHP实现的多进程,定时任务管理工具,支持守护进程 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/hboo-trip/cron-manager.git
git@gitee.com:hboo-trip/cron-manager.git
hboo-trip
cron-manager
cron-manager
master

搜索帮助