51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
use Pheanstalk\Pheanstalk;
|
|
|
|
/** @var Pheanstalk $pheanstalk */
|
|
$pheanstalk = require_once "bstd.php";
|
|
|
|
// we want jobs from 'testtube' only.
|
|
$pheanstalk->watch('ExampleTube');
|
|
|
|
// this hangs until a Job is produced.
|
|
|
|
//for($i = 0; $i < 1000; $i++) {
|
|
while (true) {
|
|
$job = null;
|
|
|
|
try {
|
|
$job = $pheanstalk->reserveWithTimeout(3);
|
|
|
|
if (is_null($job))
|
|
throw new Exception("Job is null");
|
|
|
|
$jobPayload = $job->getData();
|
|
$jobPayload = json_decode($jobPayload);
|
|
|
|
echo date('Y-m-d H:i:s') . ' ' . str_pad($jobPayload->id, 5, " ", STR_PAD_LEFT) . ' ' . $jobPayload->date . PHP_EOL;
|
|
//echo date('Y-m-d H:i:s') . ' ' . str_pad($jobPayload->id, 5, " ", STR_PAD_LEFT) . ' ' . json_encode($jobPayload) . PHP_EOL;
|
|
|
|
usleep(mt_rand(1, 10));
|
|
//sleep(2);
|
|
// If it's going to take a long time, periodically
|
|
// tell beanstalk we're alive to stop it rescheduling the job.
|
|
//$pheanstalk->touch($job);
|
|
|
|
// eventually we're done, delete job.
|
|
$pheanstalk->delete($job);
|
|
} catch (\Throwable $e) {
|
|
// handle exception.
|
|
// and let some other worker retry.
|
|
if (!is_null($job))
|
|
$pheanstalk->release($job);
|
|
|
|
if (str_contains($e->getMessage(), 'Connection refused')) {
|
|
echo 'Beanstalkd server is down';
|
|
die();
|
|
} else if (!($e instanceof Exception)) {
|
|
echo $e->getMessage() . PHP_EOL;
|
|
}
|
|
}
|
|
}
|