ondrejd/zf2-cron-helper

Zend Framework 2 的模块,简化了在 PHP 项目中处理 CRON 任务。

dev-master 2015-03-03 02:29 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:42:51 UTC


README

Build Status

基于 Zend Framework 2 的模块,简化了在 PHP 项目中处理 CRON 任务。

此模块深受 zf2-cron 模块的影响。最初我打算使用此模块而不是创建自己的,但我不喜欢使用 YAML 和对 Doctrine 的依赖——除此之外,代码看起来也不再维护了...但仍然它是起点和良好的学习资源。

功能

  1. 除了 Zend Framework 2 之外没有其他依赖
  2. 应用程序所有 CRON 任务的入口点
  3. 预配置的 CRON 任务可以在常规时间表之外修改或触发
  4. 直接从代码中简单注册一次性的 CRON 任务
  5. 高级日志功能,带有可选的后台——默认使用 SQLite 数据库,但您可以提供自己的数据库适配器
  6. 使用 EventManager,您可以轻松地添加新的操作
  7. 所有代码都经过良好的文档和测试

安装

CronHelper 专门为纯 Zend Framework 2 应用程序创建,以下描述假设您想将此模块添加到此类应用程序中。

简而言之,您需要做以下这些

  1. Composer 注册新的依赖项
  2. zf2-cron-helper 模块添加到 config/application.config.php 中的模块列表
  3. 创建 config/autoload/cronhelper.config.php 配置文件以设置模块
  4. 如有必要,创建用于记录的数据库表
  5. 到此为止

以下是详细描述

Composer.json 文件

以下是简单应用程序的示例 composer.json 文件

{
  "name": "My application",
  "description": "Application using zf2-cron-helper module",
  "version": "1.0.0",
  "type": "project",
  "keywords": ["commerce","website"],
  "homepage": "http://my.project.com/",
  "license": "MPL-2.0",
  "authors": [{
    "name": "Ondřej Doněk",
    "email": "ondrejd@gmail.com",
    "homepage": "http://ondrejdonek.blogspot.com/",
    "role": "Developer"
  }],
  "repositories": [
    {
      "type": "composer",
      "url": "https://packages.zendframework.com/"
    }, {
      "type": "git",
      "url": "https://github.com/ondrejd/zf2-cron-helper"
    }
  ],
  "require": {
    "php": ">=5.4",
    "zendframework/zendframework": "2.2.*",
    "ondrejd/zf2-cron-helper": "dev-master"
  }
}

应用程序配置

找到您应用程序的主要配置文件(通常是 config/application.config.php)并修改此文件。以下是它的一个非常简单的版本

<?php
return array(
  'modules' => array(
    'Application',
		'CronHelper',
	),
	'module_listener_options' => array(
		'config_glob_paths' => array(
			'config/autoload/{,*.}{global,local}.php',
		),
		'module_paths' => array(
			'.',
			'./vendor',
		),
	),
	'service_manager' => array(
	  'factories' => array(),
	),
);

服务配置

现在您需要配置 CronHelper 自身。首先将预先准备好的配置文件复制到 config/autoload 文件夹

cp vendor/ondrejd/zf2-cron-helper/config/cronhelper.config.php.dist config/autoload/cronhelper.config.php

现在打开它并根据其中的说明进行编辑

<?php
/**
 * zf2-cron-helper
 *
 * @link https://github.com/ondrejd/zf2-cron-helper for the canonical source repository
 * @copyright Copyright (c) 2015 Ondřej Doněk.
 * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License 2.0
 */

