/** * @param string $className * @return CM_Jobdistribution_Job_Abstract|null */ protected function _instantiateJob($className) { try { /** @var CM_Jobdistribution_Job_Abstract $job */ $job = new $className(); if ($job instanceof CM_Service_ManagerAwareInterface) { /** @var CM_Service_ManagerAwareInterface $job */ $job->setServiceManager($this->getServiceManager()); } return $job; } catch (Exception $e) { $logLevel = CM_Log_Logger::exceptionToLevel($e); $context = new CM_Log_Context(); $context->setException($e); $this->getServiceManager()->getLogger()->addMessage('Delayed queue error', $logLevel, $context); return null; } }
public function run() { $jobsRun = 0; while (true) { if ($jobsRun >= $this->_jobLimit) { return; } $workFailed = false; try { $jobsRun++; CM_Cache_Storage_Runtime::getInstance()->flush(); $workFailed = !$this->_getGearmanWorker()->work(); } catch (Exception $ex) { $this->getServiceManager()->getLogger()->addMessage('Worker failed', CM_Log_Logger::exceptionToLevel($ex), (new CM_Log_Context())->setException($ex)); } if ($workFailed) { throw new CM_Exception_Invalid('Worker failed'); } } }
/** * @param Exception $exception */ protected function _logException(Exception $exception) { $logLevel = CM_Log_Logger::exceptionToLevel($exception); $context = new CM_Log_Context(); $context->setException($exception); CM_Service_Manager::getInstance()->getLogger()->addMessage('KickBox client error', $logLevel, $context); }
/** * @param string|string[] $channels * @param Closure $callback * @return mixed return something else than null to exit the pubsub loop */ public function subscribe($channels, Closure $callback) { $redisClient = $this->_createPredisClient($this->_config['host'], $this->_config['port'], $this->_config['database'], 60, -1); $pubsub = $redisClient->pubSubLoop(['subscribe' => $channels]); $response = null; /** @var stdClass $message */ foreach ($pubsub as $message) { try { if ($message->kind == 'message') { $response = $callback($message->channel, $message->payload); } } catch (Exception $e) { $this->getServiceManager()->getLogger()->addMessage('Redis message callback failed', CM_Log_Logger::exceptionToLevel($e), (new CM_Log_Context())->setException($e)); } if (!is_null($response)) { break; } } return $response; }
/** * @param Closure $regularCode * @param Closure $errorCode * @return mixed * @throws CM_Exception */ protected function _runWithCatching(Closure $regularCode, Closure $errorCode) { try { return $regularCode(); } catch (CM_Exception $ex) { $config = self::_getConfig(); $exceptionsToCatch = $config->exceptionsToCatch; $catchPublicExceptions = !empty($config->catchPublicExceptions); $errorOptions = \Functional\first($exceptionsToCatch, function ($options, $exceptionClass) use($ex) { return is_a($ex, $exceptionClass); }); $catchException = null !== $errorOptions; if ($catchException && isset($errorOptions['log']) && true === $errorOptions['log']) { $logLevel = isset($errorOptions['level']) ? $errorOptions['level'] : null; if (null === $logLevel) { $logLevel = CM_Log_Logger::exceptionToLevel($ex); } $context = new CM_Log_Context(); $context->setUser($this->getViewer()); $context->setException($ex); $this->getServiceManager()->getLogger()->addMessage('Response processing error', $logLevel, $context); } if (!$catchException && ($catchPublicExceptions && $ex->isPublic())) { $errorOptions = []; $catchException = true; } if ($catchException) { return $errorCode($ex, $errorOptions); } throw $ex; } }