t4web/cron

ZF2 模块。为 ZF2 应用程序提供的 PHP 定时任务管理器

1.0.0 2016-03-11 08:13 UTC

This package is auto-updated.

Last update: 2024-08-26 17:21:29 UTC


README

Build Status codecov.io Scrutinizer Code Quality

Cron

ZF2 模块。为 ZF2 应用程序提供的 PHP 定时任务管理器。灵感来源于 Cron/Cron

安装

Cron 模块的安装使用 composer。

php composer.phar require t4web/cron

或者添加到您的 composer.json 文件中

"require": {
  "t4web/cron": "^1.0.0"
}

然后添加 T4web\Cron 到您的 config/application.config.php 文件

配置

添加到您的配置中

'cron' => [
    'log-directory' => 'data', // default: getcwd()/data
    'phpPath'    => 'php', // default: php
    'scriptPath' => '/path/to/application/public/folder/', // default: getcwd()/public/
    'jobs'       => [
        [
            'id' => 'cron-job1',
            'command'  => 'index.php application cron-job1',
            'schedule' => '* * * * *'
        ],
        [
            'id' => 'cron-job2',
            'command'  => 'index.php application cron-job2',
            'schedule' => '* * * * *'
        ]
    ],
    'timeout' => 3600 // default: null (without timeout)
]

常规选项

定时任务选项

从命令行运行定时任务

我们建议将此命令添加到 cron 中,每分钟运行一次 (* * * * *)

php index.php cron run

日志

日志与 T4web\EventSubscriber 模块(此模块提供一个地方来管理所有应用程序事件)一起工作。在您的应用程序配置中安装并启用 T4web\EventSubscriber。如果模块 T4web\EventSubscriber 不存在,则不会记录日志。

运行后,在 log-directory 中将创建一个名为 JOB-ID.log 的日志文件(例如:data/cron-job1.log),内容如下

[2016-02-25 15:43:48] Job fail
  Start: 2016-02-25 15:43:47
  End: 2016-02-25 15:43:48
  Execution time: 1 seconds
--Output: 
1
2
3

--End output.
--Error: 
PHP Fatal error:  Call to undefined function Application\Controller\asd() in /storage/proj/1season/module/Application/src/Application/Controller/CronController.php on line 21
PHP Stack trace:
PHP   1. {main}() /storage/proj/1season/public/index.php:0
PHP   2. Zend\Mvc\Application->run() /storage/proj/1season/public/index.php:21
PHP   3. Zend\EventManager\EventManager->trigger() /storage/proj/1season/vendor/zendframework/zend-mvc/src/Application.php:314
PHP   4. Zend\EventManager\EventManager->triggerListeners() /storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:214
PHP   5. call_user_func:{/storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:490}() /storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:490
PHP   6. Zend\Mvc\DispatchListener->onDispatch() /storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:490
PHP   7. Zend\Mvc\Controller\AbstractController->dispatch() /storage/proj/1season/vendor/zendframework/zend-mvc/src/DispatchListener.php:93
PHP   8. Zend\EventManager\EventManager->trigger() /storage/proj/1season/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php:118
PHP   9. Zend\EventManager\EventManager->triggerListeners() /storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:214
PHP  10. call_user_func:{/storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:490}() /storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:490
PHP  11. Zend\Mvc\Controller\AbstractActionController->onDispatch() /storage/proj/1season/vendor/zendframework/zend-eventmanager/src/EventManager.php:490
PHP  12. Application\Controller\CronController->job1Action() /storage/proj/1season/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php:82

--End error.