Exemple #1
0
 /**
  * {@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));
     }
 }
 public function testProduceAndConsume()
 {
     $ticket = $this->getJobManager()->addJob('log', array('message'));
     $this->processJobs();
     $this->assertEquals(Status::PROCESSED(), $this->getJobManager()->get($ticket)->getStatus());
 }
 public static function provideNonCancelStatus()
 {
     return [[Status::REQUESTED()], [Status::PROCESSING()], [Status::PROCESSED()], [Status::SLEEPING()], [Status::ERROR()]];
 }
 /**
  * @return array
  */
 public static function getTerminatedStatus()
 {
     return [[Status::PROCESSED()], [Status::ERROR()], [Status::CANCELLED()]];
 }
 public function testFindByWithStatus()
 {
     $job = $this->subject->create('JobType', null, new Schedule('Type', 'Expression'));
     $job->setStatus(Status::REQUESTED());
     $this->subject->save($job);
     $this->assertCount(1, $this->subject->findBy(['status' => Status::REQUESTED()]));
     $this->assertEmpty($this->subject->findBy(['status' => Status::PROCESSED()]));
 }
 public function testOnMessageInvokesJob()
 {
     $type = 'JobType';
     $ticket = 'JobTicket';
     $microTime = microtime(true);
     $parameters = ['parameters'];
     $response = 'response';
     $job = new Job($type);
     $job->setTicket($ticket);
     $job->setParameters($parameters);
     $message = new Message($type, $ticket);
     $this->jobManager->expects($this->once())->method('findByTicket')->with($message->getTicket())->willReturn($job);
     $this->locker->expects($this->once())->method('lock')->with(Manager::JOB_LOCK_PREFIX . $job->getTicket());
     $this->invoker->expects($this->once())->method('invoke')->with($job, $this->isInstanceOf(Context::class))->willReturn($response);
     $this->expectsCallsUpdateJob($job, Status::PROCESSED(), 0, $response);
     $this->jobManager->expects($this->at(2))->method('save')->with($this->callback(function (JobInterface $job) {
         return $job->getStatus() == Status::PROCESSED();
     }));
     $this->subject->onMessage($message);
 }
 public function testJobCanRemoveSchedule()
 {
     // create scheduled job
     $schedule = $this->getScheduleManager()->create('cron', '* * * * *');
     $ticket = $this->getJobManager()->addJob('remove_schedule', null, $schedule);
     $this->assertCount(1, $this->getScheduleManager()->findSchedules());
     $this->assertEquals(Status::REQUESTED(), $this->getJobManager()->get($ticket)->getStatus());
     // process schedules
     $this->runConsole("abc:scheduler:process", array("--iteration" => 1));
     $this->processJobs();
     $this->assertTrue($this->containsMessage('removed schedule', $this->getJobManager()->getLogs($ticket)));
     $this->assertEquals(Status::PROCESSED(), $this->getJobManager()->get($ticket)->getStatus());
     $this->assertEmpty($this->getScheduleManager()->findSchedules());
 }
 public function testEquals()
 {
     $this->assertTrue(Status::PROCESSED()->equals(Status::PROCESSED()));
     $this->assertFalse(Status::PROCESSED()->equals(Status::CANCELLED()));
 }