定制 10quality/scheduler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

10quality/scheduler

Composer 安装命令:

composer require 10quality/scheduler

包简介

PHP job scheduler. (Cronjob tasker)

README 文档

README

Latest Stable Version Total Downloads License

PHP job scheduler. (Cronjob tasker)

Installing

Via composer:

composer require 10quality/scheduler

Usage

Scheduler

Create a php file that will be called by cronjob, Sample, and include the following code lines:

// 1) LOAD COMPOSER AUTOLOAD OR BOOTSTRAP FIRST

// 2)
use Scheduler\Scheduler;

Init scheduler with the required configuration:

Scheduler::ready([
    'jobs'      => ['path' => __DIR__.'/jobs'],
    'session'   => ['driver' => 'file', 'path'=>__DIR__.'/.tmp'],
]);

NOTE: Scheduler requires a defined jobs path (where jobs are located) and the session driver/settings to use to handle intervals (the package only supports file atm).

Register your jobs and execution interval.

Scheduler::ready([...])
    ->job('MyJob', function($task) {
        return $task->daily();
    });

Start scheduler:

Scheduler::ready([...])
    ->job(...)
    ->start();

Then setup a cronjob task to run scheduler, as follows:

* * * * * php /path/to/scheduler-file.php >> /dev/null 2>&1

Jobs

Scheduler runs jobs written in PHP. A job is a PHP class extended from Scheduler\Base\Job class. Once a job is registered in the scheduler, it will call to the execute() function in runtime, Sample.

Create a job mimicking the following example:

use Scheduler\Base\Job;

class MyJob extends Job
{
    public function execute()
    {
        // My code here...
    }
}

For the example above, the job file must be named MyJob.php and must be stored in the jobs path.

Intervals

Execution intervals are defined when registering a job:

Scheduler::ready([...])
    ->job('MyJob', function($task) {
        // Here we define the task interval
        return $task->daily();
    });

Available intervals:

// On every execution
$task->now();

// Daily
$task->daily();

// Weekly
$task->weekly();

// Monthly
$task->monthly();

// Every minute
$task->everyMinute();

// Every 5 minutes
$task->everyFiveMinutes();

// Every 10 minutes
$task->everyTenMinutes();

// Every 30 minutes
$task->everyHalfHour();

// Every hour
$task->everyHour();

// Every 12 hours
$task->everyTwelveHours();

// Every 2 days
$task->everyTwoDays();

// Every 3 days
$task->everyThreeDays();

// Every XXX minutes (custom minutes)
// @param init $minutes Custome minutes interval
$task->custom($minutes);

Events

You can define callable event handlers when configuring the scheduler, like in the following example:

Scheduler::ready([
    'jobs'      => ['path' => __DIR__.'/jobs'],
    'session'   => ['driver' => 'file', 'path'=>__DIR__.'/.tmp'],
    'events'    => [
                    'on_start' => function($microtime) {
                        // Do something during event
                    },
                    'on_job_start' => function($jobName, $microtime) {
                        if ($jobName === 'Job')
                            // Do something during event
                    }
                ],
]);

List of events (parameters can be used optionally):

Event key Parameters Description
on_init Triggered before session is validated.
on_start int $microtime Triggered before executing any job.
on_finish int $microtime Triggered after executing all jobs and saving session.
on_job_start string $jobName, int $microtime Triggered before executing of a job.
on_job_finish string $jobName, int $microtime Triggered after executing of job.

Handling Exceptions

Scheduler will run continuously without interruption. You can handle exceptions individually per job or globally to log them as needed.

Global Exception Handling

Add a on_exception callable handler in the scheduler's events configuration, like in the following example:

Scheduler::ready([
    'jobs'      => ['path' => __DIR__.'/jobs'],
    'session'   => ['driver' => 'file', 'path'=>__DIR__.'/.tmp'],
    'events'    => [
                    'on_exception' => function($e) {
                        // Do anything with exception
                        echo $e->getMessage();
                    }
                ],
]);

Job Exceptions

Add a exception handling callable when defining the job task, like this:

Scheduler::ready([...])
    ->job('MyJob', function($task) {
        // Here we define the task interval
        return $task->daily()
            ->onException(function($e) {
                // Do anything with exception
                echo $e->getMessage();
            });
    });

To reset the job when an exception occurs and force it to be executed on the next run, indicate it on the task:

Scheduler::ready([...])
    ->job('MyJob', function($task) {
        // Here we define the task interval
        return $task->daily()
            ->canReset()
            ->onException(function(Exception $e) {
                // Do anything with exception
            });
    });

Session

File

Indicate the folder where session file will be stored. Make sure this folder has the proper permissions.

Scheduler::ready([
    'jobs'      => ['path' => __DIR__.'/jobs'],
    'session'   => ['driver' => 'file', 'path'=>__DIR__.'/.tmp'],
]);

Callable

You can create your own session driver by extending from the Session interface:

use Scheduler\Contracts\Session;

class MySessionDriver implements Session
{
    /*
     * See and develop required interface methods....
     */
}

Then, when initializing the scheduler, set the driver to callable and add the callable as second parameter, like the following example:

Scheduler::ready([
    'jobs'      => ['path' => __DIR__.'/jobs'],
    'session'   => [
                    'driver'    => 'callable',,
                    'callable'  => function() use(&$session) {
                        return MySessionDriver::load( $custom_options );
                    }
                ],
]);

Requirements

  • PHP >= 5.4

Coding guidelines

PSR-4.

LICENSE

The MIT License (MIT)

Copyright (c) 2016-2019 10Quality.

10quality/scheduler 适用场景与选型建议

10quality/scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「events」 「task」 「jobs」 「cronjob」 「scheduler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 10quality/scheduler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 10quality/scheduler 我们能提供哪些服务?
定制开发 / 二次开发

基于 10quality/scheduler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 112
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-23