seatgeek/djjob
最新稳定版本:1.0.0
Composer 安装命令:
composer require seatgeek/djjob
包简介
Allows PHP web applications to process long-running tasks asynchronously
关键字:
README 文档
README
DJJob allows PHP web applications to process long-running tasks asynchronously. It is a PHP port of delayed_job (developed at Shopify), which has been used in production at SeatGeek since April 2010.
Like delayed_job, DJJob uses a jobs table for persisting and tracking pending, in-progress, and failed jobs.
Requirements
- PHP5
- PDO (Ships with PHP >= 5.1)
- (Optional) PCNTL library
Setup
Import the sql database table.
mysql db < jobs.sql The jobs table structure looks like:
CREATE TABLE `jobs` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `handler` TEXT NOT NULL, `queue` VARCHAR(255) NOT NULL DEFAULT 'default', `attempts` INT UNSIGNED NOT NULL DEFAULT 0, `run_at` DATETIME NULL, `locked_at` DATETIME NULL, `locked_by` VARCHAR(255) NULL, `failed_at` DATETIME NULL, `error` TEXT NULL, `created_at` DATETIME NOT NULL ) ENGINE = INNODB;
You may need to use BLOB as the column type for
handlerif you are passing in serialized blobs of data instead of record ids. For more information, see this link This may be the case for errors such as the following:unserialize(): Error at offset 2010 of 2425 bytes
Tell DJJob how to connect to your database:
DJJob::configure([ 'driver' => 'mysql', 'host' => '127.0.0.1', 'dbname' => 'djjob', 'user' => 'root', 'password' => 'topsecret', ]);
Usage
Jobs are PHP objects that respond to a method perform. Jobs are serialized and stored in the database.
<?php // Job class class HelloWorldJob { public function __construct($name) { $this->name = $name; } public function perform() { echo "Hello {$this->name}!\n"; } } // enqueue a new job DJJob::enqueue(new HelloWorldJob("delayed_job"));
Unlike delayed_job, DJJob does not have the concept of task priority (not yet at least). Instead, it supports multiple queues. By default, jobs are placed on the "default" queue. You can specifiy an alternative queue like:
DJJob::enqueue(new SignupEmailJob("dev@seatgeek.com"), "email");
At SeatGeek, we run an email-specific queue. Emails have a sendLater method which places a job on the email queue. Here's a simplified version of our base Email class:
class Email { public function __construct($recipient) { $this->recipient = $recipient; } public function send() { // do some expensive work to build the email: geolocation, etc.. // use mail api to send this email } public function perform() { $this->send(); } public function sendLater() { DJJob::enqueue($this, "email"); } }
Because Email has a perform method, all instances of the email class are also jobs.
Running the jobs
Running a worker is as simple as:
$worker = new DJWorker($options); $worker->start();
Initializing your environment, connecting to the database, etc. is up to you. We use symfony's task system to run workers, here's an example of our jobs:worker task:
<?php class jobsWorkerTask extends sfPropelBaseTask { protected function configure() { $this->namespace = 'jobs'; $this->name = 'worker'; $this->briefDescription = ''; $this->detailedDescription = <<<EOF The [jobs:worker|INFO] task runs jobs created by the DJJob system. Call it with: [php symfony jobs:worker|INFO] EOF; $this->addArgument('application', sfCommandArgument::OPTIONAL, 'The application name', 'customer'); $this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'); $this->addOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'); $this->addOption('queue', null, sfCommandOption::PARAMETER_REQUIRED, 'The queue to pull jobs from', 'default'); $this->addOption('count', null, sfCommandOption::PARAMETER_REQUIRED, 'The number of jobs to run before exiting (0 for unlimited)', 0); $this->addOption('sleep', null, sfCommandOption::PARAMETER_REQUIRED, 'Seconds to sleep after finding no new jobs', 5); } protected function execute($arguments = array(), $options = array()) { // Database initialization $databaseManager = new sfDatabaseManager($this->configuration); $connection = Propel::getConnection($options['connection'] ? $options['connection'] : ''); $worker = new DJWorker($options); $worker->start(); } }
The worker will exit if the database has any connectivity problems. We use god to manage our workers, including restarting them when they exit for any reason.
Changes
- Eliminated Propel dependency by switching to PDO
seatgeek/djjob 适用场景与选型建议
seatgeek/djjob 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 222.62k 次下载、GitHub Stars 达 249, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「task」 「job」 「asynchronous」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 seatgeek/djjob 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 seatgeek/djjob 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 seatgeek/djjob 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a possibility to auto-setup crontabs required for project based on configuration file.
SlimQ Enables You to push jobs to background workers
PHP client for Kue priority job queue API
Expose the DDev executable commands to the Robo task runner.
A lightweight task queue on top of rybakit/phive-queue
Laravel Relevance Ensurer
统计信息
- 总下载量: 222.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 249
- 点击次数: 23
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2026-01-04