solophp/task-queue 问题修复 & 功能扩展

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

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

solophp/task-queue

Composer 安装命令:

composer require solophp/task-queue

包简介

Enhanced PHP task queue using Solo Database with retry mechanism and task expiration support.

README 文档

README

Latest Version on Packagist License PHP Version

A lightweight PHP task queue built on top of PDO.
Supports scheduled execution, retries, task expiration, indexed task types, automatic deletion of completed tasks, and optional process-level locking via LockGuard.

📦 Installation

composer require solophp/task-queue

📋 Requirements

  • PHP: >= 8.2
  • Extensions:
    • ext-json - for JSON payload handling
    • ext-pdo - for database operations
    • ext-posix - for LockGuard process locking (optional)

This package uses standard PHP extensions and has minimal external dependencies. No external database libraries required - works with any PDO-compatible database (MySQL, PostgreSQL, SQLite, etc.).

⚙️ Setup

use Solo\TaskQueue\TaskQueue;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$queue = new TaskQueue($pdo, table: 'tasks', maxRetries: 5, deleteOnSuccess: true);
$queue->install(); // creates the tasks table if not exists

🚀 Usage

Add a task:

$taskId = $queue->addTask(
    'email_notification',
    ['type' => 'email_notification', 'user_id' => 123, 'template' => 'welcome'],
    new DateTimeImmutable('tomorrow') // optional, defaults to now
);

Process all tasks:

$queue->processPendingTasks(function (string $name, array $payload) {
    match ($name) {
        'email_notification' => sendEmail($payload),
        'push_notification' => sendPush($payload),
        default => throw new RuntimeException("Unknown task: $name")
    };
});

Process only specific type:

$queue->processPendingTasks(function (string $name, array $payload) {
    sendEmail($payload);
}, 20, 'email_notification'); // only tasks where payload_type column = 'email_notification'

🔒 Using LockGuard (optional)

use Solo\TaskQueue\LockGuard;

$lockFile = __DIR__ . '/storage/locks/my_worker.lock';
$lock = new LockGuard($lockFile); // path to lock file

if (!$lock->acquire()) {
    exit(0); // Another worker is already running
}

try {
    $queue->processPendingTasks(...);
} finally {
    $lock->release(); // Optional, auto-released on shutdown
}

🧰 Features

  • Task Retries – Configurable max retry attempts before marking as failed
  • Task Expiration – Automatic expiration via expires_at timestamp
  • Indexed Task Types – Fast filtering by payload_type
  • Row-Level Locking – Prevents concurrent execution of the same task
  • Transactional Safety – All task operations are executed within a transaction
  • Optional Process Locking – Prevent overlapping workers using LockGuard
  • Optional Deletion on Success – Set deleteOnSuccess: true to automatically delete tasks after success

🔗 Integration with Event-Dispatcher

TaskQueue implements TaskQueueInterface and can be used as an async queue backend for SoloPHP Event-Dispatcher. The Event-Dispatcher library should contain a TaskQueueAdapter in its adapter collection that implements the integration.

For async event processing setup, refer to the Event-Dispatcher documentation.

🧪 API Methods

Method Description
install() Create the tasks table
addTask(string $name, array $payload, ?DateTimeImmutable $scheduledAt = null, ?DateTimeImmutable $expiresAt = null) Add task to the queue (default schedule: now)
getPendingTasks(int $limit = 10, ?string $onlyType = null) Retrieve ready-to-run tasks, optionally filtered by type
markCompleted(int $taskId) Mark task as completed
markFailed(int $taskId, string $error = '') Mark task as failed with error message
processPendingTasks(callable $callback, int $limit = 10, ?string $onlyType = null) Process pending tasks with a custom handler

🧪 Testing

# Run tests
composer test

# Run code sniffer
composer cs

# Fix code style issues
composer cs-fix

📄 License

This project is open-sourced under the MIT license.

solophp/task-queue 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-04