icy8/queue

1.0.11 2023-08-30 03:41 UTC

This package is auto-updated.

Last update: 2024-05-06 04:22:36 UTC


README

介绍

任务队列,用于发布、消费程序任务。

这是基于think-queue照葫芦画瓢的作品,很多基本的原理都来自think-queue

因为运行过程跟定时任务比较类似,所以我更喜欢称其为任务队列。

软件架构

  1. php>=7.0

  2. symfony/process

安装教程

composer require icy8/queue

使用说明

  1. 监听/消费任务

    因为think-queue的消费进程是基于think命令的,我们并不希望把thinkphp的其他依赖牵扯进来。

    所以定制了一个可以自定义消费进程的方法makeProcess,他可以让你调用任何php进程来消费任务,方法参数只接受stringProcess对象。

    <?php
    
    use icy8\Queue\Listener;
    use Symfony\Component\Process\PhpProcess;
    
    include __DIR__ . "/../../../../autoload.php";
    $listener = new Listener();
    // 进程调度间隔 单位秒 默认是1秒 允许小数
    $listener->execInterval = '0.1';
    // 定制自己的消费进程
    /*$listener->makeProcess('<?php
    // 为redis配置密码
    $exec = new \icy8\Queue\Executor("redis", ["port"=>"6399", "password"=>"123456"]);
    $exec->runNext();');*/
    // 开始监听队列
    $listener->run();
  2. 发布任务

    <?php
    
    include __DIR__ . "/../../../../autoload.php";
    $connection = new \icy8\Queue\connector\Redis();
    $connection->init();
    // 任务立即执行
    $connection->push(\icy8\Queue\test\JobTest::class, ['a', 'b', 'c']);
    // 运行任务类的自定义方法
    $connection->push([\icy8\Queue\test\JobTest::class, 'custom'], '自定义方法');
    // 延迟任务
    $connection->pushDelay(\icy8\Queue\test\JobTest::class, ['a', 'b', 'c'], 3);
  3. 其他说明

    • 核心的两部分是消费和发布,而消费的核心是\icy8\Queue\Executor,所以你完全可以不使用icy8\Queue\Listener

    • 为什么使用symfony/process:主要是涉及到热更新的问题,没想到、没找到其他更好的办法。

    • redis的配置写得比较乱,需要优化。