hatimox / jobby
无需修改crontab即可管理所有cron任务。处理锁定、日志记录、错误邮件等。
v3.6.2
2024-03-29 11:33 UTC
Requires
- php: >=7.4
- alek13/slack: ^2.2
- dragonmantank/cron-expression: ^3.0
- opis/closure: ^3.5
- swiftmailer/swiftmailer: ^5.4|^6.0
- symfony/process: ^2.7|^3.0|^4.0|^5.0
Requires (Dev)
- mp091689/dump-die: ^1.0
- phpunit/phpunit: ^4.6
- symfony/filesystem: ^2.7|^3.0|^4.0|^5.0
- dev-master
- v3.6.2
- v3.6.1
- v3.6.0
- V3.5.2
- V3.5.1
- v3.5.0
- v3.4.7
- v3.4.6
- v3.4.5
- v3.4.4
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.0
- v2.1.0
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
- dev-update-dependencies
- dev-Fix-Logs
- dev-Logs_Optimizations
- dev-Develop
This package is auto-updated.
Last update: 2024-09-29 12:43:41 UTC
README
安装master jobby cron作业,它会管理所有离线任务。无需修改crontab即可添加作业。Jobby可以处理日志记录、锁定、错误邮件等。
特性
- 维护一个主cron作业。
- 作业通过PHP运行,因此可以在任何程序性条件下运行。
- 使用普通的crontab计划语法(由出色的
cron-expression
提供支持)。 - 在给定时间只运行一个作业副本。
- 当作业以错误状态退出时发送电子邮件。
- 如果crontab用户有
sudo
权限,则以另一个用户的身份运行作业。 - 仅在特定的主机名上运行(在webfarm中很有用)。
- 理论上的Windows支持(但从未测试过)
- 当作业以错误状态退出时,向Slack或Mattermost发送警报。
入门指南
安装
推荐通过Composer安装Jobby
$ composer require hatimox/jobby
然后向您的(或任何人的)crontab添加以下行
* * * * * cd /path/to/project && php jobby.php 1>> /dev/null 2>&1
Jobby安装后,您可以将示例文件复制到项目根目录。
$ cp vendor/hatimox/jobby/resources/jobby.php .
运行作业
<?php // Ensure you have included composer's autoloader require_once __DIR__ . '/vendor/autoload.php'; // Create a new instance of Jobby $jobby = new Jobby\Jobby(); // Every job has a name $jobby->add('CommandExample', [ // Run a shell command 'command' => 'ls', // Ordinary crontab schedule format is supported. // This schedule runs every hour. 'schedule' => '0 * * * *', ]); $jobby->run();
示例
日志记录
<?php /* ... */ $jobby->add('LoggingExample', [ 'command' => 'ls', 'schedule' => '0 * * * *', // Stdout and stderr is sent to the specified file 'output' => 'logs/command.log', ]); /* ... */
禁用命令
<?php /* ... */ $jobby->add('DisabledExample', [ 'command' => 'ls', 'schedule' => '0 * * * *', // You can turn off a job by setting 'enabled' to false 'enabled' => false, ]); /* ... */
运行闭包
在运行闭包时,请注意闭包外部没有任何内容可见(参见#93)!
<?php /* ... */ $jobby->add('ClosureCommandExample', [ // Use the 'closure' key // instead of 'command' 'closure' => function() { echo "I'm a function!\n"; return true; }, 'schedule' => '0 * * * *', ]); /* ... */
使用DateTime
<?php /* ... */ $jobby->add('DateTimeExample', [ 'command' => 'ls', // Use a DateTime string in // the format Y-m-d H:i:s 'schedule' => '2017-05-03 17:15:00', ]); /* ... */
使用自定义调度器
<?php /* ... */ $jobby->add('Example', [ 'command' => 'ls', // Use any callable that returns // a boolean stating whether // to run the job or not 'schedule' => function(DateTimeImmutable $now) { // Run on even minutes return $now->format('i') % 2 === 0; }, ]); /* ... */
支持选项
每个作业都需要这些
以下列出的选项可以应用于单个作业或通过Jobby
构造函数全局应用。全局选项将被用作默认值,单个作业可以覆盖它们。
Symfony集成
Symfony为Jobby提供的包 - imper86/jobby-cron-bundle
致谢
之前已开发,但受whenever的启发。