forked from n0nag0n/simple-job-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_worker.php
30 lines (26 loc) · 894 Bytes
/
example_worker.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
<?php
$Job_Queue = new n0nag0n\Job_Queue('mysql');
$PDO = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'user', 'pass');
$Job_Queue->addQueueConnection($PDO);
$Job_Queue->watchPipeline('some_cool_pipeline_name');
while(true) {
$job = $Job_Queue->getNextJobAndReserve();
// adjust to whatever makes you sleep better at night (for database queues only, beanstalkd does not need this if statement)
if(empty($job)) {
usleep(500000);
continue;
}
echo "Processing {$job['id']}\n";
$payload = json_decode($job['payload'], true);
try {
$result = doSomethingThatDoesSomething($payload);
if($result === true) {
$Job_Queue->deleteJob($job);
} else {
// this takes it out of the ready queue and puts it in another queue that can be picked up and "kicked" later.
$Job_Queue->buryJob($job);
}
} catch(Exception $e) {
$Job_Queue->buryJob($job);
}
}