public function testApplication() { $process = new Process($this->config); $application = new Application($this->config, $process); $application->add(new MockJob()); $application->run(); $test = $process->isRunning(); $this->assertTrue($test); $process->stop(); }
/** * @param Application $application */ public function run(Application $application) { foreach ($this->workers as $name => $options) { $worker = $this->getServiceLocator()->get($name); if (!$worker instanceof JobInterface) { throw new \Exception("Error Bootstrapping German Manager, " . $name . " not an instance of JobInterface."); } $application->add($worker); } }
protected function runApplication(Application $app) { $fork = (bool) $this->fork; if ($fork) { $this->stdout("Success: Process is started\n", Console::FG_GREEN); } else { $this->stdout("Success: Process is started, but not daemonized\n", Console::FG_YELLOW); } $app->run((bool) $this->fork); }
/** * @return Application * @throws \yii\base\InvalidConfigException */ public function getApplication() { if ($this->_application === null) { $app = new Application($this->getConfig(), $this->getProcess()); foreach ($this->getJobs() as $name => $job) { $job = Yii::createObject($job); if (!$job instanceof JobInterface) { throw new \yii\base\InvalidConfigException('Gearman job must be instance of JobInterface.'); } $job->setName($name); $app->add($job); } $this->_application = $app; } return $this->_application; }
/** * @return Application */ public function getApplication() { if (null === $this->application) { $this->setApplication(new Application($this->getConfig(), $this->getProcess(), null, $this->container->getJobLogger())); } try { $this->application->getWorker()->getWorker(); } catch (Exception $e) { null; } return $this->application; }
/** * @param InputInterface $input * @param OutputInterface $output * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { $output->write('Starting gearman-handler: '); if (!$this->isDaemon) { $isDaemon = $this->isDaemon; } elseif ($input->hasOption('daemon')) { $isDaemon = $input->getOption('daemon'); if ($isDaemon == 'false') { $isDaemon = false; } elseif ($isDaemon == 'true') { $isDaemon = true; } else { $isDaemon = (bool) $isDaemon; } } else { $isDaemon = $this->isDaemon; } $config = $this->getConfig(); $config->set($input->getOptions()); $process = $this->getProcess(); if ($process->isRunning()) { $output->write('[ <error>Failed: Process is already running</error> ]', true); return; } if (is_callable($this->getRuntime())) { $runtime = $this->getRuntime(); $this->setResult(true); $output->write('[ <fg=green>OK</fg=green> ]', true); $runtime(); } else { $app = $this->getGearmanApplication(); if (!$app instanceof GearmanApplication) { $app = new Application($this->getConfig(), $this->getProcess()); } $this->setResult(true); $output->write('[ <fg=green>OK</fg=green> ]', true); $app->run($isDaemon); } }
/** * @param JobInterface $job * @param GearmanJob $gearmanJob * @param Application $root * @return mixed */ public function executeJob(JobInterface $job, GearmanJob $gearmanJob, Application $root) { if ($root->getConfig()->getAutoUpdate() && !$root->isAllowingJob) { $root->restart(); return null; } $root->isAllowingJob = false; if (null !== $root->logger) { $root->logger->info("Executing job {$job->getName()}"); } return $job->execute($gearmanJob); }
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) { require_once __DIR__ . '/../../../autoload.php'; } use Sinergi\Gearman\Application; use Sinergi\Gearman\Process; if (isset($_SERVER['argv'][1])) { $serialized = $_SERVER['argv'][1]; if (is_file($serialized)) { $application = file_get_contents($serialized); if (!empty($application)) { $application = unserialize($application); } unlink($serialized); } if (!$application instanceof Application) { $application = new Application(); } $process = $application->getProcess(); unlink($process->getPidFile()); $process->release(); $int = 0; while ($int < 1000) { if (file_exists($process->getPidFile())) { usleep(1000); $int++; } elseif (file_exists($process->getLockFile())) { $process->release(); usleep(1000); $int++; } else { $int = 1000;