/** * Bury a job */ public function buryAction() { $server = $this->_getParam("server"); $tube = $this->_getParam("tube"); $jobId = $this->_getParam("id"); try { // Connect to the server $messageQueue = new Pheanstalk_Pheanstalk($server); // Check if the next job in the queue is still the same job $stillExists = false; try { /** @var $nextJob Pheanstalk_Job */ $nextJob = $messageQueue->peekReady($tube); if ($nextJob instanceof Pheanstalk_Job && $nextJob->getId() == $jobId) { $stillExists = true; } } catch (Exception $e) { } if ($stillExists) { // Try to reserve the job /** @var $job Pheanstalk_Job */ $job = $messageQueue->reserveFromTube($tube, 0); // Last check (may have changed since first check) if ($job instanceof Pheanstalk_Job) { // Correct job if ($job->getId() == $jobId) { $messageQueue->bury($job); $response = ""; } else { // Wrong job, un-reserve $messageQueue->release($job); $response = "There was a error while burying the job, try again."; } } else { $this->getResponse()->setHttpResponseCode(500); $response = "No job found in the tube."; } } else { $this->getResponse()->setHttpResponseCode(500); $response = "The job can't be found, maybe it has been deleted, delayed or buried."; } } catch (Exception $e) { $this->getResponse()->setHttpResponseCode(500); $response = $e->getMessage(); } // Send Json response $this->jsonHelper->sendJson($response); $this->jsonHelper->getResponse()->sendResponse(); }
/** * See if we can talk to beanstalkd * */ function CheckBeanstalkd() { global $settings; $ret = false; require_once './lib/beanstalkd/pheanstalk_init.php'; $pheanstalk = new Pheanstalk_Pheanstalk($settings['beanstalkd']); if ($pheanstalk->getConnection()->isServiceListening()) { $id = $pheanstalk->putInTube('wpt.installtest', "test"); $jobStats = $pheanstalk->statsJob($id); $tubeStats = $pheanstalk->statsTube('wpt.installtest'); $job = $pheanstalk->reserveFromTube('wpt.installtest', 0); if ($job !== false && $job->getData() == 'test') { $ret = true; } $pheanstalk->delete($job); } return $ret; }
function ProcessTests($crawl) { global $beanstalkd; $tube = 'har.' . $crawl; $pheanstalk = new Pheanstalk_Pheanstalk($beanstalkd); do { $got_test = true; try { $job = $pheanstalk->reserveFromTube($tube, 0); if ($job !== false) { $id = $job->getData(); if (ProcessTest($id)) { $pheanstalk->delete($job); } else { $pheanstalk->release($job); } } else { $got_test = false; } } catch (Exception $e) { if ($e->getMessage() == 'Server reported NOT_FOUND') { $got_test = false; } else { unset($pheanstalk); sleep(1); $pheanstalk = new Pheanstalk_Pheanstalk($beanstalkd); } } } while ($got_test); unset($pheanstalk); }