Ejemplo n.º 1
0
 public function testWithDedicatedConnection()
 {
     $job = $this->getJobManager()->addJob('throw_dbal_exception');
     $this->processJobs();
     $this->getEntityManager()->clear();
     $this->assertEquals(Status::ERROR(), $job->getStatus());
 }
Ejemplo n.º 2
0
 public function testFindSchedules()
 {
     /**
      * @var ScheduleManager $scheduleManager
      */
     $scheduleManager = $this->getContainer()->get('abc.job.schedule_manager');
     /**
      * @var JobManagerInterface $jobManager
      */
     $jobManager = $this->getContainer()->get('abc.job.job_manager');
     $schedule1 = $scheduleManager->create();
     $schedule1->setType('cron');
     $schedule1->setExpression('foobar');
     $schedule1->setIsActive(true);
     $job1 = $jobManager->create('foo');
     $job1->setStatus(Status::REQUESTED());
     $job1->addSchedule($schedule1);
     $jobManager->save($job1);
     $schedule2 = $scheduleManager->create();
     $schedule2->setType('cron');
     $schedule2->setExpression('barfoo');
     $schedule2->setIsActive(false);
     $job2 = $jobManager->create('foo');
     $job2->setStatus(Status::REQUESTED());
     $job2->addSchedule($schedule2);
     $jobManager->save($job2);
     $scheduleManager->save($schedule2);
     $schedules = $scheduleManager->findSchedules();
     $this->assertCount(1, $schedules);
     $this->assertEquals($schedule1->getExpression(), $schedules[0]->getExpression());
 }
Ejemplo n.º 3
0
 /**
  * Sets status of the job to cancelled if process controller indicates to exit.
  *
  * @return boolean
  */
 public function doStop()
 {
     if ($this->controller->doStop()) {
         $this->job->setStatus(Status::CANCELLED());
         return true;
     }
     return false;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function doStop()
 {
     $time = time();
     if (null === $this->lastCheck || $this->lastCheck + $this->interval <= $time) {
         $this->lastCheck = $time;
         $this->manager->refresh($this->job);
     }
     return $this->job->getStatus() == Status::CANCELLING() || $this->job->getStatus() == Status::CANCELLED();
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (null === $value) {
         return;
     }
     if (!in_array($value, JobStatus::values())) {
         $this->context->buildViolation($constraint->message)->setParameter('{{string}}', $value)->addViolation();
     }
 }
Ejemplo n.º 6
0
 /**
  * Updates a job.
  *
  * If the given status is ERROR or CANCELLED the child jobs of the given job will be terminated with status CANCELLED.
  *
  * @param EntityJobInterface $job
  * @param Status             $status
  * @param int                $processingTime
  * @param mixed|null         $response
  */
 public function updateJob(EntityJobInterface $job, Status $status, $processingTime = 0, $response = null)
 {
     $job->setStatus($status);
     $job->setProcessingTime($job->getProcessingTime() + ($processingTime === null ? 0 : $processingTime));
     $job->setResponse($response);
     if (Status::isTerminated($status)) {
         $job->setTerminatedAt(new \DateTime());
     }
     if ($job->hasSchedules() && Status::isTerminated($status)) {
         foreach ($job->getSchedules() as $schedule) {
             if (method_exists($schedule, 'setIsActive')) {
                 $schedule->setIsActive(false);
             }
         }
     }
 }
 public static function provideSerializedJob()
 {
     return [[self::createJob('JobTicket', 'abc.mailer', Status::PROCESSING(), 12345), '{"ticket":"JobTicket","type":"abc.mailer","status":"PROCESSING","processing_time":12345}'], [self::createJob(null, 'abc.mailer', null, null, [new Message('*****@*****.**', '*****@*****.**', 'Message Subject', 'Message Body')]), '{"type":"abc.mailer","parameters":[{"to":"*****@*****.**","from":"*****@*****.**","subject":"Message Subject","message":"Message Body"}]}'], [self::createJob(null, 'abc.mailer', null, null, null, [self::createSchedule('cron', '* * * * *')]), '{"type":"abc.mailer","schedules":[{"type":"cron","expression":"* * * * *","is_active":true}]}']];
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function setStatus(Status $status)
 {
     $this->status = $status->getValue();
     $this->enumStatus = $status;
 }
 public static function provideNonCancelStatus()
 {
     return [[Status::REQUESTED()], [Status::PROCESSING()], [Status::PROCESSED()], [Status::SLEEPING()], [Status::ERROR()]];
 }
Ejemplo n.º 10
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));
     }
 }
Ejemplo n.º 11
0
 /**
  * @return array
  */
 public static function getFindByCountData()
 {
     return [[['type' => 'foo'], 1], [['type' => ['foo', 'bar']], 2], [['type' => 'undefined'], 0], [['type' => 'foo', 'status' => Status::REQUESTED()], 1], [['type' => 'foo', 'status' => Status::PROCESSING()], 0], [['type' => ['$match' => 'foo']], 2]];
 }
Ejemplo n.º 12
0
 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()]));
 }
Ejemplo n.º 13
0
 public static function provideTerminatedStatus()
 {
     $result = [];
     foreach (Status::getTerminatedStatus() as $status) {
         $result[] = [$status];
     }
     return $result;
 }
Ejemplo n.º 14
0
 public function testJobCanBeCancelled()
 {
     /**
      * @var Factory $controllerFactory
      */
     $controllerFactory = $this->getContainer()->get('abc.job.controller_factory');
     $controllerFactory->addController(new DoStopController());
     $job = $this->getJobManager()->addJob('cancel');
     $this->processJobs();
     $this->assertContains('cancelled', $job->getResponse());
     $this->assertEquals(Status::CANCELLED(), $job->getStatus());
 }
Ejemplo n.º 15
0
 public function testDoExitControllerReturnsTrue()
 {
     $this->controller->expects($this->any())->method('doStop')->willReturn(true);
     $this->assertTrue($this->subject->doStop());
     $this->assertEquals(Status::CANCELLED(), $this->job->getStatus());
 }
Ejemplo n.º 16
0
 /**
  * @return array
  */
 public static function getNonTerminatedStatus()
 {
     return [[Status::REQUESTED()], [Status::SLEEPING()], [Status::PROCESSING()]];
 }
Ejemplo n.º 17
0
 public function testProduceAndConsume()
 {
     $ticket = $this->getJobManager()->addJob('log', array('message'));
     $this->processJobs();
     $this->assertEquals(Status::PROCESSED(), $this->getJobManager()->get($ticket)->getStatus());
 }
Ejemplo n.º 18
0
 /**
  * @param Status $status
  * @return bool Whether the given status indicates that the job is terminated
  */
 public static function isTerminated(Status $status)
 {
     return in_array($status->getValue(), static::$terminated_status_values);
 }
Ejemplo n.º 19
0
 public function testEquals()
 {
     $this->assertTrue(Status::PROCESSED()->equals(Status::PROCESSED()));
     $this->assertFalse(Status::PROCESSED()->equals(Status::CANCELLED()));
 }