xcode75 / jobby
无需修改crontab即可管理所有cron任务。处理锁定、日志、错误邮件等。
v3.5.2
2023-05-11 18:49 UTC
Requires
- php: >=7.4
- dragonmantank/cron-expression: ^3.0
- opis/closure: ^3.5
- 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-11 21:51:25 UTC
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(DateTimeImmutable $now) { // Run on even minutes return $now->format('i') % 2 === 0; }, ]); /* ... */
支持选项
每个任务都需要以下内容
以下列出的选项可以应用于单个任务或通过Jobby构造函数全局应用。全局选项将用作默认值,而单个任务可以覆盖它们。
Symfony集成
Symfony的Jobby扩展 - imper86/jobby-cron-bundle
致谢
之前已经开发过,但受到了whenever的启发。