protected function execute(InputInterface $input, OutputInterface $output) { // Capture duration of job with Symfony's Stopwatch component. $stopwatch = new Stopwatch(); // Start capturing duration of job execution. $stopwatch->start('job'); /** @var ManagerRegistry $managerRegistry */ $managerRegistry = $this->getContainer()->get('doctrine'); $em = $managerRegistry->getManager(); $jobId = $input->getArgument('jobId'); /** @var Job $job */ $job = $em->getRepository('CampaignChainCoreBundle:Job')->find($jobId); if (!$job) { // TODO: Log this message, because it won't surface anywhere else. throw new \Exception('No job found with ID: ' . $jobId); } try { // TODO: Set status for operation entity (and activity if equal) as well. // If a Job service is provided, then execute it. if ($job->getName()) { $jobService = $this->getContainer()->get($job->getName()); // A Job service must implement the respective interface. if ($jobService instanceof JobActionInterface) { try { $status = $jobService->execute($job->getActionId()); switch ($status) { case JobActionInterface::STATUS_OK: $job->setStatus(Job::STATUS_CLOSED); break; case JobActionInterface::STATUS_ERROR: $job->setStatus(Job::STATUS_ERROR); break; } $job->setMessage($jobService->getMessage()); } catch (JobException $e) { $job->setMessage($e->getMessage()); $job->setStatus(Job::STATUS_ERROR); $job->setErrorCode($e->getCode()); $em->flush(); } catch (\Exception $e) { $job->setMessage($e->getMessage()); $job->setStatus(Job::STATUS_ERROR); $job->setErrorCode(ErrorCode::PHP_EXCEPTION); $em->flush(); } } else { $errorMsg = 'The job service "' . $job->getName() . '" with the class "' . get_class($jobService) . '" does not implement the interface CampaignChain\\CoreBundle\\Job\\JobActionInterface as required.'; $job->setStatus(Job::STATUS_ERROR); $job->setMessage($errorMsg); $em->flush(); throw new \Exception($errorMsg); } } else { // Actions have a job type of null value. if ($job->getJobType() == null) { // No Job service, so let's just close the action's entity and the job. $action = $em->getRepository(Action::getRepositoryName($job->getActionType()))->find($job->getActionId()); $action->setStatus(Action::STATUS_CLOSED); $job->setStatus(Job::STATUS_CLOSED); } } $job->setEndDate(new \DateTime('now', new \DateTimeZone('UTC'))); // Job is done, let's see how long it took. $stopwatchEvent = $stopwatch->stop('job'); $job->setDuration($stopwatchEvent->getDuration()); $em->persist($job); $em->flush(); } catch (\Exception $e) { $job->setMessage($e->getMessage()); $job->setStatus(Job::STATUS_ERROR); $em->flush(); // TODO: Send automatic notification. } }
/** * If the Activity equals the Operation, then set the status of the Activity to the same value. * * @param string $status * @param bool $calledFromOperation * * @return Activity */ public function setStatus($status, $calledFromOperation = false) { parent::setStatus($status); // Change the Operation as well only if this method has not been called by an Operation instance to avoid recursion. if (!$calledFromOperation && $this->getEqualsOperation() && count($this->getOperations())) { $this->getOperations()[0]->setStatus($this->status, true); } return $this; }
/** * If the Activity equals the Operation, then set the status of the Activity to the same value. * * @param string $status * @param bool $calledFromActivity * * @return Operation */ public function setStatus($status, $calledFromActivity = false) { parent::setStatus($status); // Change the Activity as well only if this method has not been called by an Activity instance to avoid recursion. if (!$calledFromActivity && $this->getActivity() && $this->getActivity()->getEqualsOperation()) { $this->getActivity()->setStatus($this->status, true); } return $this; }
public function processAction(Action $action) { $action->setStatus(Action::STATUS_PAUSED); return $action; }