/** * Starts a worker for the PEAR library * * @param array $worker_list List of worker functions to add * @return void * */ protected function start_lib_worker($worker_list) { /** * Require PEAR Net_Gearman libs */ define('NET_GEARMAN_JOB_PATH', $this->worker_dir); if (!class_exists("Net_Gearman_Job_Common")) { require "Net/Gearman/Job/Common.php"; } if (!class_exists("Net_Gearman_Job")) { require "Net/Gearman/Job.php"; } require "Net/Gearman/Worker.php"; $worker = new Net_Gearman_Worker($this->servers); foreach ($worker_list as $w) { $this->log("Adding job {$w}", GearmanManager::LOG_LEVEL_WORKER_INFO); $worker->addAbility($w); } $worker->attachCallback(array($this, 'job_start'), Net_Gearman_Worker::JOB_START); $worker->attachCallback(array($this, 'job_complete'), Net_Gearman_Worker::JOB_COMPLETE); $worker->attachCallback(array($this, 'job_fail'), Net_Gearman_Worker::JOB_FAIL); $this->start_time = time(); $this->job_execution_count++; $worker->beginWork(array($this, "monitor")); }
function worker() { //check for args if (count($this->arguments) != 3) { $this->writeLine("Usage: worker schema_id function"); exit; } $schema_id = $this->arguments[1]; $function = $this->arguments[2]; #define('NET_GEARMAN_JOB_PATH', "../../include/Net/Gearman/Job/" ); require_once 'Net/Gearman/Worker.php'; //create server array $servers = array(); foreach (Doo::db()->find('GearmanJobServers', array('where' => "schema_id = {$schema_id}")) as $s) { $servers[] = $s->hostname . ":" . $s->port; } try { $worker = new Net_Gearman_Worker($servers); $worker->addAbility($function); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; } }
/** * Starts a worker for the PEAR library * * @param array $worker_list List of worker functions to add * @param array $timeouts list of worker timeouts to pass to server * @return void * */ protected function start_lib_worker($worker_list, $timeouts = array()) { /** * Require PEAR Net_Gearman libs */ define('NET_GEARMAN_JOB_PATH', $this->worker_dir); if (!class_exists("Net_Gearman_Job_Common")) { require "Net/Gearman/Job/Common.php"; } if (!class_exists("Net_Gearman_Job")) { require "Net/Gearman/Job.php"; } if (!class_exists("Net_Gearman_Worker")) { require "Net/Gearman/Worker.php"; } $worker = new Net_Gearman_Worker($this->servers); foreach ($worker_list as $w) { $timeout = isset($timeouts[$w]) ? $timeouts[$w] : null; $message = "Adding job {$w}"; if ($timeout) { $message .= "; timeout: {$timeout}"; } $this->log($message, GearmanManager::LOG_LEVEL_WORKER_INFO); $worker->addAbility($w, $timeout, $this->functions[$w]); } $worker->attachCallback(array($this, 'job_start'), Net_Gearman_Worker::JOB_START); $worker->attachCallback(array($this, 'job_complete'), Net_Gearman_Worker::JOB_COMPLETE); $worker->attachCallback(array($this, 'job_fail'), Net_Gearman_Worker::JOB_FAIL); $this->start_time = time(); $worker->beginWork(array($this, "monitor")); }
protected function execute(InputInterface $input, OutputInterface $output) { $limit = $input->getArgument('limit'); if ($limit > 1) { for (; $limit > 0; $limit--) { BackgroundProcess::open('php index.php worker:parse 1'); } } else { if ($limit == 1) { try { $worker = new \Net_Gearman_Worker($this->config->getGearmanHost()); $worker->addAbility('parse'); $worker->beginWork(); } catch (\Net_Gearman_Exception $e) { Logger::addMessage($e->getMessage()); BackgroundProcess::open('php index.php worker:parse 1'); } } } FileLogger::save(); }
<?php error_reporting(E_ALL & ~E_NOTICE); require_once 'Net/Gearman/Worker.php'; try { $worker = new Net_Gearman_Worker(array('localhost:4730')); $worker->addAbility('Sleep'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }
/** * Copy map of jobs into worker as abilities for the worker * * @param Net_Gearman_Worker $worker Worker object to add abilities to * @param array $initParams Parameters for job constructor * as per $worker->addAbility * * @return void */ public function mapToWorker(Net_Gearman_Worker $worker, $initParams = array()) { foreach ($this->map as $job => $class) { $worker->addAbility($job, null, $initParams); } }
<?php require_once 'Net/Gearman/Worker.php'; try { $worker = new Net_Gearman_Worker(array('dev01:7003', 'dev01:7004')); $worker->addAbility('Hello'); $worker->addAbility('Fail'); $worker->addAbility('SQL'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }
<?php define('NET_GEARMAN_JOB_CLASS_PREFIX', ''); define('NET_GEARMAN_JOB_PATH', dirname(__FILE__) . "/Jobs"); include_once dirname(__FILE__) . '/../../../common/lib/Net/Gearman/Connection.php'; include_once dirname(__FILE__) . '/../../../common/lib/Net/Gearman/Worker.php'; $worker = new Net_Gearman_Worker(array('127.0.0.1:4730')); // port 7003 is standard, you can add more jobservers with array('localhost:7003','localhost:7004') $worker->addAbility('Worker_Restarter'); // make job "Redovalnica" available at this worker $worker->beginWork();