/** * Pop the next job off of the queue. * * @param string $queue * @return \Illuminate\Queue\Jobs\Job|null */ public function pop($queue = null) { $job = $this->pheanstalk->watchOnly($this->getQueue($queue))->reserve(0); if ($job instanceof Pheanstalk_Job) { return new BeanstalkdJob($this->container, $this->pheanstalk, $job); } }
/** * Count the current number of messages on the queue. * * @param $queue Queue Name * * @return int Count */ public function getMessagesCurrentCountOnQueue($queue) { try { return $this->queue->statsTube($queue)['current-jobs-ready']; } catch (\Pheanstalk_Exception_ServerException $e) { \PHPUnit_Framework_Assert::fail("queue [{$queue}] not found"); } }
public function Job_Lookup_Word() { $word = $this->job_details->word; $this->status = 'working'; $this->Report(); try { $url = 'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/' . urlencode($word) . '?key=' . DICTIONARY_API_KEY; $contents = @file_get_contents($url); /* * I really don't know how to manipulate very complex XML easily in PHP, * so please ignore the hideous hacks that you are about to see. */ $xml = new SimpleXMLElement($contents); $results = $xml->xpath('/entry_list/entry[1]//dt'); $return = ''; foreach ($results as $node) { /* @var $node SimpleXMLElement */ $string = $node->asXML(); // Please ignore the developer behind the curtain $string = str_replace('<dt>:', '', $string); $string = str_replace(':</dt>', '', $string); $def = trim(strip_tags($string)); // Return the longest definition if (strlen($def) > strlen($return)) { $return = $def; } } } catch (Exception $ex) { $def = "{$word} failed: " . $ex->getMessage(); } // Okay, you can start paying attention again now. $json = json_encode(array('word' => $word, 'def' => $def)); $this->last_word = $word; $this->last_def = $def; echo "\n {$word}: {$return} \n"; $this->redis->lpush('word.defs', $json); $this->redis->ltrim('word.defs', 0, 20); $this->beanstalk->delete($this->job); // Wait 2 seconds so we don't overload the API usleep(2000000); }
/** * Delete a message from the Beanstalk queue. * * @param string $queue * @param string $id * @return void */ public function deleteMessage($queue, $id) { $this->pheanstalk->useTube($this->getQueue($queue))->delete($id); }
/** * Get the number of times the job has been attempted. * * @return int */ public function attempts() { $stats = $this->pheanstalk->statsJob($this->job); return (int) $stats->reserves; }
/** * 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; }
set_exception_handler('handle_exception'); function handle_signal($signal) { switch ($signal) { case SIGINT: throw new Omeka_Job_Worker_InterruptException("Caught SIGINT, shutting down."); break; default: break; } } pcntl_signal(SIGINT, "handle_signal"); $application->bootstrap(array('Logger')); $host = isset($options->host) ? $options->host : '127.0.0.1'; $port = isset($options->port) ? $options->port : 11300; $pheanstalk = new Pheanstalk_Pheanstalk("{$host}:{$port}"); if (isset($options->queue) && $options->queue != 'default') { $pheanstalk->watch($options->queue)->ignore('default'); } // Reserving a job BEFORE bootstrapping the database will ensure that there are // never any MySQL timeout issues and help prevent any number of other database // usage-related problems. $pheanJob = $pheanstalk->reserve(); if (!$pheanJob) { // Timeouts can occur when reserving a job, so this must be taken // into account. No cause for alarm. echo "Beanstalk worker timed out when reserving a job."; exit(0); } $application->bootstrap(array('Config', 'Db', 'Filederivatives', 'Locale', 'Options', 'Pluginbroker', 'Plugins', 'Jobs', 'Storage', 'Mail', 'View')); // resend() must send jobs to the original queue by default.
<?php /** * Created by JetBrains PhpStorm. * User: jcarmony * Date: 5/2/13 * Time: 12:10 AM * To change this template use File | Settings | File Templates. */ require_once '../vendor/autoload.php'; $beanstalk = new Pheanstalk_Pheanstalk('127.0.0.1'); $words = $argv; unset($words[0]); foreach ($words as $word) { $data = new stdClass(); $data->type = 'lookup_word'; $data->word = $word; $beanstalk->useTube('queue')->put(json_encode($data)); echo "Added {$word} \n"; } echo "Done!\n";
/** * Kick jobs * - server * - tube * - count : number of jobs to kick */ public function kickAction() { $server = $this->_getParam("server"); $tube = $this->_getParam("tube"); $count = $this->_getParam("count", 1); try { // Connect to the server $messageQueue = new Pheanstalk_Pheanstalk($server); $messageQueue->useTube($tube); $messageQueue->kick($count); $response = ""; } catch (Exception $e) { $this->getResponse()->setHttpResponseCode(500); $response = $e->getMessage(); } // Send Json response $this->jsonHelper->sendJson($response); $this->jsonHelper->getResponse()->sendResponse(); }
public function releaseJob(JobInterface $job) { $this->pheanstalk->release($job->getPheanstalkJob()); }
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); }
/** * tells pheanstalk to use a certain tube * * @param $tube * @return $this */ public function useTube($tube) { $this->pheanstalk->useTube($tube); return $this; }