fyre/queue 问题修复 & 功能扩展

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

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

fyre/queue

Composer 安装命令:

composer require fyre/queue

包简介

A Queue library.

README 文档

README

FyreQueue is a free, open-source queue library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/queue

In PHP:

use Fyre\Queue\QueueManager;

Basic Usage

$queueManager = new QueueManager($container, $config);

Default configuration options will be resolved from the "Queue" key in the Config.

Autoloading

It is recommended to bind the QueueManager to the Container as a singleton.

$container->singleton(QueueManager::class);

Any dependencies will be injected automatically when loading from the Container.

$queueManager = $container->use(QueueManager::class);

Methods

Build

Build a Queue.

  • $options is an array containing configuration options.
$queue = $queueManager->build($options);

Queue dependencies will be resolved automatically from the Container.

Clear

Clear all instances and configs.

$queueManager->clear();

Get Config

Set a Queue config.

  • $key is a string representing the Queue key.
$config = $queueManager->getConfig($key);

Alternatively, if the $key argument is omitted an array containing all configurations will be returned.

$config = $queueManager->getConfig();

Has Config

Determine whether a Queue config exists.

  • $key is a string representing the Queue key, and will default to QueueManager::DEFAULT.
$hasConfig = $queueManager->hasConfig($key);

Is Loaded

Determine whether a Queue instance is loaded.

  • $key is a string representing the Queue key, and will default to QueueManager::DEFAULT.
$isLoaded = $queueManager->isLoaded($key);

Push

Push a job to a Queue.

  • $className is a string representing the job class.
  • $arguments is an array containing arguments that will be passed to the job.
  • $options is an array containing options for the Message.
    • config is a string representing the configuration key, and will default to QueueManager::DEFAULT.
    • queue is a string representing the queue name, and will default to QueueManager::DEFAULT.
    • method is a string representing the class method, and will default to "run".
    • delay is a number representing the number of seconds before the job should run, and will default to 0.
    • expires is a number representing the number of seconds after which the job will expire, and will default to 0.
    • retry is a boolean indicating whether the job should be retried if it fails, and will default to true.
    • maxRetries is a number indicating the maximum number of times the job should be retried, and will default to 5.
    • unique is a boolean indicating whether the job should be unique, and will default to false.
$queueManager->push($className, $arguments, $options);

Job dependencies will be resolved automatically from the Container.

Set Config

Set the Queue config.

  • $key is a string representing the Queue key.
  • $options is an array containing configuration options.
$queueManager->setConfig($key, $options);

Unload

Unload a Queue.

  • $key is a string representing the Queue key, and will default to QueueManager::DEFAULT.
$queueManager->unload($key);

Use

Load a shared Queue instance.

  • $key is a string representing the Queue key, and will default to "default".
$queue = $queueManager->use($key);

Queue dependencies will be resolved automatically from the Container.

Queues

You can load a specific queue by specifying the className option of the $options variable above.

Custom queues can be created by extending \Fyre\Queue\Queue, ensuring all below methods are implemented.

Clear

Clear all items from the queue.

  • $queueName is a string representing the queue name, and will default to QueueManager::DEFAULT.
$queue->clear($queueName);

Complete

Mark a job as completed.

$queue->complete($message);

Fail

Mark a job as failed.

$queue->fail($message);

Pop

Pop the last item off the queue.

  • $queueName is a string representing the queue name.
$message = $queue->pop($queueName);

Push

Push a job onto the queue.

$queue->push($message);

Queues

Get all the active queues.

$queues = $queue->queues();

Reset

Reset the queue statistics.

  • $queueName is a string representing the queue name, and will default to QueueManager::DEFAULT.
$queue->reset($queueName);

Stats

Get the statistics for a queue.

  • $queueName is a string representing the queue name, and will default to QueueManager::DEFAULT.
$stats = $queue->stats($queueName);

Redis

