|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Beanie\Tube; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Beanie\Beanie; |
|
8
|
|
|
use Beanie\Command\CommandFactory; |
|
9
|
|
|
use Beanie\Command\CommandInterface; |
|
10
|
|
|
use Beanie\Command\Response; |
|
11
|
|
|
use Beanie\Exception; |
|
12
|
|
|
use Beanie\Exception\InvalidArgumentException; |
|
13
|
|
|
use Beanie\Exception\NotFoundException; |
|
14
|
|
|
use Beanie\Job\Job; |
|
15
|
|
|
use Beanie\Job\JobFactory; |
|
16
|
|
|
use Beanie\Server\Server; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class Tube implements TubeAwareInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var TubeStatus */ |
|
22
|
|
|
protected $tubeStatus; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Server */ |
|
25
|
|
|
protected $server; |
|
26
|
|
|
|
|
27
|
|
|
/** @var JobFactory */ |
|
28
|
|
|
protected $jobFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** @var CommandFactory */ |
|
31
|
|
|
protected $commandFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $tubeName |
|
35
|
|
|
* @param Server $server |
|
36
|
|
|
* @param JobFactory|null $jobFactory |
|
37
|
|
|
*/ |
|
38
|
16 |
|
public function __construct($tubeName, Server $server, JobFactory $jobFactory = null) |
|
39
|
|
|
{ |
|
40
|
16 |
|
$this->tubeStatus = new TubeStatus(); |
|
41
|
16 |
|
$this->tubeStatus->setCurrentTube($tubeName); |
|
42
|
16 |
|
$this->server = $server; |
|
43
|
|
|
|
|
44
|
16 |
|
$this->jobFactory = $jobFactory ?: JobFactory::instance(); |
|
45
|
16 |
|
$this->commandFactory = CommandFactory::instance(); |
|
46
|
16 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritDoc |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function getTubeStatus() |
|
52
|
|
|
{ |
|
53
|
12 |
|
return $this->tubeStatus; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function transformTubeStatusTo(TubeStatus $tubeStatus, $mode = TubeStatus::TRANSFORM_USE) |
|
60
|
|
|
{ |
|
61
|
2 |
|
$this->getTubeStatus()->transformTo($tubeStatus, $mode); |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Job|null If no job in the ready state was found, null is returned |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function peekReady() |
|
68
|
|
|
{ |
|
69
|
3 |
|
return $this->peek($this->commandFactory->create(CommandInterface::COMMAND_PEEK_READY)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return Job|null If no job in the delayed state was found, null is returned |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function peekDelayed() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->peek($this->commandFactory->create(CommandInterface::COMMAND_PEEK_DELAYED)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return Job|null If no job in the buried state was found, null is returned |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function peekBuried() |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->peek($this->commandFactory->create(CommandInterface::COMMAND_PEEK_BURIED)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param CommandInterface $command |
|
90
|
|
|
* @return Job|null |
|
91
|
|
|
* @throws Exception\InvalidArgumentException |
|
92
|
|
|
*/ |
|
93
|
5 |
|
protected function peek(CommandInterface $command) |
|
94
|
|
|
{ |
|
95
|
5 |
|
$this->sync(); |
|
96
|
|
|
|
|
97
|
5 |
|
return $this->jobFactory->createFromCommand( |
|
98
|
5 |
|
$command, |
|
99
|
5 |
|
$this->server |
|
100
|
5 |
|
)->invoke(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
9 |
|
protected function sync() |
|
104
|
|
|
{ |
|
105
|
9 |
|
$this->server->transformTubeStatusTo($this->getTubeStatus(), TubeStatus::TRANSFORM_USE); |
|
106
|
9 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param int $howMany |
|
110
|
|
|
* @return int |
|
111
|
|
|
* @throws InvalidArgumentException |
|
112
|
|
|
*/ |
|
113
|
3 |
|
public function kick($howMany = Beanie::DEFAULT_MAX_TO_KICK) |
|
114
|
|
|
{ |
|
115
|
3 |
|
$this->checkConstraints($howMany, 1, 'Kick requires a strictly positive number of jobs to kick'); |
|
116
|
1 |
|
$this->sync(); |
|
117
|
|
|
|
|
118
|
1 |
|
return (int) $this->executeCommand(CommandInterface::COMMAND_KICK, [$howMany])->getData(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return array |
|
123
|
|
|
* @throws NotFoundException If this tube has no data, i.e. it does not exist. |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function stats() |
|
126
|
|
|
{ |
|
127
|
1 |
|
$this->sync(); |
|
128
|
|
|
|
|
129
|
1 |
|
return (array) $this |
|
130
|
1 |
|
->executeCommand(CommandInterface::COMMAND_STATS_TUBE, [$this->getTubeStatus()->getCurrentTube()]) |
|
131
|
1 |
|
->getData(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function getName() |
|
138
|
|
|
{ |
|
139
|
1 |
|
return $this->tubeStatus->getCurrentTube(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int $howLong |
|
144
|
|
|
* @return bool |
|
145
|
|
|
* @throws InvalidArgumentException |
|
146
|
|
|
* @throws NotFoundException If this tube does not exist, it cannot be paused. |
|
147
|
|
|
*/ |
|
148
|
3 |
|
public function pause($howLong) |
|
149
|
|
|
{ |
|
150
|
3 |
|
$this->checkConstraints($howLong, 0, 'Must pause for 0 or more seconds'); |
|
151
|
2 |
|
$this->sync(); |
|
152
|
|
|
|
|
153
|
2 |
|
return $this |
|
154
|
2 |
|
->executeCommand(CommandInterface::COMMAND_PAUSE_TUBE, [$this->getTubeStatus()->getCurrentTube(), $howLong]) |
|
155
|
2 |
|
->getName() == Response::RESPONSE_PAUSED; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
6 |
|
private function checkConstraints($actual, $minimum, $message) |
|
159
|
|
|
{ |
|
160
|
6 |
|
if (!is_int($actual) || $actual < $minimum) { |
|
161
|
3 |
|
throw new InvalidArgumentException($message); |
|
162
|
|
|
} |
|
163
|
3 |
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $command |
|
167
|
|
|
* @param array $arguments |
|
168
|
|
|
* @return Response |
|
169
|
|
|
*/ |
|
170
|
4 |
|
private function executeCommand($command, $arguments = []) |
|
171
|
|
|
{ |
|
172
|
4 |
|
return $this->server |
|
173
|
4 |
|
->dispatchCommand($this->commandFactory->create($command, $arguments)) |
|
174
|
4 |
|
->invoke(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|