minor changes
This commit is contained in:
56
worker.php
56
worker.php
@@ -1,8 +1,9 @@
|
||||
<?php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use Pheanstalk\Pheanstalk;
|
||||
|
||||
$pheanstalk = Pheanstalk::create('127.0.0.1');
|
||||
/** @var Pheanstalk $pheanstalk */
|
||||
$pheanstalk = require_once "bstd.php";
|
||||
|
||||
// we want jobs from 'testtube' only.
|
||||
$pheanstalk->watch('ExampleTube');
|
||||
@@ -11,24 +12,39 @@ $pheanstalk->watch('ExampleTube');
|
||||
|
||||
//for($i = 0; $i < 1000; $i++) {
|
||||
while (true) {
|
||||
$job = $pheanstalk->reserve();
|
||||
try {
|
||||
$jobPayload = $job->getData();
|
||||
$jobPayload = json_decode($jobPayload);
|
||||
echo date('Y-m-d H:i:s') . ' ' . $jobPayload->id . PHP_EOL;
|
||||
$job = null;
|
||||
|
||||
//usleep(100);
|
||||
//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);
|
||||
//sleep(2);
|
||||
try {
|
||||
$job = $pheanstalk->reserveWithTimeout(3);
|
||||
|
||||
// eventually we're done, delete job.
|
||||
$pheanstalk->delete($job);
|
||||
} catch (\Exception $e) {
|
||||
// handle exception.
|
||||
// and let some other worker retry.
|
||||
$pheanstalk->release($job);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user