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\Generator; |
15
|
|
|
|
16
|
|
|
use Mkk\GearmanBundle\Exceptions\WorkerNameTooLongException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Job Unique Key generator |
20
|
|
|
* |
21
|
|
|
* @see https://github.com/mmoreram/GearmanBundle/issues/66 |
22
|
|
|
*/ |
23
|
|
|
class UniqueJobIdentifierGenerator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
* |
28
|
|
|
* Generate unique key |
29
|
|
|
*/ |
30
|
|
|
protected $generateUniqueKey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Construct method |
34
|
|
|
* |
35
|
|
|
* @param string $generateUniqueKey Generate unique key |
36
|
|
|
*/ |
37
|
5 |
|
public function __construct($generateUniqueKey) |
38
|
|
|
{ |
39
|
5 |
|
$this->generateUniqueKey = $generateUniqueKey; |
40
|
5 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Generate unique key if generateUniqueKey is enabled |
44
|
|
|
* |
45
|
|
|
* Even some parameters are not used, are passed to allow user overwrite |
46
|
|
|
* method |
47
|
|
|
* |
48
|
|
|
* Also, if name and unique value exceeds 114 bytes, an exception is thrown |
49
|
|
|
* |
50
|
|
|
* @param string $name A GermanBundle registered function to be executed |
51
|
|
|
* @param string $params Parameters to send to task as string |
52
|
|
|
* @param string $unique unique ID used to identify a particular task |
53
|
|
|
* @param string $method Method to perform |
54
|
|
|
* |
55
|
|
|
* @return string Generated Unique Key |
56
|
|
|
* |
57
|
|
|
* @throws WorkerNameTooLongException If name is too large |
58
|
|
|
* |
59
|
|
|
* @api |
60
|
|
|
*/ |
61
|
2 |
|
public function generateUniqueKey($name, $params, $unique, $method) |
|
|
|
|
62
|
|
|
{ |
63
|
2 |
|
$unique = !$unique && $this->generateUniqueKey |
|
|
|
|
64
|
1 |
|
? md5($name . $params) |
65
|
2 |
|
: $unique; |
66
|
|
|
|
67
|
2 |
|
if (strlen($name . $unique) > 114) { |
68
|
|
|
|
69
|
|
|
throw new WorkerNameTooLongException; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return $unique; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.