-
Notifications
You must be signed in to change notification settings - Fork 10
/
SqlQueue.php
136 lines (113 loc) · 3.58 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
<?php
namespace wh\queue;
use yii\db\Connection;
use yii\db\Query;
use yii\db\Expression;
use Yii;
class SqlQueue extends Queue
{
/**
* @var string Default database connection component name
*/
public $connection = 'db';
/**
* @var string Default queue table namespace
*/
public $default = 'default';
private $_query;
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;
}
if ($schema->columns['id']->comment!=='1.0.0') {
$this->dropTable();
return false;
}
return true;
}
private function createTable()
{
$this->connection->createCommand()->createTable($this->getTableName(), [
'id' => 'pk COMMENT "1.0.0"',
'queue' => 'string(255)',
'run_at' => 'timestamp default CURRENT_TIMESTAMP 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';
}
protected function pushInternal($payload, $queue = null, $options = [])
{
if (isset($options['run_at']) && ($options['run_at'] instanceof \DateTime)) {
$run_at=$options['run_at'];
} else {
$run_at=new \DateTime;
}
$this->connection->schema->insert($this->getTableName(), [
'queue' => $this->getQueue($queue),
'payload' => $payload,
'run_at' => new Expression('FROM_UNIXTIME(:unixtime)', [
':unixtime' => $run_at->format('U')
])
]);
$payload = json_decode($payload, true);
return $payload['id'];
}
protected function getQueueInternal($queue = null)
{
return ($queue ?: $this->default);
}
private function getQuery($queue)
{
if ($this->_query) {
return $this->_query;
}
$this->_query=new Query;
$this->_query->select('id, payload')
->from($this->getTableName())
->where(array('queue'=>$queue))
->andWhere('run_at <= NOW()')
->limit(1);
return $this->_query;
}
private function deleteQueue($id)
{
$this->connection->createCommand()->delete($this->getTableName(), 'id=:id', [':id'=>$id])->execute();
}
public function popInternal($queue = null)
{
$row=$this->getQuery($this->getQueue($queue))->one($this->connection);
if ($row) {
$this->deleteQueue($row['id']);
return new Job($this, $row['payload'], $queue);
}
return null;
}
}