1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Gearman Bundle for Symfony2 / Symfony3 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* Feel free to edit as you please, and have fun. |
10
|
|
|
* |
11
|
|
|
* @author Marc Morera <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Mkk\GearmanBundle\Service\Abstracts; |
15
|
|
|
|
16
|
|
|
use Mkk\GearmanBundle\Exceptions\JobDoesNotExistException; |
17
|
|
|
use Mkk\GearmanBundle\Exceptions\WorkerDoesNotExistException; |
18
|
|
|
use Mkk\GearmanBundle\Service\GearmanCacheWrapper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Gearman execute methods. All Worker methods |
22
|
|
|
* |
23
|
|
|
* @author Marc Morera <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractGearmanService |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* All workers |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $workers; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The prefix for all job names |
36
|
|
|
* |
37
|
|
|
* @var string $jobPrefix |
38
|
|
|
*/ |
39
|
|
|
protected $jobPrefix = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct method |
43
|
|
|
* |
44
|
|
|
* @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper |
45
|
|
|
* @param array $defaultSettings The default settings for the bundle |
46
|
|
|
*/ |
47
|
3 |
|
public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings) |
48
|
|
|
{ |
49
|
3 |
|
$this->workers = $gearmanCacheWrapper->getWorkers(); |
50
|
|
|
|
51
|
3 |
|
if (isset($defaultSettings['job_prefix'])) { |
52
|
|
|
|
53
|
|
|
$this->jobPrefix = $defaultSettings['job_prefix']; |
54
|
|
|
} |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return worker containing a job with $jobName as name |
59
|
|
|
* If is not found, throws JobDoesNotExistException Exception |
60
|
|
|
* |
61
|
|
|
* @param string $jobName Name of job |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
* |
65
|
|
|
* @throws JobDoesNotExistException |
66
|
|
|
*/ |
67
|
1 |
|
public function getJob($jobName) |
68
|
|
|
{ |
69
|
1 |
|
$jobName = $this->jobPrefix . $jobName; |
|
|
|
|
70
|
|
|
|
71
|
1 |
|
foreach ($this->workers as $worker) { |
72
|
|
|
|
73
|
1 |
|
if (is_array($worker['jobs'])) { |
74
|
|
|
|
75
|
1 |
|
foreach ($worker['jobs'] as $job) { |
76
|
|
|
|
77
|
1 |
|
if ($jobName === $job['realCallableName']) { |
78
|
|
|
|
79
|
1 |
|
$worker['job'] = $job; |
80
|
|
|
|
81
|
1 |
|
return $worker; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new JobDoesNotExistException(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return worker with $workerName as name and all its jobs |
92
|
|
|
* If is not found, throws WorkerDoesNotExistException Exception |
93
|
|
|
* |
94
|
|
|
* @param string $workerName Name of worker |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
* |
98
|
|
|
* @throws WorkerDoesNotExistException |
99
|
|
|
*/ |
100
|
|
|
public function getWorker($workerName) |
101
|
|
|
{ |
102
|
|
|
foreach ($this->workers as $worker) { |
103
|
|
|
|
104
|
|
|
if ($workerName === $worker['callableName']) { |
105
|
|
|
return $worker; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
throw new WorkerDoesNotExistException(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return array of workers |
114
|
|
|
* |
115
|
|
|
* @return array all available workers |
116
|
|
|
*/ |
117
|
|
|
public function getWorkers() |
118
|
|
|
{ |
119
|
|
|
return $this->workers; |
120
|
|
|
} |
121
|
|
|
} |
|
|
|
|
122
|
|
|
|