Class zhuravljov\yii\queue\db\Driver

Inheritancezhuravljov\yii\queue\db\Driver » zhuravljov\yii\queue\BaseDriver » yii\base\Object
Implementsyii\base\BootstrapInterface
Source Code https://github.com/yiisoft/yii2-queue/blob/master/src/db/Driver.php

Class DbDriver

Protected Methods

Hide inherited methods

Method Description Defined By
getQueue() zhuravljov\yii\queue\BaseDriver

Property Details

Hide inherited properties

$db public property
public \yii\db\Connection|array|string $db 'db'
$mutex public property
public \yii\mutex\Mutex|array|string $mutex 'mutex'
$tableName public property

Table name

public string $tableName '{{%queue}}'

Method Details

Hide inherited methods

__construct() public method
public void __construct ( zhuravljov\yii\queue\Queue $queue, $config = [] )
$queue zhuravljov\yii\queue\Queue
$config

                public function __construct(Queue $queue, $config = [])
{
    $this->_queue = $queue;
    parent::__construct($config);
}

            
bootstrap() public method

public void bootstrap ( $app )
$app

                public function bootstrap($app)
{
    if ($app instanceof \yii\console\Application) {
        $app->controllerMap[$this->queue->id] = [
            'class' => Command::class,
            'queue' => $this->queue,
        ];
    }
}

            
getQueue() protected method
protected zhuravljov\yii\queue\Queue getQueue ( )

                protected function getQueue()
{
    return $this->_queue;
}

            
init() public method

public void init ( )

                public function init()
{
    parent::init();
    $this->db = Instance::ensure($this->db, Connection::class);
    $this->mutex = Instance::ensure($this->mutex, Mutex::class);
}

            
pop() public method

Pops message and job from the storage.

public boolean pop ( &$message, &$job )
$message mixed
$job zhuravljov\yii\queue\Job

                public function pop(&$message, &$job)
{
    $this->mutex->acquire(__CLASS__);
    $message = (new Query())
        ->from($this->tableName)
        ->where(['started_at' => null])
        ->orderBy(['id' => SORT_ASC])
        ->limit(1)
        ->one($this->db);
    if (is_array($message)) {
        $message['started_at'] = time();
        $this->db->createCommand()->update(
            $this->tableName,
            ['started_at' => $message['started_at']],
            ['id' => $message['id']]
        )->execute();
    }
    $this->mutex->release(__CLASS__);
    if (is_array($message)) {
        $job = unserialize($message['job']);
        return true;
    } else {
        return false;
    }
}

            
purge() public method

Purges the storage.

public void purge ( )

                public function purge()
{
    $this->mutex->acquire(__CLASS__);
    $this->db->createCommand()->delete(
        $this->tableName,
        ['started_at' => null]
    )->execute();
    $this->mutex->release(__CLASS__);
}

            
push() public method

Pushes job to the storage.

public mixed push ( $job )
$job zhuravljov\yii\queue\Job
return mixed

$message

                public function push($job)
{
    $this->db->createCommand()->insert(
        $this->tableName,
        ['job' => serialize($job), 'created_at' => time()]
    )->execute();
    return (new Query())
        ->from($this->tableName)
        ->where(['id' => $this->db->lastInsertID])
        ->one($this->db);
}

            
release() public method

Releases the message.

public void release ( $message )
$message

                public function release($message)
{
    $this->db->createCommand()->update(
        $this->tableName,
        ['finished_at' => time()],
        ['id' => $message['id']]
    )->execute();
}