chriskonnertz/jobs 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

chriskonnertz/jobs

Composer 安装命令:

composer require chriskonnertz/jobs

包简介

Simple cron job manager

README 文档

README

Minimalistic Cron job manager. Register jobs and the job manager will execute them automatically depending on their interval.

NOTE: This is not a queue manager and therefore this has nothing to do with Laravel's queue component. Also note that Laravel 5 has an integrated task scheduler that works similar to this library.

Installation

This library requires PHP >= 7.0

Add chriskonnertz/jobs to composer.json:

"chriskonnertz/jobs": "3.*"

Or via a console:

composer require chriskonnertz/jobs

In the future run composer update to update to the latest version of this library.

Framework support

This library supports Laravel >=5.5 with a service provider. Add the service provider to the config file config/app.php:

    'providers' => array(
        // ...
        'ChrisKonnertz\Jobs\Integration\JobsServiceProvider',
    ),

To create an alias for the facade, add this new entry in this file:

    'aliases' => array(
        // ...
        'Jobs' => 'ChrisKonnertz\Jobs\Integration\JobsFacade',
        'AbstractJob' => 'ChrisKonnertz\Jobs\AbstractJob',
    ),

Introduction

Create a concrete job class:

    class ExampleJob extends ChrisKonnertz\Jobs\AbstractJob 
    {

        protected $name = 'exampleJob';

        protected $interval = 5; // Run every five minutes

        public function run(int $executedAt = null)
        {
            echo 'Hello World!';
        }

    }

Instantiate the job manager:

    $cache = new ExampleCacheClass;
    $jobs = new ChrisKonnertz\Jobs\Jobs($cache);

If you use Laravel with the service provider you do not have to worry about this. The service provider will inject the cache dependency. In any other case the cache class has to implement the cache interface (CacheInterface). Take a look at the LaravelCache class (that is meant for Laravel integration) for an example implementation.

Register the job:

    $jobs->addLazy('updateStreams', 'ExampleJob');

Execute the registered jobs:

    $jobs->run();

Automatically execute jobs

If your application is built on top of Laravel, you will have access to an Artisan command: php artisan jobs This command will call Jobs::run() to execute the jobs. Therefore you can add a Cron job to the crontab to start the command, for example 1 * * * * php /var/www/laravel/artisan jobs. This will execute the Artisan command every minute. We recommend to run the Cron job every minute.

Methods of the jobs manager

Note: Some of these methods may throw a JobException.

Determine if a job exists in the pool

    $hasJob = $jobs->has('exampleJob');

Add a job to the pool (without lazy loading)

    $job = new ExampleJob;
    $jobs->add($job);

Add a job to the pool (with lazy loading)

    // Pass the class name:
    $jobs->addLazy(\My\Example\Job::class);

    // Or pass a closure:
    $jobs->addLazy(function()
    {
        return new ExampleJob;
    });

We recommend using addLazy() over add().

Remove a job from the pool

    $jobs->remove('exampleJob');

Remove all jobs from the pool

    $jobs->clear();

Count the jobs

    $howMany = $jobs->count();

Get the remaining cool down

$minutes = $jobs->remainingCoolDown();

Get the timestamp of the last iteration

$timestamp =  $jobs->lastRunAt();

Set the minimum cool down time for all jobs

    $jobs->coolDown(1); // One minute

The minimum value and the initial value is one minute. Most likely there is no reason to change this value ever.

Set the cache key namespace

    $jobs->cacheKey('jobs.');

The job class

A job class implements the job interface. Therefore it has to implement these methods:

    interface JobInterface 
    {

        public function getName() : string; // The name (identifier) of the job

        public function getActive() : bool; // Active or paused (=not executed)?

        public function getInterval() : int; // The cool down time

        public function run(int $executedAt = null); // The run method

    }

The AbstractJob class actually implements these methods so we recommend to let your concrete job classes inherit from this class. The abstract class provides the attributes name, active and interval that inheriting classes may overwrite.

The interval

Per default (as long as the inheriting job class does not overwrite it) the getInterval() is a simple getter for the interval attribute. The interval attribute defines the duration of the job's cool down in minutes. For example if it is 60 minutes (= 1 hour) the job is executed once per hour (max).

Status

Status of this repository: Deprecated. Issues will be fixed but no new feature implemented.

chriskonnertz/jobs 适用场景与选型建议

chriskonnertz/jobs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.5k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 chriskonnertz/jobs 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-27