tonychen/php-task

该软件包的最新版本(v0.1)没有提供许可信息。

php-task

v0.1 2018-08-05 12:18 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:55:17 UTC


README

uml

安装

使用以下命令安装最新版本

$ composer require tonychen/php-task

Windows下忽略依赖安装

$ composer require tonychen/php-task --ignore-platform-reqs

基本用法

<?php

include 'vendor/autoload.php';

use Tony\Task\Job\Job;
use Tony\Task\Runner;
use Tony\Task\Scheduler;
use Tony\Task\Struct\ProcessConfig;

class Demo extends Job
{
    public function execute(Scheduler $subject): void
    {
        /**@var Scheduler $subject */
        $expression = $subject->getTimer()->getExpression();
        error_log(date('Y-m-d H:i:s') . "\t{$expression}\n", 3, '/tmp/php-task.log');
    }
}

$processConfig          = new ProcessConfig();
$processConfig->pidFile = '/tmp/php-task.pid';
$processConfig->stdErr  = '/dev/null';
$processConfig->stdOut  = '/dev/null';
$processConfig->stdIn   = '/dev/null';

$schedule = new Scheduler();
$schedule->getTimer()->everyMinute();
$schedule->attach(new Demo());

// 5分钟执行一次
$schedule5 = new Scheduler();
$schedule5->getTimer()->everyFiveMinutes();
$schedule5->attach(new Demo());

// 30分钟执行一次
$schedule30 = new Scheduler();
$schedule30->getTimer()->everyThirtyMinutes();
$schedule30->attach(new Demo());

// 自定义事件表达式
// 2个小时执行一次
$schedule2Hours = new Scheduler();
$schedule2Hours->getTimer()->setExpression("* */2 * * *");
$schedule2Hours->attach(new Demo());

$schedules = new SplObjectStorage();
$schedules->attach($schedule);
$schedules->attach($schedule5);
$schedules->attach($schedule30);
$schedules->attach($schedule2Hours);

$runner = new Runner();
$runner->setProcessConfig($processConfig);
$runner->setSchedulers($schedules);

$runner->run();
# 启动进程任务
php example.php start

# 结束进程任务
php example.php stop

# 查看进程任务的在线时间、内存使用状态、进程id
php example.php status

进程状态

文件日志结果(/tmp/php-task.log)

2018-06-17 15:06:05	*/1 * * * *
2018-06-17 15:07:00	*/1 * * * *
2018-06-17 15:08:00	*/1 * * * *
2018-06-17 15:09:00	*/1 * * * *
2018-06-17 15:10:00	*/1 * * * *
2018-06-17 15:10:00	*/5 * * * *
2018-06-17 15:11:00	*/1 * * * *
2018-06-17 15:12:00	*/1 * * * *
2018-06-17 15:13:00	*/1 * * * *
2018-06-17 15:14:00	*/1 * * * *
2018-06-17 15:15:00	*/1 * * * *
2018-06-17 15:15:00	*/5 * * * *
2018-06-17 15:16:00	*/1 * * * *
2018-06-17 15:17:00	*/1 * * * *
2018-06-17 15:18:00	*/1 * * * *
2018-06-17 15:19:00	*/1 * * * *
2018-06-17 15:27:38	*/1 * * * *
2018-06-17 15:28:00	*/1 * * * *
2018-06-17 15:29:00	*/1 * * * *
2018-06-17 15:30:00	*/1 * * * *
2018-06-17 15:30:00	*/5 * * * *
2018-06-17 15:30:00	*/30 * * * *
2018-06-17 15:31:00	*/1 * * * *
2018-06-17 15:32:00	*/1 * * * *

特性

  • 自动识别时区,不同时区的服务器再也不用手动修改时区
  • 可以设置Job的执行优先级。

表达式说明

*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    |
|    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
|    |    |    +---------- month (1 - 12)
|    |    +--------------- day of month (1 - 31)
|    +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)

TODO 列表

  • 完整的项目案例
  • 测试用例
  • 内存探测的稳定性已做改进,使用一段时间后,如果没有新增功能,将发布一个0.x系列的正式版本
  • 未来计划,在功能上做一个1.0版本的规划,发布1.0版本

参考文章