예제 #1
0
 /**
  * Method to run a domain job whose workload is a serialized JobRequestInterface
  *
  * @param \GearmanJob $job Object with job parameters
  *
  * @throws \Ice\Domain\Jobs\UnsupportedJobException
  * @throws \Exception re-throw uncaught exceptions
  * @return boolean
  *
  * @Gearman\Job(
  *     iterations = 1,
  *     name = "addDomainJob",
  *     description = "Data should be an json-encoded object containing at least a 'name' property. That domain job wll be executed"
  * )
  */
 public function addDomainJob($job)
 {
     $jobRequest = $this->requestSerializer->deserializeJobRequest($job->workload());
     if ($this->logger) {
         $this->logger->notice("Job received", [$job->workload()]);
     }
     if (!$this->director->hasWorkerProviderFor($jobRequest->getName())) {
         if ($this->logger) {
             $this->logger->critical("No worker available for job of name: " . $jobRequest->getName(), [$job->workload()]);
         }
         throw new UnsupportedJobException("No worker available for job of name: " . $jobRequest->getName());
     }
     $worker = $this->director->getWorkerProviderFor($jobRequest->getName())->getWorkerFor($jobRequest->getName());
     try {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch('ice.job.pre_execute');
         }
         $worker->execute($jobRequest);
         if ($this->logger) {
             $this->logger->notice("Job complete", [$job->workload()]);
         }
     } catch (\Exception $e) {
         if ($this->logger) {
             $this->logger->critical("Uncaught exception when processing job", array('workload' => $job->workload(), 'message' => $e->getMessage(), 'stack_trace' => $e->getTrace(), 'exception' => $e));
         }
         //Re-throw the exception anyway, so that gearman knows the job has failed.
         throw $e;
     }
     return true;
 }
 function let(WorkerDirectorInterface $workerDirector, JobRequestSerializerInterface $requestSerializer, JobRequestInterface $deserializedJobRequest, WorkerProviderInterface $fooFactory, WorkerInterface $fooWorker, LoggerInterface $logger, EventDispatcherInterface $dispatcher)
 {
     $workerDirector->getWorkerProviderFor('fooName')->willReturn($fooFactory);
     $fooFactory->getWorkerFor('fooName')->willReturn($fooWorker);
     $deserializedJobRequest->getName()->willReturn('fooName');
     $requestSerializer->deserializeJobRequest(Argument::any())->willReturn($deserializedJobRequest);
     $this->beConstructedWith($workerDirector, $requestSerializer);
     //Allow 'good' log calls to be made even if we don't explicitly stub them in the specs
     $logger->debug(Argument::any(), Argument::any())->willReturn(null);
     $logger->info(Argument::any(), Argument::any())->willReturn(null);
     $logger->notice(Argument::any(), Argument::any())->willReturn(null);
 }