定制 vaibhavpandeyvpz/qatar 二次开发

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

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

vaibhavpandeyvpz/qatar

Composer 安装命令:

composer require vaibhavpandeyvpz/qatar

包简介

Framework agnostic PHP library for publishing and consuming jobs using Redis and ElasticMQ backends.

README 文档

README

Framework agnostic PHP library for publishing and consuming jobs using Redis and ElasticMQ/SQS backends.

Qatar: कतार (Queue)

Latest Version Build Status Downloads PHP Version License

Features

  • 🚀 Fast: Efficient job processing with Redis or ElasticMQ/SQS backends
  • 🔄 Reliable: Automatic retries with configurable delays
  • Delayed Jobs: Schedule jobs to run in the future
  • 👷 Multiple Workers: Run multiple workers concurrently
  • 🎯 Simple API: Clean, intuitive interface for job management
  • 🔧 Flexible: Framework agnostic - use with any PHP application
  • 📝 Type-safe: Full PHP 8.2+ type hints and modern language features
  • Graceful Shutdown: Workers handle termination signals properly
  • 💾 Two Backends: Choose between Redis (fast) or ElasticMQ/SQS (distributed).
    • Note: Backend-specific drivers (predis or aws-sdk-php) are suggested and must be installed separately.

Requirements

  • PHP 8.2 or higher
  • Redis server + PhpRedis OR predis/predis (for RedisQueue)
  • ElasticMQ or AWS SQS + aws/aws-sdk-php (for ElasticMQQueue)
  • JSON extension (ext-json) for payload serialization (usually enabled by default)

Installation

Install via Composer:

composer require vaibhavpandeyvpz/qatar

Local Development Setup

For local testing, use Docker Compose to run Redis and ElasticMQ:

# Start services
docker-compose up -d

# Verify services are running
docker-compose ps

# Run tests
./vendor/bin/phpunit

This will start:

  • Redis on localhost:6379
  • ElasticMQ on localhost:9324 (SQS-compatible API)
  • ElasticMQ UI on localhost:9325 (monitoring)

Quick Start

Creating a Job

Implementing Qatar\Job is simple:

<?php

use Qatar\Job;

class SendEmailJob extends Job
{
    public function handle(array $payload): void
    {
        // $payload has data passed during push()
        mail($payload['to'], $payload['subject'], $payload['body']);
    }
}

By extending Job, you get default implementations for:

  • retries(): Returns 3
  • delay(): Returns 60 seconds
  • failed(): Empty handler

Override them as needed:

class SendEmailJob extends Job
{
    public function handle(array $payload): void { ... }

    public function failed(\Throwable $exception, array $payload): void
    {
        // Handle permanent failure
    }

    public function retries(): int
    {
        return 5;
    }
}

Publishing Jobs

use Qatar\RedisQueue;

// Create a Redis client (Predis or PhpRedis)
$redis = new Predis\Client('tcp://127.0.0.1:6379');

// Create a queue instance
$queue = new RedisQueue($redis, 'emails');

// Basic push
$queue->push(SendEmailJob::class, [
    'to' => 'user@example.com',
    'subject' => 'Hello!',
    'body' => 'Welcome to Qatar.',
]);

// Delayed push (5 minutes)
$queue->push(SendEmailJob::class, [...], delay: 300);

Running Workers

use Qatar\Worker;
use Qatar\RedisQueue;

$redis = new Predis\Client('tcp://127.0.0.1:6379');
$queue = new RedisQueue($redis, 'emails');
$worker = new Worker($queue);

$worker->work();

API Reference

Qatar\Queue Interface

Method Description
push(string $job, array $payload, ?int $delay = null): string Add a job to the queue. Returns job ID.
pop(?int $timeout = null): ?JobPayload Retrieve next job. Optionally wait for $timeout seconds.
ack(string $id): bool Acknowledge successful completion of a job.
nack(string $id, ?int $delay = null): bool Record a failure and schedule a retry.
size(): int Get total number of pending and delayed jobs.
purge(): void Clear all jobs from the queue.

Qatar\Worker Class

The worker executes jobs from a queue. It is not final, so you can extend it to override resolveJob() for dependency injection.

use Qatar\Worker;
use Qatar\Job;

class ContainerWorker extends Worker
{
    protected function resolveJob(string $jobClass, array $payload): Job
    {
        // Use your DI container here
        return $this->container->get($jobClass);
    }
}

WorkerOptions

Option Default Description
sleep 3 Seconds to wait when the queue is empty.
maxJobs null Max number of jobs to process before stopping.
maxTime null Max seconds to run before stopping.
memory 128 Memory limit in MB. Worker stops if exceeded.
stopOnEmpty false If true, worker quits when queue is empty.

Advanced Usage

Exponential Backoff

Implement custom backoff logic by using the attempts property in the payload:

class BackoffJob extends Job
{
    public function handle(array $payload): void
    {
        // The attempt number is managed by the queue backend
    }

    public function retries(): int { return 5; }

    public function delay(): int
    {
        // Custom logic here
        return 60;
    }
}

Redis SSL/TLS Support

The RedisQueue supports secure connections over SSL/TLS. You can use the rediss:// or tls:// schemes in your connection string:

$queue = new RedisQueue('rediss://your-secure-redis-host:6379');

When using ext-redis, the library automatically handles the tls:// prefix requirement for secure connections.

Signal Handling

Workers gracefully stop when they receive SIGTERM or SIGINT. They finish the current job before exiting.

Monitoring

  • Redis: Use redis-cli MONITOR or LLEN qatar:default:ready.
  • ElasticMQ: Visit http://localhost:9325 for the stats UI.

Testing

Run tests with code coverage:

./vendor/bin/phpunit

License

MIT License. See LICENSE for details.

Author

Vaibhav Pandey

vaibhavpandeyvpz/qatar 适用场景与选型建议

vaibhavpandeyvpz/qatar 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-31