public function testProduce() { $type = 'JobType'; $ticket = 'JobTicket'; $queue = 'QueueName'; $message = new Message($type, $ticket); $jobType = $this->createMock(JobTypeInterface::class); $jobType->expects($this->once())->method('getQueue')->willReturn($queue); $producerMessage = new DefaultMessage('ConsumeJob', ['type' => $type, 'ticket' => $ticket]); $this->registry->expects($this->once())->method('get')->with($message->getType())->willReturn($jobType); $this->producer->expects($this->once())->method('produce')->with($producerMessage, $queue); $this->subject->produce($message); }
/** * Publishes a message to the backend. * * @param Message $message * @return void * @throws \RuntimeException If publishing fails */ public function produce(Message $message) { $type = $message->getType(); $body = array('ticket' => $message->getTicket()); try { $this->logger->debug(sprintf('Publish message for job %s to sonata backend', $message->getTicket()), ['type' => $type, 'body' => $body]); $queue = $this->registry->get($message->getType())->getQueue(); $this->backendProvider->getBackend($queue)->createAndPublish($type, $body); } catch (\Exception $e) { $this->logger->error(sprintf('Failed to publish message (Error: %s)', $e->getMessage()), ['exception' => $e]); if (!$e instanceof \RuntimeException) { $e = new \RuntimeException($e->getMessage(), $e->getCode(), $e); } throw $e; } }
/** * {@inheritdoc} */ public function onMessage(Message $message) { $this->stopwatch->start('processMessage'); $job = $this->findJob($message->getTicket()); if ($job->getStatus() == Status::PROCESSING() || $job->getStatus() == Status::CANCELLED() || $job->getStatus() == Status::ERROR()) { $this->logger->notice(sprintf('Skipped execution of job %s (status: %s)', $job->getTicket(), $job->getStatus())); return; } try { $this->locker->lock($this->getLockName($job)); } catch (LockException $e) { $this->logger->warning('Failed to get lock for job ' . $job->getTicket()); return; } $event = new ExecutionEvent($job, new Context()); $this->dispatchExecutionEvent(JobEvents::JOB_PRE_EXECUTE, $event); $job->setStatus(Status::PROCESSING()); $job->setProcessingTime(0); $this->jobManager->save($job); $response = null; $this->stopwatch->start('processJob'); try { $this->logger->debug(sprintf('Execute job %s of type "%s"', $job->getTicket(), $job->getType()), ['parameters' => $job->getParameters()]); // invoke the job $response = $this->invoker->invoke($job, $event->getContext()); if ($job->getStatus() != Status::CANCELLED()) { $status = $job->hasSchedules() ? Status::SLEEPING() : Status::PROCESSED(); } else { $status = Status::CANCELLED(); } $this->dispatchExecutionEvent(JobEvents::JOB_POST_EXECUTE, $event); } catch (\Throwable $e) { $this->logger->warning(sprintf('Failed to execute job %s (Error: $s)', $job->getTicket(), $e->getMessage()), ['job' => $job, 'exception' => $e]); $this->getJobLogger($job)->error($e->getMessage(), ['exception' => $e]); $response = new ExceptionResponse($e); $status = Status::ERROR(); } catch (\Exception $e) { $this->logger->warning(sprintf('Failed to execute job %s (Error: $s)', $job->getTicket(), $e->getMessage()), ['job' => $job, 'exception' => $e]); $this->getJobLogger($job)->error($e->getMessage(), ['exception' => $e]); $response = new ExceptionResponse($e); $status = Status::ERROR(); } $this->releaseLock($job); $this->helper->updateJob($job, $status, $this->stopwatch->stop('processJob')->getDuration(), $response); $this->jobManager->save($job); if (Status::isTerminated($job->getStatus())) { $this->dispatcher->dispatch(JobEvents::JOB_TERMINATED, new TerminationEvent($job)); } }
/** * {@inheritdoc} */ public function produce(Message $message) { $producerMessage = new DefaultMessage('ConsumeJob', ['type' => $message->getType(), 'ticket' => $message->getTicket()]); $this->logger->debug('Publish message to bernard queue backend', ['message' => $message]); $this->producer->produce($producerMessage, $this->registry->get($message->getType())->getQueue()); }
/** * @param Status $status * @dataProvider provideStatusToSkip */ public function testOnMessageSkipsExecutionIfStatusIs(Status $status) { $message = new Message('job-type', 'job-ticket'); $job = new Job(); $job->setType($message->getType()); $job->setTicket($message->getTicket()); $job->setStatus($status); $this->jobManager->expects($this->any())->method('findByTicket')->willReturn($job); $this->dispatcher->expects($this->never())->method('dispatch'); $this->locker->expects($this->never())->method('lock'); $this->subject->onMessage($message); }