|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Workers; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
6
|
|
|
use Puzzle\AMQP\Consumer; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Psr\Log\NullLogger; |
|
9
|
|
|
|
|
10
|
|
|
class WorkerContext |
|
11
|
|
|
{ |
|
12
|
|
|
use LoggerAwareTrait; |
|
13
|
|
|
|
|
14
|
|
|
const |
|
15
|
|
|
INSTANCES_DEFAULT_VALUE = 1, |
|
16
|
|
|
SERVER_DEFAULT_VALUE = 'worker', |
|
17
|
|
|
IS_DEPLOYMENT_ALLOWED = true; |
|
18
|
|
|
|
|
19
|
|
|
public |
|
20
|
|
|
$queue, |
|
|
|
|
|
|
21
|
|
|
$description, |
|
22
|
|
|
$instances, |
|
23
|
|
|
$servers, |
|
24
|
|
|
$isDeploymentAllowed; |
|
25
|
|
|
|
|
26
|
|
|
private |
|
27
|
|
|
$consumer, |
|
|
|
|
|
|
28
|
|
|
$worker; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(\Closure $worker, Consumer $consumer, $queue) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->worker = $worker; |
|
33
|
|
|
$this->consumer = $consumer; |
|
34
|
|
|
$this->queue = $queue; |
|
35
|
|
|
$this->logger = new NullLogger(); |
|
36
|
|
|
$this->instances = self::INSTANCES_DEFAULT_VALUE; |
|
37
|
|
|
$this->servers = [self::SERVER_DEFAULT_VALUE]; |
|
38
|
|
|
$this->isDeploymentAllowed = true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getWorker() |
|
42
|
|
|
{ |
|
43
|
|
|
if($this->worker instanceof \Closure) |
|
44
|
|
|
{ |
|
45
|
|
|
$closure = $this->worker; |
|
46
|
|
|
$this->worker = $closure(); |
|
47
|
|
|
$this->worker->setLogger($this->logger); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->worker; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function setLogger(LoggerInterface $logger) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->logger = $logger; |
|
56
|
|
|
$this->consumer->setLogger($logger); |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getLogger() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->logger; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getConsumer() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->consumer; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setDescription($description) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->description = $description; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getDescription() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->description; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getQueue() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->queue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function deployInstances($numberOfInstance) |
|
89
|
|
|
{ |
|
90
|
|
|
if(! empty($numberOfInstance)) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->instances = (int) $numberOfInstance; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getInstances() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->instances; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function deployOn($servers) |
|
104
|
|
|
{ |
|
105
|
|
|
if(! empty($servers)) |
|
106
|
|
|
{ |
|
107
|
|
|
if(! is_array($servers)) |
|
108
|
|
|
{ |
|
109
|
|
|
$servers = [$servers]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->servers = $servers; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function getServers() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->servers; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function disableDeployment() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->isDeploymentAllowed = false; |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function isDeploymentAllowed() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->isDeploymentAllowed; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.