bmdevel / jobby
无需修改crontab即可管理所有cron作业。处理锁定、日志记录、错误邮件等。
3.5.0
2020-11-03 14:23 UTC
Requires
- php: >=7.2
- ext-posix: *
- jeremeamia/superclosure: ^2.2
- mtdowling/cron-expression: ^1.0
- swiftmailer/swiftmailer: ^5.4|^6.0
- symfony/process: ^2.7|^3.0|^4.0|^5.0
Requires (Dev)
- phpunit/phpunit: ^8.5
- symfony/filesystem: ^2.7|^3.0|^4.0|^5.0
README
安装主jobby cron作业,它将管理所有离线任务。添加作业无需修改crontab。Jobby可以处理日志记录、锁定、错误邮件等。
新仓库: 我们已将jobby迁移到Github组织。请更新您的远程链接为https://github.com/jobbyphp/jobby.git。
特性
- 维护一个主cron作业。
- 作业通过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() { // Run on even minutes return date('i') % 2 === 0; }, ]); /* ... */
支持选项
每个作业都需要这些
以下列出的选项可以应用于单个作业或通过Jobby构造函数全局应用。全局选项将用作默认值,单个作业可以覆盖它们。
致谢
在whenever的启发下开发。