xshimmy / kohana-cron
本模块为您的Kohana应用程序提供了调度任务(作业)的方法。
0.1.2
2015-06-25 11:46 UTC
Requires
- php: >=5.3.0
- composer/installers: *
This package is not auto-updated.
Last update: 2024-09-28 17:49:16 UTC
README
本模块为您的Kohana应用程序提供了调度任务(作业)的方法。
安装
步骤 1:将模块下载到您的模块子目录中。
步骤 2:在您的启动文件中启用模块
/**
* Enable modules. Modules are referenced by a relative or absolute path.
*/
Kohana::modules(array(
'cron' => MODPATH.'cron',
// 'auth' => MODPATH.'auth', // Basic authentication
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
// 'database' => MODPATH.'database', // Database access
// 'image' => MODPATH.'image', // Image manipulation
// 'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'pagination' => MODPATH.'pagination', // Paging of results
// 'userguide' => MODPATH.'userguide', // User guide and API documentation
));
步骤 3:确保 config/cron.php
中的设置符合您的环境。如果不合适,请将文件复制到 application/config/cron.php
并相应地更改值。
使用方法
在其最简单形式中,任务是一个 PHP 回调 和它应该运行的时机。要配置任务,请调用 Cron::set($name, array($frequency, $callback))
,其中 $frequency
是与 crontab 中找到的日期和时间字段相同的字符串。例如,
Cron::set('reindex_catalog', array('@daily', 'Catalog::regenerate_index'));
Cron::set('calendar_notifications', array('*/5 * * * *', 'Calendar::send_emails'));
配置的任务将通过调用 Cron::run()
以适当的频率运行。在您的启动文件中调用此方法,然后您就完成了!
高级使用
任务也可以是扩展了 next()
和/或 execute()
的 Cron
实例。此类任务通过调用 Cron::set($name, $instance)
进行配置。
如果您有权访问系统 crontab,则可以运行 Cron 每次请求(或少于一次)。您需要修改启动文件中处理请求的行,以防止产生不必要的输出。默认设置是
/**
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/
echo Request::instance()
->execute()
->send_headers()
->response;
更改为
if ( ! defined('SUPPRESS_REQUEST'))
{
/**
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/
echo Request::instance()
->execute()
->send_headers()
->response;
}
然后设置一个系统 cron 作业,使您的应用程序的 Cron 每分钟运行一次
* * * * * /usr/bin/php -f /path/to/kohana/modules/cron/run.php
包含的 run.php
应该适用于大多数情况,但您可以根据自己的喜好以任何方式调用 Cron::run()
。