1
|
|
|
<?php |
2
|
|
|
namespace Da\Mailer\Queue\Backend\Beanstalkd; |
3
|
|
|
|
4
|
|
|
use Da\Mailer\Exception\InvalidCallException; |
5
|
|
|
use Da\Mailer\Queue\Backend\MailJobInterface; |
6
|
|
|
use Da\Mailer\Queue\Backend\QueueStoreAdapterInterface; |
7
|
|
|
use Pheanstalk\Job as PheanstalkJob; |
8
|
|
|
use Pheanstalk\Pheanstalk; |
9
|
|
|
use phpseclib\Crypt\Random; |
10
|
|
|
|
11
|
|
|
class BeanstalkdQueueStoreAdapter implements QueueStoreAdapterInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string the queue name |
15
|
|
|
*/ |
16
|
|
|
protected $queueName; |
17
|
|
|
/** |
18
|
|
|
* @var int the time to run. Defaults to Pheanstalkd::DEFAULT_TTR. |
19
|
|
|
*/ |
20
|
|
|
protected $timeToRun; |
21
|
|
|
/** |
22
|
|
|
* @var BeanstalkdQueueStoreConnection |
23
|
|
|
*/ |
24
|
|
|
protected $connection; |
25
|
|
|
/** |
26
|
|
|
* @var int Reserves/locks a ready job in a watched tube. A timeout value of 0 will cause the server to immediately |
27
|
|
|
* return either a response or TIMED_OUT. A positive value of timeout will limit the amount of time the client will |
28
|
|
|
* block on the reserve request until a job becomes available. |
29
|
|
|
* |
30
|
|
|
* We highly recommend a non-zero value. Defaults to 5. |
31
|
|
|
*/ |
32
|
|
|
protected $reserveTimeout; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* BeanstalkdQueueStoreAdapter constructor. |
36
|
|
|
* |
37
|
|
|
* @param BeanstalkdQueueStoreConnection $connection |
38
|
|
|
* @param string $queueName |
39
|
|
|
* @param int $timeToRun |
40
|
|
|
* @param int $reserveTimeOut |
41
|
|
|
*/ |
42
|
4 |
|
public function __construct( |
43
|
|
|
BeanstalkdQueueStoreConnection $connection, |
44
|
|
|
$queueName = 'mail_queue', |
45
|
|
|
$timeToRun = Pheanstalk::DEFAULT_TTR, |
46
|
|
|
$reserveTimeOut = 5 |
47
|
|
|
) { |
48
|
4 |
|
$this->connection = $connection; |
49
|
4 |
|
$this->queueName = $queueName; |
50
|
4 |
|
$this->timeToRun = $timeToRun; |
51
|
4 |
|
$this->reserveTimeout = $reserveTimeOut; |
52
|
4 |
|
$this->init(); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return BeanstalkdQueueStoreAdapter |
57
|
|
|
*/ |
58
|
4 |
|
public function init() |
59
|
|
|
{ |
60
|
4 |
|
$this->getConnection()->connect(); |
61
|
|
|
|
62
|
4 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return BeanstalkdQueueStoreConnection |
67
|
|
|
*/ |
68
|
4 |
|
public function getConnection() |
69
|
|
|
{ |
70
|
4 |
|
return $this->connection; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param BeanstalkdMailJob|MailJobInterface $mailJob |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
3 |
|
public function enqueue(MailJobInterface $mailJob) |
79
|
|
|
{ |
80
|
3 |
|
$timestamp = $mailJob->getTimeToSend(); |
|
|
|
|
81
|
3 |
|
$payload = $this->createPayload($mailJob); |
82
|
3 |
|
$delay = (int) max(Pheanstalk::DEFAULT_DELAY, $timestamp - time()); |
83
|
|
|
|
84
|
3 |
|
return $this->getConnection() |
85
|
3 |
|
->getInstance() |
86
|
3 |
|
->useTube($this->queueName) |
87
|
3 |
|
->put($payload, Pheanstalk::DEFAULT_PRIORITY, $delay, $this->timeToRun); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return BeanstalkdMailJob|null |
92
|
|
|
*/ |
93
|
3 |
|
public function dequeue() |
94
|
|
|
{ |
95
|
3 |
|
$job = $this->getConnection()->getInstance()->watch($this->queueName)->reserve($this->reserveTimeout); |
96
|
3 |
|
if ($job instanceof PheanstalkJob) { |
97
|
3 |
|
$data = json_decode($job->getData(), true); |
98
|
|
|
|
99
|
3 |
|
return new BeanstalkdMailJob( |
100
|
|
|
[ |
101
|
3 |
|
'id' => $data['id'], |
102
|
3 |
|
'attempt' => $data['attempt'], |
103
|
3 |
|
'message' => $data['message'], |
104
|
3 |
|
'pheanstalkJob' => $job, |
105
|
|
|
] |
106
|
3 |
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param BeanstalkdMailJob|MailJobInterface $mailJob |
114
|
|
|
*/ |
115
|
4 |
|
public function ack(MailJobInterface $mailJob) |
116
|
|
|
{ |
117
|
4 |
|
if ($mailJob->isNewRecord()) { |
118
|
1 |
|
throw new InvalidCallException('BeanstalkdMailJob cannot be a new object to be acknowledged'); |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
$pheanstalk = $this->getConnection()->getInstance()->useTube($this->queueName); |
122
|
3 |
|
if ($mailJob->isCompleted()) { |
123
|
2 |
|
$pheanstalk->delete($mailJob->getPheanstalkJob()); |
|
|
|
|
124
|
2 |
|
} else { |
125
|
1 |
|
$timestamp = $mailJob->getTimeToSend(); |
|
|
|
|
126
|
1 |
|
$delay = max(0, $timestamp - time()); |
127
|
|
|
|
128
|
|
|
// add back to the queue as it wasn't completed maybe due to some transitory error |
129
|
|
|
// could also be failed. |
130
|
1 |
|
$pheanstalk->release($mailJob->getPheanstalkJob(), Pheanstalk::DEFAULT_PRIORITY, $delay); |
|
|
|
|
131
|
|
|
} |
132
|
3 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
2 |
|
public function isEmpty() |
139
|
|
|
{ |
140
|
2 |
|
$stats = $this->getConnection()->getInstance()->statsTube($this->queueName); |
141
|
|
|
|
142
|
2 |
|
return (int) $stats->current_jobs_delayed === 0 |
143
|
2 |
|
&& (int) $stats->current_jobs_urgent === 0 |
144
|
2 |
|
&& (int) $stats->current_jobs_ready === 0; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param BeanstalkdMailJob|MailJobInterface $mailJob |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
3 |
|
protected function createPayload(MailJobInterface $mailJob) |
153
|
|
|
{ |
154
|
3 |
|
return json_encode( |
155
|
|
|
[ |
156
|
3 |
|
'id' => $mailJob->isNewRecord() ? sha1(Random::string(32)) : $mailJob->getId(), |
157
|
3 |
|
'attempt' => $mailJob->getAttempt(), |
158
|
3 |
|
'message' => $mailJob->getMessage(), |
159
|
|
|
] |
160
|
3 |
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: