定制 ignitor/queues 二次开发

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

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

ignitor/queues

Composer 安装命令:

composer require ignitor/queues

包简介

Better Queues for CodeIgniter 4

README 文档

README

This is a Laravel like Queues for CodeIgniter 4.

Installation

You can install the package via composer:

composer require ignitor/queues:^1.0@dev

php spark queue:table

php spark migrate

Usage

// In your method
public function testQueue()
{
    toQueue(function () {
        sleep(10);
        // log to file or send an email
    });

    return "Request is being Processed";
}
php spark queue:work

Dedicated Job Class

If you want to use a dedicated Job class, you can use the make:job command.

php spark make:job TestJob

Job Class

<?php

namespace App\Jobs;

use Igniter\Queues\Queue\DispatchableTrait;
// use Igniter\Queues\Queue\IsEncryptedInterface;
use Igniter\Queues\Queue\ShouldQueueInterface;

class TestJob implements ShouldQueueInterface
{
    use DispatchableTrait;

    /**
     * The queue to run the job on.
     *
     * @var string
     */
    public string $queue = 'default';

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param int $delay
     *
     */
    public int $delay = 0;

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param string $delayType
     *
     */
    public string $delayType = 'minutes';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Run the job.
     *
     * @return void
     */
    public function run()
    {
        // log to file or send an email, etc.
    }
}

Then you can use it like this:

public function testQueue()
{
    TestJob::dispatch();

    return "Request is being Processed";
}

Sending Data to the Queue

You can send data to the queue by using the toQueue method.

public function testQueue()
{
    toQueue(function ($data) {
        sleep(10);
        // log to file or send an email
    }, ['data' => 'some data']);

    return "Request is being Processed";
}

For a dedicated job, you can use pass as arguments to the dispatch method.

TestJob::dispatch('some data');

TestJob::dispatch('some data', 'more data', 'even more data');

This data will be available in the job as constructor arguments in your job class.

Delaying the Job

You can delay the job by using the delay method.

public function testQueue()
{
    toQueue(function () {
        sleep(10);
        // log to file or send an email
    }, delay: 10);

    return "Request is being Processed";
}

Encrypting Sensitive Data

You can encrypt sensitive data by implementing the IsEncryptedInterface interface.

<?php

namespace App\Jobs;

use Igniter\Queues\Queue\DispatchableTrait;
use Igniter\Queues\Queue\IsEncryptedInterface;
use Igniter\Queues\Queue\ShouldQueueInterface;

class TestJob implements ShouldQueueInterface, IsEncryptedInterface
{
    use DispatchableTrait;

    /**
     * The queue to run the job on.
     *
     * @var string
     */
    public string $queue = 'default';

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param int $delay
     *
     */
    public int $delay = 0;

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param string $delayType
     *
     */
    public string $delayType = 'minutes';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Run the job.
     *
     * @return void
     */
    public function run()
    {
        // log to file or send an email, etc.
    }
}

Running Multiple Workers

You can run multiple workers by using the queue:work command.

php spark queue:work --workers 2

You can also just run the workers in single-worker mode. and run the command multiple times.

php spark queue:work

php spark queue:work

php spark queue:work

php spark queue:work

php spark queue:work

You can use the --retry option to retry failed jobs.

php spark queue:work --retry 5

Restarting Workers

You can restart automatically workers when they stop by using the queue:work command with the --restart option.

php spark queue:work --restart

Queue Table

You can create the queue table by using the queue:table command.

php spark queue:table

Specifying the Queue name

You can specify the queue name by using the queue option.

public function testQueue()
{
    toQueue(function () {
        sleep(10);
        // log to file or send an email
    }, queue: 'emails');

    return "Request is being Processed";
}

For Dedicated Jobs, you can use the queue property on the job class.

To specify the queue name when working the queue, you can use the --queue option.

php spark queue:work --queue emails

Handling Failed Jobs

You can handle a failed job at a per-job basis by implementing the CanFailInterface interface.

<?php

namespace App\Jobs;

use Igniter\Queues\Queue\DispatchableTrait;
use Igniter\Queues\Queue\ShouldQueueInterface;
use Igniter\Queues\Queue\CanFailInterface;

class TestJob implements ShouldQueueInterface, CanFailInterface
{
    use DispatchableTrait;

    /**
     * The queue to run the job on.
     *
     * @var string
     */
    public string $queue = 'default';

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param int $delay
     *
     */
    public int $delay = 0;

    /**
     * Delay the job by a given amount of seconds.
     *
     * @param string $delayType
     *
     */
    public string $delayType = 'minutes';

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Run the job.
     *
     * @return void
     */
    public function run()
    {
        // log to file or send an email, etc.
    }

    /**
     * Triggered when this job fails for whatever reason and only if this job is queueable
     * 
     * @param stdClass $job
     * @param string $message
     */
    public function onFailure($job, $message)
    {
        if ($job->attempts >= 3) { // It totally failed
            $email = service('email');
            $email->setTo('info@example.com');
            $email->setSubject('Some Subject');
            $email->setMessage('Some Message: job-id(' . $job->id . ') <br /> Data:' . json_encode($this->data) . ' with messege: ' . $message);
            $email->send();
        }
    }
}

For non-dedicated jobs, you can pass a closure to the toQueue method.

public function testQueue()
{
    toQueue(
        callback: function ($data) {
            sleep(10);
            // log to file or send an email
        },
        data: ['data' => 'some data'],
        onFailure: function ($job, $message) {
            if ($job->attempts >= 3) { // It totally failed
                $email = service('email');
                $email->setTo('info@example.com');
                $email->setSubject('Some Subject');
                $email->setMessage('Some Message: job-id(' . $job->id . ') <br /> Data:' . json_encode($data) . ' with message: ' . $message);
                $email->send();
            }
        }
    );

    return "Request is being Processed";
}

Running a Queue as a Cron Job

You can run a queue as a cron job by using the queue:work command.

In your crontab file, add the following line:

*/5 * * * * cd /var/www/html/your-project && php spark queue:work

This will run the queue every 5 minutes.

You can also specify the queue name by using the --queue option.

*/5 * * * * cd /var/www/html/your-project && php spark queue:work --queue emails

You can also specify the number of workers to run by using the --workers option.

*/5 * * * * cd /var/www/html/your-project && php spark queue:work --workers 4

License

The MIT License (MIT). Please see License File for more information.

ignitor/queues 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-24