-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRedisQueue.php
53 lines (44 loc) · 1.34 KB
/
RedisQueue.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
<?php
namespace wh\queue;
use Yii;
use yii\base\InvalidConfigException;
use yii\redis\Connection;
class RedisQueue extends Queue
{
/**
* @var string Default redis component name
*/
public $redis = 'redis';
/**
* Class initialization logic
*
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if (is_string($this->redis)) {
$this->redis = Yii::$app->get($this->redis);
} elseif (is_array($this->redis)) {
$this->redis = Yii::createObject($this->redis);
}
if (!$this->redis instanceof Connection) {
throw new InvalidConfigException("Queue::redis must be either a Redis connection instance or the application component ID of a Redis connection.");
}
}
protected function pushInternal($payload, $queue = null, $options = [])
{
$this->redis->rpush($this->getQueue($queue), $payload);
$payload = json_decode($payload, true);
return $payload['id'];
}
public function popInternal($queue = null)
{
$payload = $this->redis->lpop($this->getQueue($queue));
if ($payload) {
//$this->redis->zadd($queue.':reserved', $this->getTime() + 60, $job);
return new Job($this, $payload, $queue);
}
return null;
}
}