bjornbasar/karhu-queue 问题修复 & 功能扩展

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

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

bjornbasar/karhu-queue

Composer 安装命令:

composer require bjornbasar/karhu-queue

包简介

Minimal queue/worker abstraction for the karhu PHP microframework

README 文档

README

Minimal queue/worker abstraction for the karhu PHP microframework.

Install

composer require bjornbasar/karhu-queue bjornbasar/karhu-db

Push jobs

use Karhu\Queue\DatabaseQueue;
use Karhu\Db\Connection;

$db = new Connection('sqlite:jobs.db');
$queue = new DatabaseQueue($db);

$queue->push('SendEmail', ['to' => 'bjorn@example.com', 'subject' => 'Hello']);

Process jobs

use Karhu\Queue\Worker;

$worker = new Worker($queue);
$worker->register('SendEmail', function (array $data) {
    mail($data['to'], $data['subject'], 'Body here');
});
$worker->run(); // loops until stopped

Schema

CREATE TABLE jobs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    queue VARCHAR(50) NOT NULL DEFAULT 'default',
    job VARCHAR(255) NOT NULL,
    data TEXT NOT NULL DEFAULT '{}',
    status VARCHAR(20) NOT NULL DEFAULT 'pending',
    error TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

updated_at tracks the last status transition (v0.2.0+). push() relies on the INSERT default; pop(), complete(), and fail() each bump it. This is what unstick() keys off to detect stuck-in-processing rows.

complete() and fail() are guarded by WHERE id = :id AND status = 'processing' so a row that's already been reset (e.g. by unstick()) is never silently flipped to completed/failed by a stale handler.

Stuck-job recovery

When a worker is SIGKILL'd mid-job (OOM, host reboot, docker kill) the row stays in processing forever — Worker's try/catch only catches handler exceptions, not no-exception process death. QueueInterface::unstick() is the recovery path. Run it on a cron:

// In your CLI:
$reset = $queue->unstick(300);          // reset rows stuck >5 min
$reset = $queue->unstick(300, 'mail');  // scope to one queue

It returns the count of rows reset. The UPDATE's WHERE clause (status='processing' AND updated_at < cutoff) IS the dedup — a live worker that completes a row between the cron's snapshot and the UPDATE will have flipped status='completed', so the row simply no longer matches. No race window vs. a completing worker.

Callers MUST ensure handlers are idempotent. "Stuck" means "no status transition in N seconds", NOT "definitely dead". A slow live handler whose wall time exceeds the threshold WILL be unstuck and re-popped while still executing. Pick the threshold to safely exceed your slowest handler's wall time (5× safety factor recommended). Handler idempotency is the load-bearing contract that makes recovery safe.

Custom drivers

Implement QueueInterface for Redis, RabbitMQ, etc.

Concurrency

DatabaseQueue::pop() is atomic on PostgreSQL (9.5+) at the claim step. Two concurrent workers will never claim the same row: each worker's SELECT locks one pending row with FOR UPDATE SKIP LOCKED inside a transaction, and a second worker's SELECT skips the locked row and either claims the next pending one or returns null.

-- v0.3.0+ on PostgreSQL
SELECT * FROM jobs
  WHERE queue = :queue AND status = 'pending'
  ORDER BY id ASC
  LIMIT 1 FOR UPDATE SKIP LOCKED;
-- then UPDATE … SET status='processing' … inside the same txn.

Driver detection is automatic (constructor reads PDO::ATTR_DRIVER_NAME); no caller change needed when moving from SQLite tests to PG production.

FIFO is best-effort: under sequence rollback or any other source of non-monotonic id, SKIP LOCKED does not re-evaluate ORDER BY id after the lock is taken, so a row with a smaller id inserted concurrently can be claimed after a row with a larger id. For typical use (monotonic SERIAL/AUTOINCREMENT, FIFO-by-insertion semantics) this is invisible.

Other drivers

  • SQLite: pop() falls back to the v0.2 shape (SELECT then UPDATE, no FOR UPDATE — SQLite rejects the syntax). SQLite's single-writer engine makes the race practically rare but not eliminated. Recommendation: single worker per queue on SQLite.
  • MySQL: same fallback as SQLite. MySQL 8.0+ supports FOR UPDATE SKIP LOCKED but karhu-queue v0.3 doesn't enable the suffix (no test surface). Single-worker until a future release.

Caller-owned transactions

pop() opens its own transaction only if PDO::inTransaction() is false. If you call pop() inside an outer transaction, the row lock is held by your outer txn and released on your commit/rollback — pop() will not commit or rollback the outer txn.

bjornbasar/karhu-queue 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-14