hellogerard / jobby
无需修改crontab即可管理所有cron作业。处理锁定、日志记录、错误邮件等。
v3.5.0
2020-07-25 20:18 UTC
Requires
- php: >=5.6
- 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
This package is auto-updated.
Last update: 2024-09-15 06:44:40 UTC
README
安装主jobby cron作业,它会管理所有离线任务。无需修改crontab即可添加作业。Jobby可处理日志记录、锁定、错误邮件等。
新仓库:我们将jobby
迁移到了Github组织。请更新您的远程链接到https://github.com/jobbyphp/jobby.git
。
特性
- 维护一个主crontab作业。
- 作业通过PHP运行,因此您可以在任何程序性条件下运行它们。
- 使用普通的crontab计划语法(由优秀的
cron-expression
提供支持)。 - 在任何给定时间只运行一个作业的副本。
- 当作业以错误状态退出时发送电子邮件。
- 如果crontab用户有
sudo
权限,则以其他用户运行作业。 - 仅在特定的主机名上运行(在Web农场中很有用)。
- 理论上的Windows支持(但从未测试过)
入门
安装
推荐通过Composer安装Jobby
$ composer require hellogerard/jobby
然后添加以下行到您的crontab文件中
* * * * * cd /path/to/project && php jobby.php 1>> /dev/null 2>&1
Jobby安装后,您可以将示例文件复制到项目根目录。
$ cp vendor/hellogerard/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 Bundle - imper86/jobby-cron-bundle
鸣谢
之前开发过,但受到了whenever的启发。