|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Beanie\Command; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Beanie\Beanie; |
|
8
|
|
|
use Beanie\Command\CommandLineCreator\CommandLineCreatorInterface; |
|
9
|
|
|
use Beanie\Command\CommandLineCreator\GenericCommandLineCreator; |
|
10
|
|
|
use Beanie\Command\CommandLineCreator\PutCommandLineCreator; |
|
11
|
|
|
use Beanie\Command\CommandLineCreator\TubeNameCheckingCommandLineCreator; |
|
12
|
|
|
use Beanie\Command\ResponseParser\GenericResponseParser; |
|
13
|
|
|
use Beanie\Command\ResponseParser\JobResponseParser; |
|
14
|
|
|
use Beanie\Command\ResponseParser\ResponseParserInterface; |
|
15
|
|
|
use Beanie\Command\ResponseParser\SimpleValueResponseParser; |
|
16
|
|
|
use Beanie\Command\ResponseParser\YAMLResponseParser; |
|
17
|
|
|
use Beanie\Exception\DeadlineSoonException; |
|
18
|
|
|
use Beanie\Exception\DrainingException; |
|
19
|
|
|
use Beanie\Exception\ExpectedCRLFException; |
|
20
|
|
|
use Beanie\Exception\InvalidArgumentException; |
|
21
|
|
|
use Beanie\Exception\JobTooBigException; |
|
22
|
|
|
use Beanie\Exception\NotFoundException; |
|
23
|
|
|
use Beanie\Exception\NotIgnoredException; |
|
24
|
|
|
use Beanie\Exception\TimedOutException; |
|
25
|
|
|
use Beanie\Util\FactoryInterface; |
|
26
|
|
|
use Beanie\Util\FactoryTrait; |
|
27
|
|
|
|
|
28
|
|
|
class CommandFactory implements FactoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use FactoryTrait; |
|
31
|
|
|
|
|
32
|
|
|
protected static $defaultCommandStructure = [ |
|
33
|
|
|
'responseParser' => GenericResponseParser::class, |
|
34
|
|
|
'acceptableResponses' => [], |
|
35
|
|
|
'expectedErrorResponses' => [], |
|
36
|
|
|
'commandLineCreator' => GenericCommandLineCreator::class, |
|
37
|
|
|
'argumentDefaults' => [] |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
protected static $commandStructureMap = [ |
|
41
|
|
|
CommandInterface::COMMAND_BURY => [ |
|
42
|
|
|
'acceptableResponses' => [Response::RESPONSE_BURIED], |
|
43
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
44
|
|
|
'argumentDefaults' => [ |
|
45
|
|
|
'jobId' => null, |
|
46
|
|
|
'priority' => Beanie::DEFAULT_PRIORITY |
|
47
|
|
|
] |
|
48
|
|
|
], |
|
49
|
|
|
CommandInterface::COMMAND_DELETE => [ |
|
50
|
|
|
'acceptableResponses' => [Response::RESPONSE_DELETED], |
|
51
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
52
|
|
|
'argumentDefaults' => [ |
|
53
|
|
|
'jobId' => null |
|
54
|
|
|
] |
|
55
|
|
|
], |
|
56
|
|
|
CommandInterface::COMMAND_IGNORE => [ |
|
57
|
|
|
'responseParser' => SimpleValueResponseParser::class, |
|
58
|
|
|
'acceptableResponses' => [Response::RESPONSE_WATCHING], |
|
59
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_IGNORED], |
|
60
|
|
|
'commandLineCreator' => TubeNameCheckingCommandLineCreator::class, |
|
61
|
|
|
'argumentDefaults' => [ |
|
62
|
|
|
'tubeName' => null |
|
63
|
|
|
] |
|
64
|
|
|
], |
|
65
|
|
|
CommandInterface::COMMAND_KICK => [ |
|
66
|
|
|
'responseParser' => SimpleValueResponseParser::class, |
|
67
|
|
|
'acceptableResponses' => [Response::RESPONSE_KICKED], |
|
68
|
|
|
'argumentDefaults' => [ |
|
69
|
|
|
'howMany' => Beanie::DEFAULT_MAX_TO_KICK |
|
70
|
|
|
] |
|
71
|
|
|
], |
|
72
|
|
|
CommandInterface::COMMAND_KICK_JOB => [ |
|
73
|
|
|
'acceptableResponses' => [Response::RESPONSE_KICKED], |
|
74
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
75
|
|
|
'argumentDefaults' => [ |
|
76
|
|
|
'jobId' => null |
|
77
|
|
|
] |
|
78
|
|
|
], |
|
79
|
|
|
CommandInterface::COMMAND_LIST_TUBES => [ |
|
80
|
|
|
'responseParser' => YAMLResponseParser::class, |
|
81
|
|
|
'acceptableResponses' => [Response::RESPONSE_OK] |
|
82
|
|
|
], |
|
83
|
|
|
CommandInterface::COMMAND_LIST_TUBES_WATCHED => [ |
|
84
|
|
|
'responseParser' => YAMLResponseParser::class, |
|
85
|
|
|
'acceptableResponses' => [Response::RESPONSE_OK] |
|
86
|
|
|
], |
|
87
|
|
|
CommandInterface::COMMAND_LIST_TUBE_USED => [ |
|
88
|
|
|
'responseParser' => SimpleValueResponseParser::class, |
|
89
|
|
|
'acceptableResponses' => [Response::RESPONSE_USING] |
|
90
|
|
|
], |
|
91
|
|
|
CommandInterface::COMMAND_PAUSE_TUBE => [ |
|
92
|
|
|
'acceptableResponses' => [Response::RESPONSE_PAUSED], |
|
93
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
94
|
|
|
'commandLineCreator' => TubeNameCheckingCommandLineCreator::class, |
|
95
|
|
|
'argumentDefaults' => [ |
|
96
|
|
|
'tubeName' => null, |
|
97
|
|
|
'howLong' => Beanie::DEFAULT_DELAY |
|
98
|
|
|
] |
|
99
|
|
|
], |
|
100
|
|
|
CommandInterface::COMMAND_PEEK => [ |
|
101
|
|
|
'responseParser' => JobResponseParser::class, |
|
102
|
|
|
'acceptableResponses' => [Response::RESPONSE_FOUND], |
|
103
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
104
|
|
|
'argumentDefaults' => [ |
|
105
|
|
|
'jobId' => null |
|
106
|
|
|
] |
|
107
|
|
|
], |
|
108
|
|
|
CommandInterface::COMMAND_PEEK_BURIED => [ |
|
109
|
|
|
'responseParser' => JobResponseParser::class, |
|
110
|
|
|
'acceptableResponses' => [Response::RESPONSE_FOUND], |
|
111
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND] |
|
112
|
|
|
], |
|
113
|
|
|
CommandInterface::COMMAND_PEEK_DELAYED => [ |
|
114
|
|
|
'responseParser' => JobResponseParser::class, |
|
115
|
|
|
'acceptableResponses' => [Response::RESPONSE_FOUND], |
|
116
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND] |
|
117
|
|
|
], |
|
118
|
|
|
CommandInterface::COMMAND_PEEK_READY => [ |
|
119
|
|
|
'responseParser' => JobResponseParser::class, |
|
120
|
|
|
'acceptableResponses' => [Response::RESPONSE_FOUND], |
|
121
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND] |
|
122
|
|
|
], |
|
123
|
|
|
CommandInterface::COMMAND_PUT => [ |
|
124
|
|
|
'responseParser' => SimpleValueResponseParser::class, |
|
125
|
|
|
'acceptableResponses' => [Response::RESPONSE_INSERTED, Response::RESPONSE_BURIED], |
|
126
|
|
|
'expectedErrorResponses' => [ |
|
127
|
|
|
Response::FAILURE_DRAINING, |
|
128
|
|
|
Response::FAILURE_EXPECTED_CRLF, |
|
129
|
|
|
Response::FAILURE_JOB_TOO_BIG |
|
130
|
|
|
], |
|
131
|
|
|
'commandLineCreator' => PutCommandLineCreator::class, |
|
132
|
|
|
'argumentDefaults' => [ |
|
133
|
|
|
'priority' => Beanie::DEFAULT_PRIORITY, |
|
134
|
|
|
'delay' => Beanie::DEFAULT_DELAY, |
|
135
|
|
|
'timeToRun' => Beanie::DEFAULT_TIME_TO_RUN |
|
136
|
|
|
] |
|
137
|
|
|
], |
|
138
|
|
|
CommandInterface::COMMAND_QUIT => [], |
|
139
|
|
|
CommandInterface::COMMAND_RELEASE => [ |
|
140
|
|
|
'acceptableResponses' => [Response::RESPONSE_BURIED, Response::RESPONSE_RELEASED], |
|
141
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
142
|
|
|
'argumentDefaults' => [ |
|
143
|
|
|
'jobId' => null, |
|
144
|
|
|
'priority' => Beanie::DEFAULT_PRIORITY, |
|
145
|
|
|
'delay' => Beanie::DEFAULT_DELAY |
|
146
|
|
|
] |
|
147
|
|
|
], |
|
148
|
|
|
CommandInterface::COMMAND_RESERVE => [ |
|
149
|
|
|
'responseParser' => JobResponseParser::class, |
|
150
|
|
|
'acceptableResponses' => [Response::RESPONSE_RESERVED], |
|
151
|
|
|
'expectedErrorResponses' => [Response::FAILURE_DEADLINE_SOON] |
|
152
|
|
|
], |
|
153
|
|
|
CommandInterface::COMMAND_RESERVE_WITH_TIMEOUT => [ |
|
154
|
|
|
'responseParser' => JobResponseParser::class, |
|
155
|
|
|
'acceptableResponses' => [Response::RESPONSE_RESERVED], |
|
156
|
|
|
'expectedErrorResponses' => [Response::FAILURE_DEADLINE_SOON, Response::FAILURE_TIMED_OUT], |
|
157
|
|
|
'argumentDefaults' => [ |
|
158
|
|
|
'timeout' => null |
|
159
|
|
|
] |
|
160
|
|
|
], |
|
161
|
|
|
CommandInterface::COMMAND_STATS => [ |
|
162
|
|
|
'responseParser' => YAMLResponseParser::class, |
|
163
|
|
|
'acceptableResponses' => [Response::RESPONSE_OK] |
|
164
|
|
|
], |
|
165
|
|
|
CommandInterface::COMMAND_STATS_JOB => [ |
|
166
|
|
|
'responseParser' => YAMLResponseParser::class, |
|
167
|
|
|
'acceptableResponses' => [Response::RESPONSE_OK], |
|
168
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
169
|
|
|
'argumentDefaults' => [ |
|
170
|
|
|
'jobId' => null |
|
171
|
|
|
] |
|
172
|
|
|
], |
|
173
|
|
|
CommandInterface::COMMAND_STATS_TUBE => [ |
|
174
|
|
|
'responseParser' => YAMLResponseParser::class, |
|
175
|
|
|
'acceptableResponses' => [Response::RESPONSE_OK], |
|
176
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
177
|
|
|
'commandLineCreator' => TubeNameCheckingCommandLineCreator::class, |
|
178
|
|
|
'argumentDefaults' => [ |
|
179
|
|
|
'tubeName' => null |
|
180
|
|
|
] |
|
181
|
|
|
], |
|
182
|
|
|
CommandInterface::COMMAND_TOUCH => [ |
|
183
|
|
|
'acceptableResponses' => [Response::RESPONSE_TOUCHED], |
|
184
|
|
|
'expectedErrorResponses' => [Response::FAILURE_NOT_FOUND], |
|
185
|
|
|
'argumentDefaults' => [ |
|
186
|
|
|
'jobId' => null |
|
187
|
|
|
] |
|
188
|
|
|
], |
|
189
|
|
|
CommandInterface::COMMAND_USE => [ |
|
190
|
|
|
'responseParser' => SimpleValueResponseParser::class, |
|
191
|
|
|
'acceptableResponses' => [Response::RESPONSE_USING], |
|
192
|
|
|
'commandLineCreator' => TubeNameCheckingCommandLineCreator::class, |
|
193
|
|
|
'argumentDefaults' => [ |
|
194
|
|
|
'tubeName' => null |
|
195
|
|
|
] |
|
196
|
|
|
], |
|
197
|
|
|
CommandInterface::COMMAND_WATCH => [ |
|
198
|
|
|
'responseParser' => SimpleValueResponseParser::class, |
|
199
|
|
|
'acceptableResponses' => [Response::RESPONSE_WATCHING], |
|
200
|
|
|
'expectedErrorResponses' => [], |
|
201
|
|
|
'commandLineCreator' => TubeNameCheckingCommandLineCreator::class, |
|
202
|
|
|
'argumentDefaults' => [ |
|
203
|
|
|
'tubeName' => null |
|
204
|
|
|
] |
|
205
|
|
|
] |
|
206
|
|
|
]; |
|
207
|
|
|
|
|
208
|
|
|
protected static $errorResponseExceptionMap = [ |
|
209
|
|
|
Response::FAILURE_NOT_FOUND => NotFoundException::class, |
|
210
|
|
|
Response::FAILURE_NOT_IGNORED => NotIgnoredException::class, |
|
211
|
|
|
Response::FAILURE_DRAINING => DrainingException::class, |
|
212
|
|
|
Response::FAILURE_EXPECTED_CRLF => ExpectedCRLFException::class, |
|
213
|
|
|
Response::FAILURE_JOB_TOO_BIG => JobTooBigException::class, |
|
214
|
|
|
Response::FAILURE_DEADLINE_SOON => DeadlineSoonException::class, |
|
215
|
|
|
Response::FAILURE_TIMED_OUT => TimedOutException::class |
|
216
|
|
|
]; |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string $commandName |
|
220
|
|
|
* @param array $arguments |
|
221
|
|
|
* @return GenericCommand |
|
222
|
|
|
* @throws InvalidArgumentException |
|
223
|
|
|
*/ |
|
224
|
50 |
|
public function create($commandName, array $arguments = []) |
|
225
|
|
|
{ |
|
226
|
50 |
|
if (!isset(self::$commandStructureMap[$commandName])) { |
|
227
|
1 |
|
throw new InvalidArgumentException('Could not create Command for \'' . $commandName . '\''); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
49 |
|
$commandStructure = array_merge(self::$defaultCommandStructure, self::$commandStructureMap[$commandName]); |
|
231
|
|
|
|
|
232
|
49 |
|
return new GenericCommand( |
|
233
|
49 |
|
$this->createLineCreator($commandStructure, $commandName, $arguments), |
|
234
|
49 |
|
$this->createResponseParser($commandStructure) |
|
235
|
49 |
|
); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @param array $commandStructure |
|
240
|
|
|
* @return ResponseParserInterface |
|
241
|
|
|
*/ |
|
242
|
49 |
|
private function createResponseParser($commandStructure) |
|
243
|
|
|
{ |
|
244
|
49 |
|
return new $commandStructure['responseParser']( |
|
245
|
49 |
|
$commandStructure['acceptableResponses'], |
|
246
|
49 |
|
array_intersect_key( |
|
247
|
49 |
|
self::$errorResponseExceptionMap, |
|
248
|
49 |
|
array_flip($commandStructure['expectedErrorResponses']) |
|
249
|
49 |
|
) |
|
250
|
49 |
|
); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param array $commandStructure |
|
255
|
|
|
* @param string $commandName |
|
256
|
|
|
* @param array $arguments |
|
257
|
|
|
* @return CommandLineCreatorInterface |
|
258
|
|
|
*/ |
|
259
|
49 |
|
private function createLineCreator($commandStructure, $commandName, $arguments) |
|
260
|
|
|
{ |
|
261
|
49 |
|
return new $commandStructure['commandLineCreator']( |
|
262
|
49 |
|
$commandName, $arguments, $commandStructure['argumentDefaults'] |
|
263
|
49 |
|
); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|