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
|
4 |
|
public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $defaultSettings) |
48
|
|
|
{ |
49
|
4 |
|
$this->workers = $gearmanCacheWrapper->getWorkers(); |
50
|
4 |
|
if (isset($defaultSettings['job_prefix'])) { |
51
|
|
|
$this->jobPrefix = $defaultSettings['job_prefix']; |
52
|
|
|
} |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return worker containing a job with $jobName as name |
57
|
|
|
* If is not found, throws JobDoesNotExistException Exception |
58
|
|
|
* |
59
|
|
|
* @param string $jobName Name of job |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
* |
63
|
|
|
* @throws JobDoesNotExistException |
64
|
|
|
*/ |
65
|
1 |
|
public function getJob($jobName) |
66
|
|
|
{ |
67
|
1 |
|
$jobName = $this->jobPrefix . $jobName; |
|
|
|
|
68
|
1 |
|
foreach ($this->workers as $worker) { |
69
|
1 |
|
if (is_array($worker['jobs'])) { |
70
|
1 |
|
foreach ($worker['jobs'] as $job) { |
71
|
1 |
|
if ($jobName === $job['realCallableName']) { |
72
|
1 |
|
$worker['job'] = $job; |
73
|
1 |
|
return $worker; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
throw new JobDoesNotExistException(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return worker with $workerName as name and all its jobs |
83
|
|
|
* If is not found, throws WorkerDoesNotExistException Exception |
84
|
|
|
* |
85
|
|
|
* @param string $workerName Name of worker |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
* |
89
|
|
|
* @throws WorkerDoesNotExistException |
90
|
|
|
*/ |
91
|
|
|
public function getWorker($workerName) |
92
|
|
|
{ |
93
|
|
|
foreach ($this->workers as $worker) { |
94
|
|
|
if ($workerName === $worker['callableName']) { |
95
|
|
|
return $worker; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
throw new WorkerDoesNotExistException(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return array of workers |
103
|
|
|
* |
104
|
|
|
* @return array all available workers |
105
|
|
|
*/ |
106
|
1 |
|
public function getWorkers() |
107
|
|
|
{ |
108
|
1 |
|
return $this->workers; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|