gitbugr/

jobby

无需修改crontab即可管理所有cron任务。处理锁定、日志记录、错误邮件等功能。

维护者

详细信息

github.com/gitbugr/jobby

主页

源代码

安装: 23

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 0

分支: 155

v4.0.0 2024-09-16 16:16 UTC

This package is auto-updated.

Last update: 2024-09-16 16:18:04 UTC


README

Total Downloads Latest Version Build Status MIT License

安装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集成

Jobby的Symfony扩展 - imper86/jobby-cron-bundle

致谢

以前开发过,但受whenever的启发。

支持此项目