1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Phive Queue package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phive\Queue; |
13
|
|
|
|
14
|
|
|
use Pheanstalk\Exception\ServerException; |
15
|
|
|
use Pheanstalk\PheanstalkInterface; |
16
|
|
|
|
17
|
|
|
class PheanstalkQueue implements Queue |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var PheanstalkInterface |
21
|
|
|
*/ |
22
|
|
|
private $pheanstalk; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $tubeName; |
28
|
|
|
|
29
|
15 |
|
public function __construct(PheanstalkInterface $pheanstalk, $tubeName) |
30
|
|
|
{ |
31
|
15 |
|
$this->pheanstalk = $pheanstalk; |
32
|
15 |
|
$this->tubeName = $tubeName; |
33
|
15 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
14 |
|
public function push($item, $eta = null) |
39
|
|
|
{ |
40
|
14 |
|
$this->pheanstalk->putInTube( |
41
|
14 |
|
$this->tubeName, |
42
|
14 |
|
$item, |
43
|
14 |
|
PheanstalkInterface::DEFAULT_PRIORITY, |
44
|
14 |
|
QueueUtils::calculateDelay($eta) |
45
|
14 |
|
); |
46
|
12 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
11 |
|
public function pop() |
52
|
|
|
{ |
53
|
11 |
|
if (!$item = $this->pheanstalk->reserveFromTube($this->tubeName, 0)) { |
54
|
2 |
|
throw new NoItemAvailableException($this); |
55
|
|
|
} |
56
|
|
|
|
57
|
11 |
|
$this->pheanstalk->delete($item); |
58
|
|
|
|
59
|
11 |
|
return $item->getData(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function count() |
66
|
|
|
{ |
67
|
1 |
|
$stats = $this->pheanstalk->statsTube($this->tubeName); |
68
|
|
|
|
69
|
1 |
|
return $stats['current-jobs-ready']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function clear() |
76
|
|
|
{ |
77
|
1 |
|
$this->doClear('ready'); |
78
|
1 |
|
$this->doClear('buried'); |
79
|
1 |
|
$this->doClear('delayed'); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
protected function doClear($state) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
1 |
|
while ($item = $this->pheanstalk->{'peek'.$state}($this->tubeName)) { |
86
|
1 |
|
$this->pheanstalk->delete($item); |
87
|
1 |
|
} |
88
|
1 |
|
} catch (ServerException $e) { |
|
|
|
|
89
|
|
|
} |
90
|
1 |
|
} |
91
|
|
|
} |
92
|
|
|
|