DB Driver

DB driver uses database to store queue data.

Configuration example:

return [
    'bootstrap' => [
        'queue', // The component registers own console commands
    'components' => [
        'db' => [
            'class' => \yii\db\Connection::class, 
            // ...
        'queue' => [
            'class' => \yii\queue\db\Queue::class,
            'db' => 'db', // DB connection component or its config 
            'tableName' => '{{%queue}}', // Table name
            'channel' => 'default', // Queue channel key
            'mutex' => \yii\mutex\MysqlMutex::class, // Mutex that used to sync queries

You have to add a table to the database. Example schema for MySQL:

CREATE TABLE `queue` (
  `channel` varchar(255) NOT NULL,
  `job` blob NOT NULL,
  `pushed_at` int(11) NOT NULL,
  `ttr` int(11) NOT NULL,
  `delay` int(11) NOT NULL DEFAULT 0,
  `priority` int(11) unsigned NOT NULL DEFAULT 1024,
  `reserved_at` int(11) DEFAULT NULL,
  `attempt` int(11) DEFAULT NULL,
  `done_at` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `channel` (`channel`),
  KEY `reserved_at` (`reserved_at`),
  KEY `priority` (`priority`)

Migrations are available from src/drivers/db/migrations.

To add migrations to your application, edit console config file to configure the namespaced migration:

'controllerMap' => [
    // ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => null,
        'migrationNamespaces' => [
            // ...

Then issue migration command:

yii migrate/up


Console command is used to execute tasks.

yii queue/listen [timeout]

listen command launches a daemon which infinitely queries the queue. If there are new tasks they're immediately obtained and executed. timeout parameter is number of seconds to sleep between querying a queue next time. This method is most efficient when command is properly daemonized via supervisor or systemd.

yii queue/run

run command obtains and executes tasks in a loop until queue is empty. Works well with cron.

run and listen commands have options:

  • --verbose, -v: print executing statuses into console.
  • --isolate: verbose mode of a job execute. If enabled, execute result of each job will be printed.
  • --color: highlighting for verbose mode.
yii queue/info

info command prints out information about queue status.

yii queue/clear

clear command clears a queue.

yii queue/remove [id]

remove command removes a job.