forked from micmorozov/yii2-gearman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dispatcher.php
186 lines (161 loc) · 4.26 KB
/
Dispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace micmorozov\yii2\gearman;
use Psr\Log\LoggerInterface;
class Dispatcher
{
const NORMAL = 0;
const LOW = 1;
const HIGH = 2;
/**
* @var Client
*/
private $client;
/**
* @var Config
*/
private $config;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param Config $config
* @param LoggerInterface|null $logger
*/
public function __construct(Config $config, LoggerInterface $logger = null)
{
$this->setConfig($config);
if (null !== $logger) {
$this->setLogger($logger);
}
}
/**
* @param string $name
* @param mixed $data
* @param int $priority
* @param string $unique
* @return string $jobHandle
*/
public function background($name, $data = null, $priority = self::NORMAL, $unique = null)
{
$client = $this->getClient()->getClient();
if (null !== $this->logger) {
$this->logger->debug("Sent background job \"{$name}\" to GearmanClient");
}
$jobHandle = null;
switch ($priority) {
case self::LOW:
$jobHandle = $client->doLowBackground($name, self::serialize($data), $unique);
break;
case self::HIGH:
$jobHandle = $client->doHighBackground($name, self::serialize($data), $unique);
break;
default:
$jobHandle = $client->doBackground($name, self::serialize($data), $unique);
break;
}
if ($client->returnCode() !== GEARMAN_SUCCESS) {
if (null !== $this->logger) {
$this->logger->error("Bad return code");
}
}
if (null !== $this->logger) {
$this->logger->info("Sent job \"{$jobHandle}\" to GearmanWorker");
}
return $jobHandle;
}
/**
* @param string $name
* @param mixed $data
* @param int $priority
* @param string $unique
* @return mixed
*/
public function execute($name, $data = null, $priority = self::NORMAL, $unique = null)
{
$client = $this->getClient()->getClient();
if (null !== $this->logger) {
$this->logger->debug("Sent job \"{$name}\" to GearmanClient");
}
$result = null;
switch ($priority) {
case self::LOW:
$result = $client->doLow($name, self::serialize($data), $unique);
break;
case self::HIGH:
$result = $client->doHigh($name, self::serialize($data), $unique);
break;
default:
$result = $client->doNormal($name, self::serialize($data), $unique);
break;
}
if ($client->returnCode() !== GEARMAN_SUCCESS) {
if (null !== $this->logger) {
$this->logger->error("Bad return code");
}
}
if (null !== $this->logger) {
$this->logger->debug("Job \"{$name}\" returned {$result}");
}
return unserialize($result);
}
/**
* @param mixed $data
* @return string
*/
private function serialize($data = [])
{
return serialize($data);
}
/**
* @param Client|null $client
* @return $this
*/
public function setClient(Client $client = null)
{
$this->client = $client;
return $this;
}
/**
* @return Client|null
*/
public function getClient()
{
if (null === $this->client) {
$this->setClient(new Client($this->getConfig(), $this->getLogger()));
}
return $this->client;
}
/**
* @param Config $config
* @return $this
*/
public function setConfig(Config $config)
{
$this->config = $config;
return $this;
}
/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @return LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
/**
* @param LoggerInterface $logger
* @return $this
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
return $this;
}
}