1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueue\Worker; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Worker runner manager has capabilities of managing workers. |
11
|
|
|
* |
12
|
|
|
* @author Tomasz Struczyński <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class WorkerRunnerManager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Worker runner services configuration. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $runners = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Register runner. |
25
|
|
|
* |
26
|
|
|
* @param string $name Worker name. |
27
|
|
|
* @param WorkerRunnerInterface $runner Worker runner. |
28
|
|
|
* @param array $options Worker options. |
29
|
|
|
*/ |
30
|
|
|
public function addRunner($name, WorkerRunnerInterface $runner, array $options = array()) |
31
|
|
|
{ |
32
|
|
|
$this->runners[$name] = array( |
33
|
|
|
'runner' => $runner, |
34
|
|
|
'options' => $options, |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return true, if worker with given name is registered, false otherwise. |
40
|
|
|
* |
41
|
|
|
* @param string $name |
42
|
|
|
* @return boolean |
43
|
|
|
*/ |
44
|
|
|
public function has($name) |
45
|
|
|
{ |
46
|
|
|
return array_key_exists($name, $this->runners); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Run worker. |
51
|
|
|
* |
52
|
|
|
* @param string $name |
53
|
|
|
* @param OutputInterface $output |
54
|
|
|
* @throws InvalidArgumentException Thrown, if worker cannto be found for provided name. |
55
|
|
|
* @throws Exception Can be thrown, if runner resulted with an error. |
56
|
|
|
*/ |
57
|
|
|
public function run($name, OutputInterface $output = null) |
58
|
|
|
{ |
59
|
|
|
if (!$this->has($name)) { |
60
|
|
|
throw new InvalidArgumentException("No runner service registered for provided name."); |
61
|
|
|
} |
62
|
|
|
/* @var $runner WorkerRunnerInterface */ |
63
|
|
|
$runner = $this->runners[$name]['runner']; |
64
|
|
|
$runner->run($this->runners[$name]['options'], $output); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get registered runners. |
69
|
|
|
* |
70
|
|
|
* @return string[] |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function getRunners() |
73
|
|
|
{ |
74
|
|
|
return array_keys($this->runners); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.