[原创]php任务调度器 hellogerard/jobby

简介

一个强大的php层面上的定时任务调度器, 无需修改系统层面的crontab

实际中, php 结合 crontab 来实现定时任务是一种经得住考验的实践, 但每次新增/修改定时任务项时都需要去修改系统的crontab, 这会带来两个麻烦: 一个是繁琐, 一个是难以版本控制.

特点:

  • 只需维护一个crontab主作业
  • 子作业完全由PHP编写
  • 支持标准crontab的调度时间格式
  • 作业执行超时时可防止运行多个作业副本
  • 作业执行出错退出时发送邮件通知
  • 可以其他用户身份执行(若主crontab执行用户具有sudo权限)
  • 支持仅在某个主机名上运行
  • 理论上的Windows支持(包作者未测试过)

相关链接:

  • https://packagist.org/packages/hellogerard/jobby
  • https://github.com/jobbyphp/jobby

安装

php 5.4

composer require "hellogerard/jobby:3.4.1"

注意版本不要用3.4.1以上的, 会出问题(尽管packagist里3.4.2和3.4.3写着php>=5.4, 但实际会出问题)

php 更高版本

composer require hellogerard/jobby

配置crontab主作业

* * * * * cd /path/to/project && php jobby.php &>/dev/null

使用示例文件

cp vendor/hellogerard/jobby/resources/jobby.php .

标准使用

// 加载composer autoload
require_once __DIR__ . '/vendor/autoload.php';

// 创建Jobby实例
$jobby = new Jobby\Jobby();

// 每个job都有一个唯一名称
$jobby->add('CommandExample', [

    // 运行一个shell命令
    'command'  => 'ls',
    
    // 运行一个闭包(与 'command' 互斥)
    //'closure'  => function() {
    //    echo "I'm a function!\n";
    //    return true;
    //},

    // 支持通用调度配置
    'schedule' => '0 * * * *',
    
    // 可使用日期时间字符串
    //'schedule' => '2017-05-03 17:15:00',
    
    // 使用闭包返回boolean来确定是否执行
    //'schedule' => function(DateTimeImmutable $now) {
    //    // Run on even minutes
    //    return $now->format('i') % 2 === 0;
    //},

    
    // 标准输出/标准错误会写入到指定日志文件
    'output'   => 'logs/command.log',
    
    // 临时关闭某个任务
    'enabled'  => false,
]);

$jobby->run();

选项

每个任务的必须选项

KeyTypeDescription
schedulestringCrontab schedule format (man -s 5 crontab) or DateTime format (Y-m-d H:i:s) or callable (function(): Bool { /* ... */ })
commandstringThe shell command to run (exclusive-or with closure)
closureClosureThe anonymous PHP function to run (exclusive-or with command)

以下的选项列表可应用于单个任务或全局任务(通过传入 Jobby构造器).

全局选项将会作为每个任务的默认配置, 单个任务可覆盖全局选项.

OptionTypeDefaultDescription
runAsstringnullRun as this user, if crontab user has sudo privileges
debugbooleanfalseSend jobby internal messages to 'debug.log'
Filtering 过滤Options to determine whether the job should run or not
environmentstringnull or getenv('APPLICATION_ENV')Development environment for this job
runOnHoststringgethostname()Run jobs only on this hostname
maxRuntimeintegernullMaximum execution time for this job (in seconds)
enabledbooleantrueRun this job at scheduled times
haltDirstringnullA job will not run if this directory contains a file bearing the job's name
Logging 日志Options for logging
outputstring/dev/nullRedirect stdout and stderr to this file
output_stdoutstringvalue from output optionRedirect stdout to this file
output_stderrstringvalue from output optionRedirect stderr to this file
dateFormatstringY-m-d H:i:sFormat for dates on jobby log messages
Mailing 邮件Options for emailing errors
recipientsstringnullComma-separated string of email addresses
mailerstringsendmailEmail method: sendmail or smtp or mail
smtpHoststringnullSMTP host, if mailer is smtp
smtpPortinteger25SMTP port, if mailer is smtp
smtpUsernamestringnullSMTP user, if mailer is smtp
smtpPasswordstringnullSMTP password, if mailer is smtp
smtpSecuritystringnullSMTP security option: ssl or tls, if mailer is smtp
smtpSenderstringjobby@The sender and from addresses used in SMTP notices
smtpSenderNamestringJobbyThe name used in the from field for SMTP messages

项目实践

项目存在多个不同游戏服, 且每个服使用的配置文件不一样, 因此执行定时任务时需对每个服单独执行, 特创建crontab.php 文件用于针对不同服分别调用 jobby.php

crontab配置

* * * * * cd /path/to/project && php crontab.php &>/dev/null

crontab.php

<?php
// 服务器列表, 实际是从其他地方读取的
$servers = ["s1", "s2", "s3", "s4"];
foreach ($servers as $server) {
    $cmd = sprintf("php jobby.php %s &>/dev/null &", $server);
    exec($cmd);
    var_dump(compact('cmd'));
}

jobby.php

<?php
// 环境配置
// ...

$server = $argv[1];    
 
$cmdPrefix = "php dispatcher.php $server ";

$jobby = new \Jobby\Jobby([
    'output' => "具体的日志输出文件路径",
]);
$jobs = require("jobby_config.php");
foreach ($jobs as $name => $job) {
    if (isset($config['command'])) {
        $config['command'] = $cmdPrefix.$config['command'];
    }
    $jobby->add($name, $job);
}
$jobby->run();

jobby_config.php

<?php
return [
    "check_index" => [
        'command' => "month_check_index",
        'schedule' => '30 5 1 * *',
        'maxRuntime' => 600,
    ],
    
    'minute_stats' => [
        'command' => "minute_stats",
        'schedule' => '* * * * *',
        'maxRuntime' => 300,
        'enabled' => true,
    ],
];

dispatcher.php 加载具体服的具体环境, 并调用实际要运行的php定时任务代码

$server = $argv[1];
$command = $argv[2];

// 加载具体服的环境配置
// ...

// 执行具体定时任务代码(此处忽略异常处理)
require __DIR__ . DIRECTORY_SEPARATOR . "jobs" . DIRECTORY_SEPARATOR . $command . ".php";

jobs/month_check_index.php

<?php
// 具体任务

jobs/minute_stats.php

<?php
// 具体任务

转载于:https://www.cnblogs.com/youjiaxing/p/10365904.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值