The Redis queue can be loaded using custom configuration.

  • $options is an array containing configuration options.
    • className must be set to \Fyre\Queue\Handlers\RedisQueue.
    • host is a string representing the Redis host, and will default to "127.0.0.1".
    • password is a string representing the Redis password
    • port is a number indicating the Redis port, and will default to 6379.
    • database is a string representing the Redis database.
    • timeout is a number indicating the connection timeout.
    • persist is a boolean indicating whether to use a persistent connection, and will default to true.
    • tls is a boolean indicating whether to use a tls connection, and will default to true.
    • ssl is an array containing SSL options.
      • key is a string representing the path to the key file.
      • cert is a string representing the path to the certificate file.
      • ca is a string representing the path to the certificate authority file.
$container->use(Config::class)->set('Queue.redis', $options);

Workers

Workers are long running tasks that will consume and execute jobs from the queue.

use Fyre\Queue\Worker;
  • $container is a Container.
  • $queueManager is a QueueManager.
  • $eventManager is an EventManager.
  • $options is an array containing configuration options.
    • config is a string representing the configuration key, and will default to QueueManager::DEFAULT.
    • queue is a string representing the queue name, and will default to QueueManager::DEFAULT.
    • maxJobs is a number representing the maximum number of jobs to execute, and will default to 0.
    • maxRuntime is a number representing the maximum number of seconds the worker should run, and will default to 0.
    • rest is a number representing the number of microseconds to rest after processing a job, and will default to 10000.
    • sleep is a number representing the number of microseconds to sleep if no jobs are in the queue, and will default to 100000.
$worker = new Worker($container, $queueManager, $eventManager, $options);

Run

Run the worker.

$worker->run();

Messages

Messages are used internally to pass data between the Queue and Worker.

use Fyre\Queue\Message;
  • $options is an array containing options for the message.
    • className is a string representing the job class.
    • arguments is an array containing arguments that will be passed to the job.
    • config is a string representing the configuration key, and will default to QueueManager::DEFAULT.
    • queue is a string representing the queue name, and will default to QueueManager::DEFAULT.
    • method is a string representing the class method, and will default to "run".
    • delay is a number representing the number of seconds before the job should run, and will default to 0.
    • expires is a number representing the number of seconds after which the job will expire, and will default to 0.
    • retry is a boolean indicating whether the job should be retried if it fails, and will default to true.
    • maxRetries is a number indicating the maximum number of times the job should be retried, and will default to 5.
    • unique is a boolean indicating whether the job should be unique, and will default to false.
$message = new Message($options);

Get After

Get the timestamp when the message can be sent.

$after = $message->getTimestamp();

Get Config

Get the message config.

$config = $message->getConfig();

Get Hash

Get the message hash.

$hash = $message->getHash();

Get Queue

Get the message queue.

$queueName = $message->getQueue();

Is Expired

Determine whether the message has expired.

$isExpired = $message->isExpired();

Is Ready

Determine whether the message is ready.

$isReady = $message->isReady();

Is Unique

Determine whether the message is unique.

$isUnique = $message->isUnique();

Is Valid

Determine whether the message is valid.

$isValid = $message->isValid();

Should Retry

Determine whether the message should be retried.

$shouldretry = $message->shouldRetry();

Commands

Stats

Display stats for the queue.

  • --config is a the configuration key, and will default to QueueManager::DEFAULT.
  • --queue is a the queue name, and will default to QueueManager::DEFAULT.
$commandRunner->run('queue:stats', ['--config', 'default', '--queue', 'default']);

Worker

Start a background queue worker.

  • --config is a the configuration key, and will default to QueueManager::DEFAULT.
  • --queue is a the queue name, and will default to QueueManager::DEFAULT.
  • --max-jobs is the maximum number of jobs to execute, and will default to 0.
  • --max-runtime is the maximum number of seconds the worker should run, and will default to 0.
$commandRunner->run('queue:worker', ['--config', 'default', '--queue', 'default', '--max-jobs', '99', '--max-runtime', '60']);

fyre/queue 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-08