undostres/jobby
无需修改crontab即可管理所有cron任务。处理锁定、日志记录、错误邮件等。
此包的官方仓库似乎已不存在,因此该包已被冻结。
v3.4.6
2019-08-02 14:48 UTC
Requires
- php: >=5.6
- jeremeamia/superclosure: ^2.2
- mtdowling/cron-expression: ^1.0
- swiftmailer/swiftmailer: ^5.4|^6.0
- symfony/process: ^2.7|^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^4.6
- symfony/filesystem: ^2.7|^3.0|^4.0
This package is not auto-updated.
Last update: 2021-08-03 10:09:56 UTC
README
安装主jobby cron任务,它将管理所有您的离线任务。添加任务无需修改crontab。Jobby可以处理日志记录、锁定、错误邮件等。
新仓库:我们已经将jobby
迁移到了一个Github组织。请更新您的远程仓库为https://github.com/jobbyphp/jobby.git
。
功能
- 维护一个主cron任务。
- 任务通过PHP运行,因此您可以在任何程序性条件下运行它们。
- 使用普通的crontab计划语法(由出色的
cron-expression
提供支持)。 - 在给定时间只运行一个任务副本。
- 当任务以错误状态退出时发送电子邮件。
- 如果crontab用户有
sudo
权限,则以其他用户身份运行任务。 - 仅在某些主机名上运行(在web农场中很有用)。
- 理论上的Windows支持(但从未测试过)
入门
安装
安装Jobby的推荐方法是使用Composer。
$ 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; }, ]); /* ... */
支持选项
每个任务都需要以下内容
键 | 类型 | 描述 |
---|---|---|
schedule | 字符串 | Crontab计划格式(man -s 5 crontab )、DateTime格式(Y-m-d H:i:s )或可调用(function(): Bool { /* ... */ } ) |
command | 字符串 | 要运行的shell命令(与closure 互斥) |
closure | 闭包 | 要运行的匿名PHP函数(与command 互斥) |
以下选项可以应用于单个任务或通过Jobby
构造函数全局应用。全局选项将用作默认值,而单个任务可以覆盖它们。
选项 | 类型 | 默认 | 描述 |
---|---|---|---|
runAs | 字符串 | null | 如果crontab用户有sudo 权限,则以此用户身份运行 |
debug | 布尔值 | False | 向 'debug.log' 发送 jobby 内部消息 |
过滤 | 确定作业是否应运行或不应运行的选项 | ||
环境 | 字符串 | null 或 getenv('APPLICATION_ENV') |
此作业的开发环境 |
runOnHost | 字符串 | gethostname() |
仅在当前主机上运行作业 |
maxRuntime | 整数 | null | 此作业的最大执行时间(以秒为单位) |
enabled | 布尔值 | true | 按计划时间运行此作业 |
haltDir | 字符串 | null | 如果此目录包含具有作业名称的文件,则作业将不会运行 |
日志记录 | 日志记录选项 | ||
output | 字符串 | /dev/null | 将 stdout 和 stderr 重定向到该文件 |
output_stdout | 字符串 | 来自 output 选项的值 |
将 stdout 重定向到该文件 |
output_stderr | 字符串 | 来自 output 选项的值 |
将 stderr 重定向到该文件 |
dateFormat | 字符串 | Y-m-d H:i:s | jobby 日志消息上的日期格式 |
邮件 | 错误邮件选项 | ||
recipients | 字符串 | null | 以逗号分隔的电子邮件地址字符串 |
mailer | 字符串 | sendmail | 邮件方法: sendmail 或 smtp 或 mail |
smtpHost | 字符串 | null | SMTP 主机,如果 mailer 是 smtp |
smtpPort | 整数 | 25 | SMTP 端口,如果 mailer 是 smtp |
smtpUsername | 字符串 | null | SMTP 用户,如果 mailer 是 smtp |
smtpPassword | 字符串 | null | SMTP 密码,如果 mailer 是 smtp |
smtpSecurity | 字符串 | null | SMTP 安全选项: ssl 或 tls,如果 mailer 是 smtp |
smtpSender | 字符串 | jobby@<hostname> | SMTP 通知中使用的发送者和来自地址 |
smtpSenderName | 字符串 | Jobby | SMTP 消息中用于来自字段的名称 |
致谢
之前已开发,但受 whenever 的启发。