return array(
    // Example configuration for the `zf2-cron-helper` module
    'cron_helper' => array(
        // Main options
        'options' => array(
            // Time in minutes for how long ahead CRON jobs have to be scheduled.
            // This means for how long before the scheduled execution time should
            // be job inserted into the database (e.g. scheduled).
            'scheduleAhead' => 1440, // one day before
            // Time in minutes for how long it takes before the scheduled job
            // is considered missed.
            'scheduleLifetime' => 15,
            // Maximal running time (in minutes) for the each CRON job.
            // If `0` is set than the set (in `php.ini`) `max_execution_time` is used.
            'maxRunningTime' => 0,
            // Time in minutes for how long to keep records about successfully
            // completed CRON jobs.
            'successLogLifetime' => 1440, // one day
            // Time in minutes for how long to keep records about failed CRON jobs.
            'failureLogLifetime' => 2880, // two days
            // If `TRUE` then events are emited during processing CRON jobs.
            // This can be useful if you need to perform other actions related
            // to executed CRON jobs.
            'emitEvents' => false,
            // If `TRUE` then you can access info about current status by simple
            // JSON API.
            // This can be useful when you want to provide some sort of UI
            // to watch or manage CRON jobs.
            'allowJsonApi' => false,
            // If JSON API is allowed the security hash MUST BE SET to achive
            // the full functionality. Otherwise will be available only status
            // informations but all managment functions will be disabled.
            'jsonApiSecurityHash' => 'YOUR_SECURITY_HASH',
        ),
        // Optionaly you can define CronHelper own database adapter.
        // If you omit to do that adapter will be searched using
        // ServiceManager by commonly used alias "dbAdapter".
        //'db' => array(
        //	'driver' => 'Pdo_Sqlite',
        //	'database' => 'cronhelper.sqlite'
        //),
        // Here are defined CRON jobs of our application. Keys of these jobs
        // can be used for triggering them directly from the application
        // beside the scheduled timeplan.
        'jobs' => array(
            // The first example job
            'job1' => array(
                // Name/identifier of the job. Can be omitted if is same
                // as the key of its array.
                'code' => 'job1',
                // Frequency of executing. If is omitted than the job will be
                // executed only on demand.
                'frequency' => '0 20 * * *',
                // `RouteTask` defines task that is using existing application's
                // route as a target job's action.
                'task' => array(
                    'type' => 'CronHelper\Service\JobTask\RouteTask',
                    'options' => array(
                        'routeName' => 'cron_job1',
                    ),
                ),
                // Optional callback arguments.
                'args' => array(
                    'name' => 'value'
                ),
            ),
            // The second example job
            'job2' => array(
                'frequency' => '0 0 1 * *',
                // `CallbackTask` is task that executes regular PHP code.
                'task' => array(
                    'type' => 'CronHelper\Service\JobTask\CallbackTask',
                    'options' => array(
                        'className' => 'YourClass',
                        'methodName' => 'doAction',
                    ),
                ),
            ),
            // The third example job - this job has no frequency defined
            // so can be executed only on direct demand from the code.
            'job3' => array(
                // `ExternalTask` is used for executing external scripts.
                'task' => array(
                    'type' => 'CronHelper\Service\JobTask\ExternalTask',
                    'options' => array(
                        'command' => '/var/www/renbo/bin/export_dump.sh'
                    ),
                ),
            ),
        ),
    ),
);

此时最重要的是正确配置数据库驱动程序。

准备数据库

如果我们正确设置了数据库适配器,我们可以创建记录的表 - 在您的应用程序文件夹中打开控制台并执行 create storage 命令

php public/index.php db create

您应该收到消息 存储已成功创建!

Crontab

最后一步很明显——我们需要将我们的服务添加到您的 crontab 文件中,以便所有这些都能正常工作。

TODO ... 完成(示例)! ...

此时安装过程结束,您可以使用 CronHelper 服务了。

使用

CronHelper 命令只能通过 CLI 接口访问(即使您的应用程序尚未支持 CLI)。

以下是可用命令的列表

  • 主命令(从 crontab 使用):cron
  • 与日志数据库相关的命令:db createdb cleardb destroy

TODO ... 完成! ...

开发者

以下是一些针对开发者的注意事项

  1. CI服务正在Travis CI上运行,测试也在PHP 5.4以及PHP 5.5上执行。
  2. 开发过程中使用的其他工具:ComposerphpDocumentorPHPUnitNetBeans IDE

运行测试

phpunit -c test/phpunit.xml

测试覆盖率报告可以在test/log/report文件夹中找到。

生成文档

phpdoc run -d "src" -t "docs/generated" --title "CronHelper Module" --defaultpackagename "CronHelper" -q