定制 kfosoft/yii2-queue-scheduler 二次开发

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

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

kfosoft/yii2-queue-scheduler

Composer 安装命令:

composer require kfosoft/yii2-queue-scheduler

包简介

Extension provides functionality for make scheduled job for yii2-queue lib

README 文档

README

Yii2 Queue Scheduler

Yii2 extension for yiisoft/yii2-queue library

Install

composer require kfosoft/yii2-queue-scheduler

Configure

For both(web, console) configs

    'components' => [
        kfosoft\queue\components\QueueScheduler::COMPONENT_NAME => [
            'class'              => kfosoft\queue\components\QueueScheduler::class,
            'modelClassName'     => 'default', // you can change model class name for don't use SchedulerQueueModel. But this class has to be implement the SchedulerQueueModelInterface
            'tableName'          => kfosoft\queue\models\SchedulerQueueModel::tableName(), // you can change name of database table without change SchedulerQueueModel
            'queueComponentName' => 'queue', // name of queue component yiisoft/yii2-queue
            'db'                 => 'db', // name of database component
            'daemonSleepTime'    => 10, // this timeout defines time between reading of SchedulerQueue database
        ],
        ...
    ],
    ...

For console config

    'bootstrap' => ['queue', kfosoft\queue\components\QueueScheduler::COMPONENT_NAME, ...],
    ...

And migrate the migration from repository

To run daemon

bin/yii queue-scheduler

NOTE: To make it work properly you have to choose one of the options:

  • supervisor look at the supervisor config in repository
  • cron with WatcherDaemon(NOT TESTED).
namespace console\controllers;

use kfosoft\daemon\WatcherDaemon;

class WatcherDaemonController extends WatcherDaemon
{
    /**
     * @return array
     */
    protected function defineJobs()
    {
        sleep($this->sleep);

        //TODO: modify list, or get it from config, it does not matter
        $daemons = [
            ['className' => \kfosoft\queue\commands\SchedulerDaemonController::class, 'enabled' => true],
        ];

        return $daemons;
    }
}

Then add this line to crontab

* * * * * /path/to/yii/project/yii watcher-daemon --demonize=1

Using

  • In order to use scheduler you have to create job and implement ScheduledJobInterface. Also you have to implement getJobParams method in job class. It has to return the array fields values for creating a job. For example:
<?php

namespace app\jobs;

use kfosoft\queue\ScheduledJobInterface;
use Yii;
use yii\base\BaseObject;
use yii\base\InvalidConfigException;

class ImpotantJob extends BaseObject implements ScheduledJobInterface
{
    /**
     * @var int
     */
    public $param1;

    /**
     * @var string
     */
    public $param2;

    /**
     * {@inheritdoc}
     * @throws InvalidConfigException
     */
    public function execute($queue): void
    {
        // Your very important logic
    }

    /**
     * {@inheritdoc}
     */
    public function getJobParams(): array
    {
        return [
            'param1' => $this->param1,
            'param2' => $this->param2
        ];
    }
}

  • You can schedule a job for example in 5 minutes or at 7AM next day.
// In 5 min
$dateTime = (new \DateTime())->modify('+5 minutes');
$job = new \app\jobs\ImpotantJob(['param1' => 1, 'param2' => 'Very important text']);

Yii::$app->get(\kfosoft\queue\components\QueueScheduler::COMPONENT_NAME)
    ->enqueueAt($job, $dateTime->getTimestamp());
// Next day at 7AM
$dateTime = (new \DateTime())->modify('+1 day')->setTime(7,0,0);
$job = new \app\jobs\ImpotantJob(['param1' => 1, 'param2' => 'Very important text']);

Yii::$app->get(\kfosoft\queue\components\QueueScheduler::COMPONENT_NAME)
    ->enqueueAt($job, $dateTime->getTimestamp());

NOTE: All jobs run in UTC timezone. Please set timestamp for enqueueAt method(The second param) in server's timezone because the component converts the timezone to UTC automatically.

  • You can check scheduled jobs if needed.
$job = new \app\jobs\ImpotantJob(['param1' => 1, 'param2' => 'Very important text']);
$hasJob = Yii::$app->get(\kfosoft\queue\components\QueueScheduler::COMPONENT_NAME)
    ->hasDelayed($job); // bool returns
  • Also you can remove the scheduled jobs if needed.
$job = new \app\jobs\ImpotantJob(['param1' => 1, 'param2' => 'Very important text']);
Yii::$app->get(\kfosoft\queue\components\QueueScheduler::COMPONENT_NAME)
    ->removeDelayed($job);
  • Also you can get the scheduled jobs for current moment.
Yii::$app->get(\kfosoft\queue\components\QueueScheduler::COMPONENT_NAME)
    ->getJobsForNow(); // returns array jobs

kfosoft/yii2-queue-scheduler 适用场景与选型建议

kfosoft/yii2-queue-scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 268 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 kfosoft/yii2-queue-scheduler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-19