1 | <?php |
||
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 | 6 | public function __construct(array $config = []) |
|
47 | |||
48 | 6 | public function setPrefix() |
|
52 | |||
53 | |||
54 | 6 | public function getPrefix() |
|
59 | |||
60 | 1 | public function setWaiting() |
|
61 | { |
||
62 | 1 | $this->waiting = Yii::$app->queue->redis->llen("$this->prefix.waiting"); |
|
63 | 1 | } |
|
64 | |||
65 | 1 | public function getWaiting() |
|
66 | { |
||
67 | 1 | $this->setWaiting(); |
|
68 | 1 | return $this->waiting; |
|
69 | } |
||
70 | |||
71 | 1 | public function setDelayed() |
|
72 | { |
||
73 | 1 | $this->delayed = Yii::$app->queue->redis->zcount("$this->prefix.delayed", '-inf', '+inf'); |
|
74 | 1 | } |
|
75 | |||
76 | 1 | public function getDelayed() |
|
77 | { |
||
78 | 1 | $this->setDelayed(); |
|
79 | 1 | return $this->delayed; |
|
80 | } |
||
81 | |||
82 | 1 | public function setReserved() |
|
83 | { |
||
84 | 1 | $this->reserved = Yii::$app->queue->redis->zcount("$this->prefix.reserved", '-inf', '+inf'); |
|
85 | 1 | } |
|
86 | |||
87 | 1 | public function getReserved() |
|
88 | { |
||
89 | 1 | $this->setReserved(); |
|
90 | 1 | 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 | 1 | } |
|
97 | |||
98 | 1 | public function getTotal() |
|
99 | { |
||
100 | 1 | $this->setTotal(); |
|
101 | 1 | return $this->total; |
|
102 | } |
||
103 | |||
104 | 1 | public function setDone() |
|
105 | { |
||
106 | 1 | $this->done = $this->total - $this->waiting - $this->delayed - $this->reserved; |
|
107 | 1 | } |
|
108 | |||
109 | 1 | public function getDone() |
|
110 | { |
||
111 | 1 | $this->setDone(); |
|
112 | 1 | return $this->done; |
|
113 | } |
||
114 | |||
115 | 1 | public function getWorkInfo() |
|
116 | { |
||
117 | 1 | $workers = []; |
|
118 | 1 | $data = Yii::$app->queue->redis->clientList(); |
|
119 | 1 | foreach (explode("\n", trim($data)) as $line) { |
|
120 | 1 | $client = []; |
|
121 | 1 | foreach (explode(' ', trim($line)) as $pair) { |
|
122 | 1 | list($key, $value) = explode('=', $pair, 2); |
|
123 | 1 | $client[$key] = $value; |
|
124 | 1 | } |
|
125 | |||
126 | 1 | if (isset($client['name']) && strpos($client['name'], Yii::$app->queue->channel . '.worker') === 0) { |
|
127 | $workers[$client['name']] = $client; |
||
128 | } |
||
129 | 1 | } |
|
130 | 1 | return $workers; |
|
131 | } |
||
132 | |||
133 | public function getWaitContent() |
||
138 | |||
139 | public function getReservedContent() |
||
143 | |||
144 | public function getDelayedContent() |
||
148 | |||
149 | 1 | public function getMessage($id) |
|
150 | { |
||
151 | 1 | $message = Yii::$app->queue->redis->hget("$this->prefix.messages", $id); |
|
152 | 1 | $strMessage = ltrim($message, '300;'); |
|
153 | 1 | return $this->serializer->unserialize($strMessage); |
|
154 | } |
||
155 | |||
156 | public function getAttempt($id) |
||
162 | |||
163 | |||
164 | /** 得到执行时间 |
||
165 | * @param $id todo |
||
166 | */ |
||
167 | public function getExecutionTime($id) |
||
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 | 1 | } elseif (Yii::$app->queue->redis->hexists("$this->prefix.messages", $id)) { |
|
180 | 1 | return self::STATUS_WAITING; |
|
181 | } else { |
||
182 | return self::STATUS_DONE; |
||
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.