|
1
|
|
|
<?php |
|
2
|
|
|
namespace Phloppy\Client; |
|
3
|
|
|
|
|
4
|
|
|
use Phloppy\Job; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Producer client implementation. |
|
8
|
|
|
*/ |
|
9
|
|
|
class Producer extends AbstractClient { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Timeout in ms that disque should block the client while replicating the job. |
|
13
|
|
|
* |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
private $replicationTimeout = 2000; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Replication factor for jobs. |
|
20
|
|
|
* |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
private $replicationFactor = 1; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Enqueue the given job. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $queue |
|
30
|
|
|
* @param Job $job |
|
31
|
|
|
* @param int $maxlen specifies that if there are already count messages queued for the specified queue name, |
|
32
|
|
|
* the message is refused and an error reported to the client. |
|
33
|
|
|
* @oaram bool $async asks the server to let the command return ASAP and replicate the job to other nodes in the |
|
34
|
|
|
* background. The job gets queued ASAP, while normally the job is put into the queue only when |
|
35
|
|
|
* the client gets a positive reply. |
|
36
|
|
|
* |
|
37
|
|
|
* @return Job The updated job (e.g. ID set). |
|
38
|
|
|
*/ |
|
39
|
17 |
|
public function addJob($queue, Job $job, $maxlen = 0, $async = false) |
|
40
|
|
|
{ |
|
41
|
|
|
$command = [ |
|
42
|
17 |
|
'ADDJOB', |
|
43
|
17 |
|
$queue, |
|
44
|
17 |
|
$job->getBody(), |
|
45
|
17 |
|
$this->getReplicationTimeout(), |
|
46
|
17 |
|
'REPLICATE', $this->getReplicationFactor(), |
|
47
|
17 |
|
'DELAY', $job->getDelay(), |
|
48
|
17 |
|
'RETRY', $job->getRetry(), |
|
49
|
17 |
|
'TTL', $job->getTtL(), |
|
50
|
17 |
|
]; |
|
51
|
|
|
|
|
52
|
17 |
|
if ($maxlen) { |
|
53
|
2 |
|
$command[] = 'MAXLEN'; |
|
54
|
2 |
|
$command[] = (int) $maxlen; |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
17 |
|
if ($async) { |
|
58
|
1 |
|
$command[] = 'ASYNC'; |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
17 |
|
$id = $this->send($command); |
|
62
|
16 |
|
$job->setId($id); |
|
63
|
16 |
|
$job->setQueue($queue); |
|
64
|
16 |
|
return $job; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
18 |
|
public function getReplicationTimeout() |
|
71
|
|
|
{ |
|
72
|
18 |
|
return $this->replicationTimeout; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param int $replicationTimeout |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function setReplicationTimeout($replicationTimeout) |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->replicationTimeout = $replicationTimeout; |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return int |
|
85
|
|
|
*/ |
|
86
|
18 |
|
public function getReplicationFactor() |
|
87
|
|
|
{ |
|
88
|
18 |
|
return $this->replicationFactor; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int $replicationFactor |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function setReplicationFactor($replicationFactor) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$this->replicationFactor = $replicationFactor; |
|
97
|
1 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|