public function shutDown() { //To protect call methods from null if it's empty worker if ($this->taskWasGot) { $this->logStartShutDown(); if ($this->alreadyShutDown === false) { //for case of use PCNTL signals $this->logInitShutDown(); $this->alreadyShutDown = true; /** * @var BaseAdapted $fetchService */ $adaptedService = $this->adapter->getAdaptedService(); if ($adaptedService->isScriptWasInterrupted() === true) { if ($adaptedService->getZmqPerformer()->getPerformerEarlyTerminated()->isStandOnSubscription()) { $adaptedService->getZmqPerformer()->pushPerformerEarlyTerminated(); } } if ($this->adapter->isJobInfoWasSent() === false) { $errorMsg = "Worker process was terminated before task with id " . $adaptedService->getExecutionDto()->getTaskId() . " and params " . serialize($adaptedService->getParams()) . " was complete."; $this->adapter->getJob()->sendComplete(serialize(InspectionHelper::prepareErrorExecutionDto($adaptedService->getExecutionDto()->getTaskId(), $errorMsg))); } } $this->logFinishShutDown(); } return null; }
public function initAdapted(\GearmanJob $job) { $this->loop = Factory::create(); $this->read = new \React\Stream\Stream(STDIN, $this->loop); $this->read->bufferSize = 8192; $this->write = new \React\Stream\Stream(STDOUT, $this->loop); $this->write->bufferSize = 8192; $this->job = $job; //protect from repeated execution $initStart = false; $pmErrorDtoAlreadySent = false; /** * Receive sockets params json from PM to set it into performer */ $this->read->on('data', function ($data) use($initStart, $pmErrorDtoAlreadySent) { if (!is_array($this->pmWorkerDta)) { $this->pmWorkerDta = @json_decode($data, true); if ($this->pmWorkerDta !== false && is_array($this->pmWorkerDta)) { if ($initStart === false) { $initStart = true; try { $this->initBasicParams(); $this->adaptedService->getTerminatorPauseStander()->setPublisherPmSocketAddress($this->pmWorkerDta[DataTransferConstants::PUBLISHER_PM]); $this->adaptedService->getTerminatorPauseStander()->setUSleepTime(5000000); $performerSocketParams = new PerformerSocketsParamsDto(); $performerSocketParams->setRequestPulsarRsSocketAddress($this->pmWorkerDta[DataTransferConstants::REQUEST_PULSAR_RS]); $performerSocketParams->setPublisherPulsarSocketAddress($this->pmWorkerDta[DataTransferConstants::PUBLISHER_PULSAR]); $performerSocketParams->setPushPulsarSocketAddress($this->pmWorkerDta[DataTransferConstants::PUSH_PULSAR]); $this->adaptedService->getZmqPerformer()->setSocketsParams($performerSocketParams); $this->adaptedService->getZmqPerformer()->setLogger($this->logger); $this->adaptedService->serviceExec(); $this->adaptedService->getExecutionDto()->setExecutionMessage($this->adaptedService->getParams()); $this->job->sendComplete(serialize($this->adaptedService->getExecutionDto())); $this->jobInfoWasSent = true; $this->logger->critical("Job complete was sent."); } catch (\Exception $e) { $errorMsg = "Adapter die in Exception with \$e: " . $e->getMessage() . "|params: " . serialize($this->adaptedService->getParams()); //. $e->getTraceAsString(); $this->logger->critical($errorMsg . " | " . serialize($this->pmWorkerDta)); $this->job->sendComplete(serialize(InspectionHelper::prepareErrorExecutionDto($this->adaptedService->getTaskId(), $errorMsg))); $this->jobInfoWasSent = true; $this->logger->critical("Job complete with exception was sent."); die; } $this->loop->nextTick(function () { $this->loop->stop(); }); } } else { if ($pmErrorDtoAlreadySent === false) { $pmErrorDtoAlreadySent = true; $pmErrorArr = []; $pmErrorArr[DataTransferConstants::ERROR_LEVEL] = ErrorsConstants::CRITICAL; $pmErrorArr[DataTransferConstants::ERROR_REASON] = PmErrorConstants::WORKER_NOT_RECEIVE_CORRECT_DTO; $pmErrorArr[DataTransferConstants::ERROR_ELEMENT] = $this->pmWorkerDta; //write to PM's allotted STDIN about critical error $this->write->write(json_encode($pmErrorArr)); $this->loop->nextTick(function () { $this->loop->stop(); }); } } } }); $timerIteration = 0; $this->loop->addPeriodicTimer(3, function (Timer $timer) use(&$timerIteration) { if ($this->pmWorkerDta === null) { if ($timerIteration > $this->maxTimerIteration) { $this->initBasicParams(); die; } $timerIteration++; } else { $timer->cancel(); } }); $this->loop->run(); if ($pmErrorDtoAlreadySent) { die; } }