forked from PHPNuts/yii2-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlQueue.php
144 lines (122 loc) · 3.53 KB
/
SqlQueue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace yii\queue;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\db\Query;
use yii\base\Component;
use Yii;
use yii\helpers\Json;
class SqlQueue extends Component implements QueueInterface
{
/**
* @var string Default database connection component name
*/
public $connection = 'db';
/**
* @var string Default queue table namespace
*/
public $default = 'default';
public function init()
{
parent::init();
if (is_string($this->connection)) {
$this->connection = Yii::$app->get($this->connection);
} elseif (is_array($this->connection)) {
if (!isset($this->connection['class'])) {
$this->connection['class'] = Connection::className();
}
$this->connection = Yii::createObject($this->connection);
}
if (!$this->connection instanceof Connection) {
throw new InvalidConfigException('Queue::connection must be application component ID of a SQL connection.');
}
if (!$this->hasTable()) {
$this->createTable();
}
}
private function hasTable()
{
$schema = $this->connection->schema->getTableSchema($this->getTableName(), true);
if ($schema == null) {
return false;
}
return true;
}
private function createTable()
{
$this->connection->createCommand()->createTable($this->getTableName(), [
'id' => 'pk',
'queue' => 'string(255)',
'run_at' => 'INTEGER NOT NULL',
'payload' => 'text',
])->execute();
$this->connection->schema->refresh();
}
public function dropTable()
{
$this->connection->createCommand()->dropTable($this->getTableName())->execute();
}
private function getTableName()
{
return $this->default.'_queue';
}
/**
* {@inheritdoc}
*/
public function push($payload, $queue = null, $delay = 0)
{
$this->connection->schema->insert($this->getTableName(), [
'queue' => $queue,
'payload' => Json::encode($payload),
'run_at' => time() + $delay,
]);
return $this->connection->lastInsertID;
}
private function getQuery($queue)
{
$query = new Query();
$query->from($this->getTableName())
->andFilterWhere(['queue' => $queue])
->andWhere('run_at <= :timestamp', ['timestamp' => time()])
->limit(1);
return $query;
}
/**
* {@inheritdoc}
*/
public function delete(array $message)
{
$this->connection->createCommand()->delete($this->getTableName(), 'id=:id', [':id' => $message['id']])->execute();
}
/**
* {@inheritdoc}
*/
public function pop($queue = null)
{
$row = $this->getQuery($queue)->one($this->connection);
if ($row) {
$row['body'] = Json::decode($row['payload']);
return $row;
}
return false;
}
/**
* {@inheritdoc}
*/
public function purge($queue)
{
$this->connection->createCommand()->delete($this->getTableName(), 'queue=:queue', [':queue' => $queue])->execute();
}
/**
* {@inheritdoc}
*/
public function release(array $message, $delay = 0)
{
$this->connection->createCommand()->update(
$this->getTableName(),
['run_at' => time() + $delay],
'id = :id',
['id' => $message['id']]
)->execute();
}
}