/** * Starts a worker for the PECL 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()) { $thisWorker = new GearmanWorker(); $thisWorker->addOptions(GEARMAN_WORKER_NON_BLOCKING); $thisWorker->setTimeout(5000); foreach ($this->servers as $s) { $this->log("Adding server {$s}", GearmanManager::LOG_LEVEL_WORKER_INFO); // see: https://bugs.php.net/bug.php?id=63041 try { $thisWorker->addServers($s); } catch (\GearmanException $e) { if ($e->getMessage() !== 'Failed to set exception option') { throw $e; } } } foreach ($worker_list as $w) { $timeout = isset($timeouts[$w]) ? $timeouts[$w] : null; $this->log("Adding job {$w} ; timeout: " . $timeout, GearmanManager::LOG_LEVEL_WORKER_INFO); $thisWorker->addFunction($w, array($this, "do_job"), $this, $timeout); } $start = time(); while (!$this->stop_work) { if (@$thisWorker->work() || $thisWorker->returnCode() == GEARMAN_IO_WAIT || $thisWorker->returnCode() == GEARMAN_NO_JOBS) { if ($thisWorker->returnCode() == GEARMAN_SUCCESS) { continue; } if (!@$thisWorker->wait()) { if ($thisWorker->returnCode() == GEARMAN_NO_ACTIVE_FDS) { sleep(5); } } } /** * Check the running time of the current child. If it has * been too long, stop working. */ if ($this->max_run_time > 0 && time() - $start > $this->max_run_time) { $this->log("Been running too long, exiting", GearmanManager::LOG_LEVEL_WORKER_INFO); $this->stop_work = true; } if (!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) { $this->log("Ran {$this->job_execution_count} jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO); $this->stop_work = true; } } $thisWorker->unregisterAll(); }
/** * Starts a worker for the PECL library * * @param array $worker_list List of worker functions to add * @return void * */ protected function start_lib_worker($worker_list) { $thisWorker = new GearmanWorker(); $thisWorker->addOptions(GEARMAN_WORKER_NON_BLOCKING); $thisWorker->setTimeout(5000); foreach ($this->servers as $s) { $this->log("Adding server {$s}", GearmanManager::LOG_LEVEL_WORKER_INFO); $thisWorker->addServers($s); } foreach ($worker_list as $w) { $this->log("Adding job {$w}", GearmanManager::LOG_LEVEL_WORKER_INFO); $thisWorker->addFunction($w, array($this, "do_job"), $this); } $start = time(); while (!$this->stop_work) { if (@$thisWorker->work() || $thisWorker->returnCode() == GEARMAN_IO_WAIT || $thisWorker->returnCode() == GEARMAN_NO_JOBS) { if ($thisWorker->returnCode() == GEARMAN_SUCCESS) { continue; } if (!@$thisWorker->wait()) { if ($thisWorker->returnCode() == GEARMAN_NO_ACTIVE_FDS) { sleep(5); } } } /** * Check the running time of the current child. If it has * been too long, stop working. */ if ($this->max_run_time > 0 && time() - $start > $this->max_run_time) { $this->log("Been running too long, exiting", GearmanManager::LOG_LEVEL_WORKER_INFO); $this->stop_work = true; } if (!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) { $this->log("Ran {$this->job_execution_count} jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO); $this->stop_work = true; } } $thisWorker->unregisterAll(); }
protected function destroyGearmanWorker(\GearmanWorker $worker) { $worker->unregisterAll(); }