yii2tech/crontab
最新稳定版本:1.0.4
Composer 安装命令:
composer require yii2tech/crontab
包简介
Crontab support extension for the Yii2 framework
README 文档
README
Crontab Extension for Yii 2
This extension adds Crontab setup support.
For license information check the LICENSE-file.
Requirements
This extension requires Linux OS. 'crontab' should be installed and cron daemon should be running.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/crontab
or add
"yii2tech/crontab": "*"
to the require section of your composer.json.
Usage
You can setup cron tab using [[yii2tech\crontab\CronTab]], for example:
use yii2tech\crontab\CronTab; $cronTab = new CronTab(); $cronTab->setJobs([ [ 'min' => '0', 'hour' => '0', 'command' => 'php /path/to/project/yii some-cron', ], [ 'line' => '0 0 * * * php /path/to/project/yii another-cron' ] ]); $cronTab->apply();
You may specify particular cron job using [[yii2tech\crontab\CronJob]] instance, for example:
use yii2tech\crontab\CronJob; use yii2tech\crontab\CronTab; $cronJob = new CronJob(); $cronJob->min = '0'; $cronJob->hour = '0'; $cronJob->command = 'php /path/to/project/yii some-cron'; $cronTab = new CronTab(); $cronTab->setJobs([ $cronJob ]); $cronTab->apply();
Tip: [[yii2tech\crontab\CronJob]] is a descendant of [[yii\base\Model]] and have built in validation rules for each parameter, thus it can be used in the web forms to create a cron setup interface.
Parsing cron jobs
[[yii2tech\crontab\CronJob]] composes a cron job line, like:
0 0 * * * php /path/to/my/project/yii some-cron
However it can also parse such lines filling up own internal attributes. For example:
use yii2tech\crontab\CronJob; $cronJob = new CronJob(); $cronJob->setLine('0 0 * * * php /path/to/my/project/yii some-cron'); echo $cronJob->min; // outputs: '0' echo $cronJob->hour; // outputs: '0' echo $cronJob->day; // outputs: '*' echo $cronJob->month; // outputs: '*' echo $cronJob->command; // outputs: 'php /path/to/my/project/yii some-cron'
Merging cron jobs
Method [[yii2tech\crontab\CronTab::apply()]] adds all specified cron jobs to crontab, keeping already exiting cron jobs intact. For example, if current crontab is following:
0 0 * * * php /path/to/my/project/yii daily-cron
running following code:
use yii2tech\crontab\CronTab; $cronTab = new CronTab(); $cronTab->setJobs([ [ 'min' => '0', 'hour' => '0', 'weekDay' => '5', 'command' => 'php /path/to/project/yii weekly-cron', ], ]); $cronTab->apply();
will produce following crontab:
0 0 * * * php /path/to/my/project/yii daily-cron
0 0 * * 5 php /path/to/my/project/yii weekly-cron
While merging crontab lines [[yii2tech\crontab\CronTab::apply()]] avoids duplication, so same cron job will never be added twice. However while doing this, lines are compared by exact match, inlcuding command and time pattern. If same command added twice with different time pattern - 2 crontab records will be present. For example, if current crontab is following:
0 0 * * * php /path/to/my/project/yii some-cron
running following code:
use yii2tech\crontab\CronTab; $cronTab = new CronTab(); $cronTab->setJobs([ [ 'min' => '15', 'hour' => '2', 'command' => 'php /path/to/project/yii some-cron', ], ]); $cronTab->apply();
will produce following crontab:
0 0 * * * php /path/to/my/project/yii some-cron
15 2 * * * php /path/to/my/project/yii some-cron
You may interfere in merging process using [[yii2tech\crontab\CronTab::$mergeFilter]], which allows indicating
those existing cron jobs, which should be removed while merging. Its value could be a plain string - in this case
all lines, which contains this string as a substring will be removed, or a PHP callable of the following signature:
bool function (string $line) - if function returns true the line should be removed.
For example, if current crontab is following:
0 0 * * * php /path/to/my/project/yii some-cron
running following code:
use yii2tech\crontab\CronTab; $cronTab = new CronTab(); $cronTab->mergeFilter = '/path/to/project/yii'; // filter all invocation of Yii console $cronTab->setJobs([ [ 'min' => '15', 'hour' => '2', 'command' => 'php /path/to/project/yii some-cron', ], ]); $cronTab->apply();
will produce following crontab:
15 2 * * * php /path/to/my/project/yii some-cron
Extra lines setup
Crontab file may content additional lines beside jobs specifications. It may contain comments or extra shell configuration. For example:
# this crontab created by my application
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin
0 0 * * * php /path/to/my/project/yii some-cron
You may append such extra lines into the crontab using [[yii2tech\crontab\CronTab::$headLines]]. For example:
use yii2tech\crontab\CronTab; $cronTab = new CronTab(); $cronTab->headLines = [ '# this crontab created by my application', 'SHELL=/bin/sh', 'PATH=/usr/bin:/usr/sbin', ]; $cronTab->setJobs([ [ 'min' => '0', 'hour' => '0', 'command' => 'php /path/to/project/yii some-cron', ], ]); $cronTab->apply();
Note: usage of the
headLinesmay produce unexpected results, while merging crontab with existing one.
User setup
Each Linux system user has his own crontab. Ownership of crontab affected by this extension is determined by the user running the PHP script. For the web application it is usually 'apache', for the console application - current local user or root. Thus crontab application from web application and from console application will produce 2 separated cron jobs list for 2 different system users.
You may explicitly setup name of the user whose crontab is to be affected via [[yii2tech\crontab\CronTab::$username]]. For example:
use yii2tech\crontab\CronTab; $cronTab = new CronTab(); $cronTab->username = 'www-data'; // apply crontab for 'www-data' user $cronTab->setJobs([ [ 'min' => '0', 'hour' => '0', 'command' => 'php /path/to/project/yii some-cron', ], ]); $cronTab->apply();
However, this will work only in case PHP script is running from privileged user (e.g. 'root').
yii2tech/crontab 适用场景与选型建议
yii2tech/crontab 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 245.26k 次下载、GitHub Stars 达 178, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cron」 「crontab」 「cronjob」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2tech/crontab 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2tech/crontab 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2tech/crontab 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle to monitor and execute commands
Provides a possibility to auto-setup crontabs required for project based on configuration file.
symfony bundle for jobbyphp/jobby
Scheduling extension for Yii2 framework
Monitoring for scheduled jobs
Yii2 scheduler module
统计信息
- 总下载量: 245.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 185
- 点击次数: 30
- 依赖项目数: 5
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 未知