|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace graychen\yii2\queue\backend\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Model; |
|
7
|
|
|
use yii\di\Instance; |
|
8
|
|
|
use yii\queue\serializers\PhpSerializer; |
|
9
|
|
|
use yii\queue\serializers\Serializer; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class RedisQueue |
|
13
|
|
|
* @package graychen\yii2\queue\backend\models |
|
14
|
|
|
* @property integer $total |
|
15
|
|
|
* @property integer $done |
|
16
|
|
|
* @property integer $waiting |
|
17
|
|
|
* @property integer $delayed |
|
18
|
|
|
* @property integer $reserved |
|
19
|
|
|
*/ |
|
20
|
|
|
class RedisQueue extends Model |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @see Queue::isWaiting() |
|
24
|
|
|
*/ |
|
25
|
|
|
const STATUS_WAITING = 1; |
|
26
|
|
|
/** |
|
27
|
|
|
* @see Queue::isReserved() |
|
28
|
|
|
*/ |
|
29
|
|
|
const STATUS_RESERVED = 2; |
|
30
|
|
|
/** |
|
31
|
|
|
* @see Queue::isDone() |
|
32
|
|
|
*/ |
|
33
|
|
|
const STATUS_DONE = 3; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Serializer|array |
|
36
|
|
|
*/ |
|
37
|
|
|
public $serializer = PhpSerializer::class; |
|
38
|
|
|
|
|
39
|
|
|
public $prefix; |
|
40
|
|
|
|
|
41
|
5 |
|
public function __construct(array $config = []) |
|
42
|
|
|
{ |
|
43
|
5 |
|
$this->getPrefix(); |
|
44
|
5 |
|
$this->serializer = Instance::ensure($this->serializer, Serializer::class); |
|
45
|
5 |
|
parent::__construct($config); |
|
46
|
5 |
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
public function setPrefix() |
|
49
|
|
|
{ |
|
50
|
5 |
|
$this->prefix = Yii::$app->queue->channel; |
|
51
|
5 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
5 |
|
public function getPrefix() |
|
55
|
|
|
{ |
|
56
|
5 |
|
$this->setPrefix(); |
|
57
|
5 |
|
return $this->prefix; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function setWaiting() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->waiting = Yii::$app->queue->redis->llen("$this->prefix.waiting"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getWaiting() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->setWaiting(); |
|
68
|
|
|
return $this->waiting; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setDelayed() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->delayed = Yii::$app->queue->redis->zcount("$this->prefix.delayed", '-inf', '+inf'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getDelayed() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->setDelayed(); |
|
79
|
|
|
return $this->delayed; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function setReserved() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->reserved = Yii::$app->queue->redis->zcount("$this->prefix.reserved", '-inf', '+inf'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getReserved() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->setReserved(); |
|
90
|
|
|
return $this->reserved; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function setTotal() |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->total = Yii::$app->queue->redis->get("$this->prefix.message_id"); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function getTotal() |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->setTotal(); |
|
101
|
|
|
return $this->total; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function setDone() |
|
105
|
|
|
{ |
|
106
|
1 |
|
$this->done = $this->total - $this->waiting - $this->delayed - $this->reserved; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public function getDone() |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->setDone(); |
|
112
|
|
|
return $this->done; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
public function getWorkInfo() |
|
116
|
|
|
{ |
|
117
|
1 |
|
$workers = []; |
|
118
|
1 |
|
$data = Yii::$app->queue->redis->clientList(); |
|
119
|
|
|
foreach (explode("\n", trim($data)) as $line) { |
|
120
|
|
|
$client = []; |
|
121
|
|
|
foreach (explode(' ', trim($line)) as $pair) { |
|
122
|
|
|
list($key, $value) = explode('=', $pair, 2); |
|
123
|
|
|
$client[$key] = $value; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (isset($client['name']) && strpos($client['name'], Yii::$app->queue->channel . '.worker') === 0) { |
|
127
|
|
|
$workers[$client['name']] = $client; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
return $workers; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getWaitContent() |
|
134
|
|
|
{ |
|
135
|
|
|
$waitContent = Yii::$app->queue->redis->lrange("$this->prefix.waiting", 0, 10); |
|
136
|
|
|
return $waitContent; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function getReservedContent() |
|
140
|
|
|
{ |
|
141
|
|
|
return Yii::$app->queue->redis->zrange("$this->prefix.reserved", 0, -1); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getDelayedContent() |
|
145
|
|
|
{ |
|
146
|
|
|
return Yii::$app->queue->redis->zrange("$this->prefix.delayed", 0, -1); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
public function getMessage($id) |
|
150
|
|
|
{ |
|
151
|
1 |
|
$message = Yii::$app->queue->redis->hget("$this->prefix.messages", $id); |
|
152
|
|
|
$strMessage = ltrim($message, '300;'); |
|
153
|
|
|
return $this->serializer->unserialize($strMessage); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function getAttempt($id) |
|
157
|
|
|
{ |
|
158
|
|
|
$attempt = Yii::$app->queue->redis->hget("$this->prefix.attempt", $id); |
|
159
|
|
|
$strAttempt = ltrim($attempt, '300;'); |
|
160
|
|
|
return $this->serializer->unserialize($strAttempt); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** 得到执行时间 |
|
165
|
|
|
* @param $id todo |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getExecutionTime($id) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** 得到队列的状态 |
|
172
|
|
|
* @param $id |
|
173
|
|
|
* @return int |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function status($id) |
|
176
|
|
|
{ |
|
177
|
1 |
|
if (Yii::$app->queue->redis->hexists("$this->prefix.attempts", $id)) { |
|
178
|
|
|
return self::STATUS_RESERVED; |
|
179
|
|
|
} elseif (Yii::$app->queue->redis->hexists("$this->prefix.messages", $id)) { |
|
180
|
|
|
return self::STATUS_WAITING; |
|
181
|
|
|
} else { |
|
182
|
|
|
return self::STATUS_DONE; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.