PHPackages                             ed-smartass/yii2-queue-worker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. ed-smartass/yii2-queue-worker

ActiveYii2-extension[Queues &amp; Workers](/categories/queues)

ed-smartass/yii2-queue-worker
=============================

Adding ability to start and manage yii2-queue workers

1.0.8(3y ago)071MITPHPCI passing

Since Apr 25Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/ed-smartass/yii2-queue-worker)[ Packagist](https://packagist.org/packages/ed-smartass/yii2-queue-worker)[ RSS](/packages/ed-smartass-yii2-queue-worker/feed)WikiDiscussions master Synced today

READMEChangelog (9)Dependencies (2)Versions (13)Used By (0)

Yii2 Queue Worker
=================

[](#yii2-queue-worker)

[![CI](https://github.com/ed-smartass/yii2-queue-worker/actions/workflows/ci.yml/badge.svg)](https://github.com/ed-smartass/yii2-queue-worker/actions)[![License](https://camo.githubusercontent.com/09a961b6b992d767415a1ddc01af2afd82a1b69671ed33cc4d8b3f3d97a85544/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f65642d736d6172746173732f796969322d71756575652d776f726b6572)](LICENSE)

[Русская версия](README.ru.md)

A Yii2 extension for starting, stopping, and monitoring [yii2-queue](https://github.com/yiisoft/yii2-queue) workers directly from your application — via code or a built-in web UI.

Worker processes are tracked in a database table with PID, component name, heartbeat timestamp, and current job ID. The extension supports graceful shutdown via POSIX signals (Ctrl+C), in-process auto-restart with crash-loop protection, and a health-check console command for cron-based monitoring.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Yii2 &gt;= 2.0.14
- yii2-queue &gt;= 2.0
- `pcntl` extension (recommended, for signal handling)
- `posix` extension (recommended, for process checks)

Installation
------------

[](#installation)

```
composer require ed-smartass/yii2-queue-worker
```

### Apply Migrations

[](#apply-migrations)

```
php yii migrate --migrationPath=@vendor/ed-smartass/yii2-queue-worker/migrations
```

Or add to your console config:

```
return [
    'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                '@console/migrations',
                '@vendor/ed-smartass/yii2-queue-worker/migrations',
            ],
        ],
    ],
];
```

### Add Behavior to Queue

[](#add-behavior-to-queue)

```
return [
    'components' => [
        'queue' => [
            'class' => 'yii\queue\db\Queue',
            // ...
            'as worker' => [
                'class' => 'Smartass\Yii2QueueWorker\QueueWorkerBehavior',
                // Optional configuration:
                // 'timeout' => 3,
                // 'phpPath' => '/usr/bin/php',
                // 'yiiPath' => '@app/../yii',
                // 'params' => '--verbose --color',
                // 'minRestartUptime' => 10,
            ],
        ],
    ],
];
```

### Add Web Module (Optional)

[](#add-web-module-optional)

```
return [
    'modules' => [
        'queue-worker' => [
            'class' => 'Smartass\Yii2QueueWorker\module\Module',
            // 'db' => 'db',
            // 'table' => '{{%queue_worker}}',
        ],
    ],
];
```

The web UI will be available at `/queue-worker`.

Configuration
-------------

[](#configuration)

### Behavior Properties

[](#behavior-properties)

PropertyTypeDefaultDescription`table`string`{{%queue_worker}}`Database table name`db`string/Connection`db`Database connection`yiiPath`string`@app/../yii`Path to Yii console entry script`timeout`int`3`Queue listen timeout (seconds)`params`string`--verbose --color`CLI arguments for queue/listen`phpPath`string`php`Path to PHP binary`minRestartUptime`int`10`Minimum uptime (seconds) before a crashed worker is auto-restartedUsage
-----

[](#usage)

### Starting Workers

[](#starting-workers)

From code:

```
// Start a worker using behavior
Yii::$app->queue->start();

// Start with static method
QueueWorkerBehavior::startComponent('queue', timeout: 3);
```

### Stopping Workers

[](#stopping-workers)

```
// Stop a specific worker
Yii::$app->queue->stop(workerId: 42);

// Stop multiple workers
Yii::$app->queue->stop([42, 43, 44]);

// Stop all workers for this component
Yii::$app->queue->stop();

// Stop using static method
QueueWorkerBehavior::stopComponent('queue');
```

When stopping, the extension:

1. Marks workers as `stopped` in the database
2. Sends `SIGTERM` to each worker process (if `posix` extension is available)
3. Workers exit gracefully after completing the current job

### Health Check (Cron)

[](#health-check-cron)

The `WorkerController` checks if registered workers are still alive and restarts dead ones:

```
php yii worker/check
```

Add to console config:

```
return [
    'controllerMap' => [
        'worker' => [
            'class' => 'Smartass\Yii2QueueWorker\controllers\WorkerController',
        ],
    ],
];
```

Recommended cron entry:

```
* * * * * php /path/to/yii worker/check

```

### Signal Handling

[](#signal-handling)

If the `pcntl` extension is installed, workers handle these signals:

- **SIGINT** (Ctrl+C) — marks the worker as stopped, exits after the current loop
- **SIGTERM** — same graceful shutdown behavior

On signal, the worker record is updated in the database and the process terminates cleanly — no zombie records left behind.

### Auto-Restart

[](#auto-restart)

If a worker exits unexpectedly (not via a manual stop or signal), the behavior launches a new worker process in place. To avoid crash loops, the behavior only restarts workers whose uptime was at least `minRestartUptime` seconds (default `10`). Workers that die sooner than that — and workers with a missing or unparseable `started_at` — are logged as a warning and **not** auto-restarted: the DB record is cleaned up and the worker must be brought back by an external supervisor (`systemd`, `supervisord`) or by re-issuing `start()` manually.

Note that the `worker/check` cron command is designed to recover workers whose process died **without** triggering `onWorkerStop()` (e.g. hard kill, segfault). It does not recover workers skipped by the crash-loop guard above, because those records are deleted as part of the normal stop event. Use an external supervisor if you need tight in-process crash-loop recovery.

This deliberately does not try to count restarts across processes: each spawned worker is a fresh PHP process with its own memory.

Architecture
------------

[](#architecture)

```
┌─────────────────────────────────────┐
│         Queue Component             │
│  (yii\queue\db\Queue, etc.)        │
│                                     │
│  ┌─────────────────────────────┐    │
│  │   QueueWorkerBehavior       │    │
│  │   - onWorkerStart()         │    │
│  │   - onWorkerLoop()          │    │
│  │   - onWorkerStop()          │    │
│  │   - onBeforeExec()          │    │
│  │   - onAfterExec()           │    │
│  └──────────┬──────────────────┘    │
└─────────────┼───────────────────────┘
              │
              ▼
┌─────────────────────────────────────┐
│       queue_worker table            │
│  worker_id | pid | component |      │
│  stopped | queue_id | started_at |  │
│  looped_at                          │
└─────────────────────────────────────┘

```

**Event flow:**

1. `EVENT_WORKER_START` → registers PID and signal handlers
2. `EVENT_WORKER_LOOP` → dispatches signals, checks stop flag, updates heartbeat
3. `EVENT_BEFORE_EXEC` → records current job ID
4. `EVENT_AFTER_EXEC` → clears job ID
5. `EVENT_WORKER_STOP` → deletes record, optionally restarts

Development
-----------

[](#development)

```
# Install dependencies
composer install

# Run tests
vendor/bin/phpunit

# Run static analysis
vendor/bin/phpstan analyse
```

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE) for details.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance56

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~160 days

Recently: every ~330 days

Total

10

Last Release

82d ago

Major Versions

1.0.8 → v2.0.x-dev2026-04-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/dfbc1cfb2ad235a55d89d24c99f2e9a772eb62974d2cfdd61f15b973aacfe67f?d=identicon)[ed-smartass](/maintainers/ed-smartass)

---

Top Contributors

[![ed-smartass](https://avatars.githubusercontent.com/u/62896221?v=4)](https://github.com/ed-smartass "ed-smartass (20 commits)")[![claude](https://avatars.githubusercontent.com/u/81847?v=4)](https://github.com/claude "claude (5 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (1 commits)")

---

Tags

queueyii2workerqueue-worker

### Embed Badge

![Health badge](/badges/ed-smartass-yii2-queue-worker/health.svg)

```
[![Health](https://phpackages.com/badges/ed-smartass-yii2-queue-worker/health.svg)](https://phpackages.com/packages/ed-smartass-yii2-queue-worker)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.0k](/packages/craftcms-cms)[trntv/yii2-command-bus

Yii2 Command Bus extension

57660.0k8](/packages/trntv-yii2-command-bus)[ignatenkovnikita/yii2-queuemanager

Yii2 Queue Manager

2063.4k2](/packages/ignatenkovnikita-yii2-queuemanager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
