定制 ez-php/queue 二次开发

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

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

ez-php/queue

Composer 安装命令:

composer require ez-php/queue

包简介

Async job queue for the ez-php framework — database and Redis drivers, Worker, and queue:work command

README 文档

README

Async job queue for the ez-php framework — database and Redis drivers, a Worker, and a queue:work console command.

Requirements

  • PHP 8.5+
  • ext-pdo
  • ez-php/contracts 0.*
  • ez-php/console 0.*
  • ext-redis (Redis driver only)

Installation

composer require ez-php/queue

Setup

Register the service provider:

$app->register(\EzPhp\Queue\QueueServiceProvider::class);

Add the worker command to the CLI:

$app->registerCommand(\EzPhp\Queue\Console\WorkCommand::class);

Add config/queue.php to your application:

return [
    'driver' => env('QUEUE_DRIVER', 'database'),  // 'database' | 'redis'
    'redis'  => [
        'host'     => env('REDIS_HOST', '127.0.0.1'),
        'port'     => (int) env('REDIS_PORT', 6379),
        'database' => (int) env('REDIS_DATABASE', 0),
    ],
];

Defining Jobs

use EzPhp\Queue\Job;

final class SendWelcomeEmail extends Job
{
    protected string $queue    = 'emails';
    protected int    $maxTries = 5;

    public function __construct(private readonly string $email) {}

    public function handle(): void
    {
        // send the email ...
    }

    public function fail(\Throwable $exception): void
    {
        // log or notify on permanent failure
    }
}

Dispatching Jobs

use EzPhp\Contracts\QueueInterface;

$queue = $app->make(QueueInterface::class);
$queue->push(new SendWelcomeEmail('alice@example.com'));

Delay a job by setting $delay (seconds):

final class ProcessReport extends Job
{
    protected int $delay = 60; // available after 60 seconds
    // ...
}

Note: The Redis driver does not enforce $delay. Use the database driver for delayed job delivery.

Running the Worker

php ez queue:work                  # process 'default' queue, sleep 3s on empty
php ez queue:work emails           # process 'emails' queue
php ez queue:work emails --sleep=5 # custom sleep interval
php ez queue:work --max-jobs=100   # stop after 100 jobs (useful for cron-based workers)

Drivers

Database Driver (default)

Stores jobs in a jobs table and failed jobs in failed_jobs. Both tables are created automatically. Requires a configured DatabaseInterface in the container.

Supports delayed delivery via available_at column.

Redis Driver

Uses ext-redis. Jobs are pushed to queues:{name} (RPUSH) and consumed via LPOP (FIFO). Failed jobs are appended to queues:failed:{name}.

Delayed delivery is not enforced — jobs are queued immediately regardless of $delay.

Failed jobs

The database driver stores permanently failed jobs and exposes management commands:

php ez queue:failed list            # list all failed jobs
php ez queue:failed retry {id}      # re-queue a failed job
php ez queue:failed delete {id}     # delete a failed job record
php ez queue:failed flush           # delete all failed jobs

Monitoring

php ez queue:monitor                        # snapshot of queue depths + failed count
php ez queue:monitor --queues=emails,sms    # specific queues
php ez queue:monitor --watch=5              # refresh every 5 seconds

Scheduling

Register recurring jobs in a service provider's boot():

$scheduler = $app->make(\EzPhp\Queue\Scheduling\Scheduler::class);

$scheduler->job(SendDailyReport::class)->daily();
$scheduler->job(PruneTokens::class)->hourly();
$scheduler->job(SyncData::class)->everyMinutes(15);
$scheduler->job(CustomJob::class)->cron('30 6 * * 1');

Run queue:schedule every minute via system cron:

* * * * * php /var/www/html/ez queue:schedule

Classes

Class Description
Job Abstract base class for all jobs
Worker Pops and executes jobs; handles retries and permanent failures
QueueServiceProvider Registers QueueInterface and Worker with the DI container
Driver\DatabaseDriver PDO-backed driver with atomic pop, delayed delivery, and FailedJobRepositoryInterface
Driver\RedisDriver Redis-backed driver via ext-redis
FailedJobRepositoryInterface Contract for failed-job stores: all(), retry(), forget(), flush()
Scheduling\Scheduler Registry of recurring jobs; evaluates due tasks by cron expression
Scheduling\ScheduledTask Fluent builder: daily(), hourly(), everyMinutes(), cron()
Console\WorkCommand queue:work CLI command
Console\MonitorCommand queue:monitor CLI command
Console\FailedCommand queue:failed CLI command
Console\ScheduleRunCommand queue:schedule CLI command
QueueException Base exception for queue errors

Setup (standalone development)

cp .env.example .env
./start.sh

ez-php/queue 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-17