forked from AOEpeople/Aoe_Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueProcessorExample.php
executable file
·56 lines (49 loc) · 1.57 KB
/
QueueProcessorExample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* QueueProcessorExample Task
*
* @author Fabrizio Branca
* @since 2016-03-08
*/
class Aoe_Scheduler_Model_Task_QueueProcessorExample
{
/**
* Run
*
* @return string
* @throws Exception
*/
public function run(Aoe_Scheduler_Model_Schedule $schedule)
{
$sqsClient = null;
// in case you're interested how often this was repeated so far...
$currentRepetition = $schedule->getRepetition();
$numberOfMessagesToFetch = 10;
// [...]
$result = $sqsClient->receiveMessage([
'AttributeNames' => ['All'],
'MaxNumberOfMessages' => $numberOfMessagesToFetch,
'QueueUrl' => $queueUrl,
]);
$messages = $result->search('Messages[]');
if ((is_countable($messages) ? count($messages) : 0) == 0) {
$schedule->setStatus(Aoe_Scheduler_Model_Schedule::STATUS_DIDNTDOANYTHING);
return;
}
// process messages
foreach ($messages as $message) {
// do something with the message
// [...]
$sqsClient->deleteMessage([
'QueueUrl' => $queueUrl,
'ReceiptHandle' => $message['ReceiptHandle']
]);
}
if ((is_countable($messages) ? count($messages) : 0) == $numberOfMessagesToFetch) {
// there might be more messages on the queue
$schedule->setStatus(Aoe_Scheduler_Model_Schedule::STATUS_REPEAT);
} else {
$schedule->setStatus(Aoe_Scheduler_Model_Schedule::STATUS_SUCCESS);
}
}
}