Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make reserve faster on DbQueue #281

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/drivers/db/InfoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function getWaiting()
->from($this->queue->tableName)
->andWhere(['channel' => $this->queue->channel])
->andWhere(['reserved_at' => null])
->andWhere(['delay' => 0]);
->andWhere(['<=', 'execute_at', time()]);
}

/**
Expand All @@ -65,7 +65,7 @@ protected function getDelayed()
->from($this->queue->tableName)
->andWhere(['channel' => $this->queue->channel])
->andWhere(['reserved_at' => null])
->andWhere(['>', 'delay', 0]);
->andWhere(['>', 'execute_at', time()]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/db/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function pushMessage($message, $ttr, $delay, $priority)
'job' => $message,
'pushed_at' => time(),
'ttr' => $ttr,
'delay' => $delay,
'execute_at' => time() + $delay,
'priority' => $priority ?: 1024,
])->execute();
$tableSchema = $this->db->getTableSchema($this->tableName);
Expand All @@ -185,7 +185,7 @@ protected function reserve()
$payload = (new Query())
->from($this->tableName)
->andWhere(['channel' => $this->channel, 'reserved_at' => null])
->andWhere('[[pushed_at]] <= :time - [[delay]]', [':time' => time()])
->andWhere('[[execute_at]] <= :time', [':time' => time()])
->orderBy(['priority' => SORT_ASC, 'id' => SORT_ASC])
->limit(1)
->one($this->db);
Expand Down
80 changes: 80 additions & 0 deletions src/drivers/db/migrations/M181024151600DelayToExecuteAt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yii\queue\db\migrations;

use yii\db\Expression;
use yii\db\Migration;

/**
* Example of migration for queue message storage.
*
* @author Roman Zhuravlev <[email protected]>
*/
class M181024151600DelayToExecuteAt extends Migration
{
public $tableName = '{{%queue}}';

public function up()
{
$this->dropIndex('channel', $this->tableName, 'channel');
if ($this->db->driverName !== 'sqlite') {
$this->addColumn($this->tableName, 'execute_at', $this->integer()->notNull()->after('ttr'));
$this->update($this->tableName, [
'execute_at' => new Expression('[[pushed_at]] + [[delay]]')
]);
$this->dropColumn($this->tableName, 'delay');
} else {
$this->dropTable($this->tableName);
$this->createTable($this->tableName, [
'id' => $this->primaryKey(),
'channel' => $this->string()->notNull(),
'job' => $this->binary()->notNull(),
'pushed_at' => $this->integer()->notNull(),
'ttr' => $this->integer()->notNull(),
'execute_at' => $this->integer()->notNull(),
'priority' => $this->integer()->unsigned()->notNull()->defaultValue(1024),
'reserved_at' => $this->integer(),
'attempt' => $this->integer(),
'done_at' => $this->integer(),
]);
$this->createIndex('reserved_at', $this->tableName, 'reserved_at');
$this->createIndex('priority', $this->tableName, 'priority');
}
$this->createIndex('idx_queue__channel__execute_at', $this->tableName, ['channel', 'execute_at']);
}

public function down()
{
if ($this->db->driverName !== 'sqlite') {
$this->dropIndex('idx_queue__channel__execute_at', $this->tableName);
$this->addColumn($this->tableName, 'delay', $this->integer()->notNull()->after('ttr'));
$this->update($this->tableName, [
'delay' => new Expression('[[execute_at]] - [[pushed_at]]')
]);
$this->dropColumn($this->tableName, 'execute_at');
} else {
$this->dropTable($this->tableName);
$this->createTable($this->tableName, [
'id' => $this->primaryKey(),
'channel' => $this->string()->notNull(),
'job' => $this->binary()->notNull(),
'pushed_at' => $this->integer()->notNull(),
'ttr' => $this->integer()->notNull(),
'delay' => $this->integer()->notNull(),
'priority' => $this->integer()->unsigned()->notNull()->defaultValue(1024),
'reserved_at' => $this->integer(),
'attempt' => $this->integer(),
'done_at' => $this->integer(),
]);
$this->createIndex('reserved_at', $this->tableName, 'reserved_at');
$this->createIndex('priority', $this->tableName, 'priority');
}
$this->createIndex('channel', $this->tableName, 'channel');
}
}