|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Beanie; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Beanie\Command\CommandFactory; |
|
8
|
|
|
use Beanie\Command\CommandInterface; |
|
9
|
|
|
use Beanie\Exception\TimedOutException; |
|
10
|
|
|
use Beanie\Job\Job; |
|
11
|
|
|
use Beanie\Job\JobFactory; |
|
12
|
|
|
use Beanie\Job\JobOath; |
|
13
|
|
|
use Beanie\Server\Server; |
|
14
|
|
|
use Beanie\Server\TubeAwareTrait; |
|
15
|
|
|
use Beanie\Tube\TubeAwareInterface; |
|
16
|
|
|
use Beanie\Tube\TubeStatus; |
|
17
|
|
|
|
|
18
|
|
|
class Worker implements TubeAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use TubeAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Server */ |
|
23
|
|
|
protected $server; |
|
24
|
|
|
|
|
25
|
|
|
/** @var CommandFactory */ |
|
26
|
|
|
protected $commandFactory; |
|
27
|
|
|
|
|
28
|
|
|
/** @var JobFactory */ |
|
29
|
|
|
protected $jobFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Server $server |
|
33
|
|
|
*/ |
|
34
|
16 |
|
public function __construct(Server $server) |
|
35
|
|
|
{ |
|
36
|
16 |
|
$this->server = $server; |
|
37
|
16 |
|
$this->tubeStatus = new TubeStatus(); |
|
38
|
16 |
|
$this->commandFactory = CommandFactory::instance(); |
|
39
|
16 |
|
$this->jobFactory = JobFactory::instance(); |
|
40
|
16 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $tubeName |
|
44
|
|
|
* @param bool|false $onlyThisTube |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function watch($tubeName, $onlyThisTube = false) |
|
48
|
|
|
{ |
|
49
|
3 |
|
if ($onlyThisTube) { |
|
50
|
1 |
|
$this->tubeStatus->setWatchedTubes([$tubeName]); |
|
51
|
1 |
|
} else { |
|
52
|
2 |
|
$this->tubeStatus->addWatchedTube($tubeName); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $tubeName |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function ignore($tubeName) |
|
63
|
|
|
{ |
|
64
|
1 |
|
$this->tubeStatus->removeWatchedTube($tubeName); |
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @throws Exception\InvalidArgumentException |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function quit() |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->server->dispatchCommand($this->commandFactory->create(CommandInterface::COMMAND_QUIT)); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param null|int $timeout |
|
78
|
|
|
* @return Job|null |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function reserve($timeout = null) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$command = CommandInterface::COMMAND_RESERVE; |
|
83
|
3 |
|
$arguments = []; |
|
84
|
|
|
|
|
85
|
3 |
|
if (!is_null($timeout)) { |
|
86
|
2 |
|
$command = CommandInterface::COMMAND_RESERVE_WITH_TIMEOUT; |
|
87
|
2 |
|
$arguments[] = $timeout; |
|
88
|
2 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
3 |
|
return $this->jobFactory->createFromCommand( |
|
92
|
3 |
|
$this->commandFactory->create($command, $arguments), |
|
93
|
3 |
|
$this->server->transformTubeStatusTo($this->getTubeStatus()) |
|
94
|
3 |
|
)->invoke(); |
|
95
|
1 |
|
} catch (TimedOutException $timeOut) { |
|
96
|
1 |
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return JobOath |
|
102
|
|
|
* @throws Exception\InvalidArgumentException |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function reserveOath() |
|
105
|
|
|
{ |
|
106
|
1 |
|
return $this->jobFactory->createFromCommand( |
|
107
|
1 |
|
$this->commandFactory->create(CommandInterface::COMMAND_RESERVE), |
|
108
|
1 |
|
$this->server->transformTubeStatusTo($this->getTubeStatus(), TubeStatus::TRANSFORM_WATCHED) |
|
109
|
1 |
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return Server |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function getServer() |
|
116
|
|
|
{ |
|
117
|
3 |
|
return $this->server; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function __toString() |
|
124
|
|
|
{ |
|
125
|
1 |
|
return (string) $this->server; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
public function reconnect() |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->getServer()->connect(); |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
public function disconnect() |
|
134
|
|
|
{ |
|
135
|
1 |
|
$this->getServer()->disconnect(); |
|
136
|
1 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|