Example #1
0
 /**
  * {@inheritdoc}
  */
 public function runTasks()
 {
     $executions = $this->taskExecutionRepository->findScheduled();
     foreach ($executions as $execution) {
         $handler = $this->taskHandlerFactory->create($execution->getHandlerClass());
         $start = microtime(true);
         $execution->setStartTime(new \DateTime());
         $execution->setStatus(TaskStatus::RUNNING);
         $this->taskExecutionRepository->save($execution);
         try {
             $this->eventDispatcher->dispatch(Events::TASK_BEFORE, new TaskExecutionEvent($execution->getTask(), $execution));
             $result = $handler->handle($execution->getWorkload());
             $execution->setStatus(TaskStatus::COMPLETED);
             $execution->setResult($result);
             $this->eventDispatcher->dispatch(Events::TASK_PASSED, new TaskExecutionEvent($execution->getTask(), $execution));
         } catch (\Exception $ex) {
             $execution->setException($ex->__toString());
             $execution->setStatus(TaskStatus::FAILED);
             $this->eventDispatcher->dispatch(Events::TASK_FAILED, new TaskExecutionEvent($execution->getTask(), $execution));
         }
         $execution->setEndTime(new \DateTime());
         $execution->setDuration(microtime(true) - $start);
         $this->eventDispatcher->dispatch(Events::TASK_FINISHED, new TaskExecutionEvent($execution->getTask(), $execution));
         $this->taskExecutionRepository->save($execution);
     }
 }
Example #2
0
 public function testRunTasksFailed()
 {
     $task = $this->createTask();
     $executions = [$this->createTaskExecution($task, new \DateTime(), 'Test 1')->setStatus(TaskStatus::PLANNED), $this->createTaskExecution($task, new \DateTime(), 'Test 2')->setStatus(TaskStatus::PLANNED)];
     $this->taskExecutionRepository->save($executions[0])->willReturnArgument(0)->shouldBeCalledTimes(2);
     $this->taskExecutionRepository->save($executions[1])->willReturnArgument(0)->shouldBeCalledTimes(2);
     $handler = $this->prophesize(TaskHandlerInterface::class);
     $handler->handle('Test 1')->willThrow(new \Exception());
     $handler->handle('Test 2')->willReturn(strrev('Test 2'));
     $this->taskExecutionRepository->findScheduled()->willReturn($executions);
     $this->taskHandlerFactory->create(TestHandler::class)->willReturn($handler->reveal());
     $this->initializeDispatcher($this->eventDispatcher, $executions[0], Events::TASK_FAILED);
     $this->initializeDispatcher($this->eventDispatcher, $executions[1]);
     $this->taskRunner->runTasks();
     $this->assertLessThanOrEqual(new \DateTime(), $executions[0]->getStartTime());
     $this->assertLessThanOrEqual(new \DateTime(), $executions[1]->getStartTime());
     $this->assertLessThanOrEqual($executions[1]->getStartTime(), $executions[0]->getStartTime());
     $this->assertLessThanOrEqual($executions[0]->getEndTime(), $executions[0]->getStartTime());
     $this->assertLessThanOrEqual($executions[1]->getEndTime(), $executions[1]->getStartTime());
     $this->assertGreaterThan(0, $executions[0]->getDuration());
     $this->assertGreaterThan(0, $executions[1]->getDuration());
     $this->assertNull($executions[0]->getResult());
     $this->assertNotNull($executions[0]->getException());
     $this->assertEquals(strrev('Test 2'), $executions[1]->getResult());
     $this->assertNull($executions[1]->getException());
     $this->assertEquals(TaskStatus::FAILED, $executions[0]->getStatus());
     $this->assertEquals(TaskStatus::COMPLETED, $executions[1]->getStatus());
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $page = $input->getOption('page');
     $pageSize = $input->getOption('page-size');
     $executions = $this->taskExecutionRepository->findAll($page, $pageSize);
     $table = new Table($output);
     $table->setHeaders(['uuid', 'status', 'handler', 'schedule time', 'end time', 'duration']);
     foreach ($executions as $execution) {
         $table->addRow([$execution->getUuid(), $execution->getStatus(), $execution->getHandlerClass(), $execution->getScheduleTime()->format(\DateTime::RFC3339), !$execution->getEndTime() ? '' : $execution->getEndTime()->format(\DateTime::RFC3339), round($execution->getDuration(), 6) * 1000000 . 'ms']);
     }
     $table->render();
 }
Example #4
0
 public function testScheduleTasks()
 {
     $tasks = [$this->createTask($expression1 = CronExpression::factory('@hourly')), $this->createTask($expression2 = CronExpression::factory('@yearly')), $this->createTask(null, $date = new \DateTime('+1 day'))];
     $this->taskRepository->findEndBeforeNow()->willReturn($tasks);
     // single task
     $this->taskExecutionRepository->findByTask($tasks[2])->willReturn([]);
     // already scheduled
     $this->taskExecutionRepository->findPending($tasks[0])->willReturn($this->prophesize(TaskExecutionInterface::class)->reveal());
     $this->taskExecutionRepository->findPending($tasks[1])->willReturn(null);
     $this->taskExecutionRepository->findPending($tasks[2])->willReturn(null);
     $execution1 = $this->prophesize(TaskExecutionInterface::class);
     $execution1->setStatus(TaskStatus::PLANNED)->shouldBeCalled();
     $this->taskExecutionRepository->create($tasks[1], $expression2->getNextRunDate())->willReturn($execution1->reveal());
     $this->taskExecutionRepository->save($execution1)->shouldBeCalled();
     $execution2 = $this->prophesize(TaskExecutionInterface::class);
     $execution2->setStatus(TaskStatus::PLANNED)->shouldBeCalled();
     $this->taskExecutionRepository->create($tasks[2], $date)->willReturn($execution2->reveal());
     $this->taskExecutionRepository->save($execution2)->shouldBeCalled();
     $this->eventDispatcher->dispatch(Events::TASK_EXECUTION_CREATE, Argument::that(function (TaskExecutionEvent $event) use($tasks, $execution1) {
         return $event->getTask() === $tasks[1] && $event->getTaskExecution() === $execution1->reveal();
     }))->shouldBeCalled();
     $this->eventDispatcher->dispatch(Events::TASK_EXECUTION_CREATE, Argument::that(function (TaskExecutionEvent $event) use($tasks, $execution2) {
         return $event->getTask() === $tasks[2] && $event->getTaskExecution() === $execution2->reveal();
     }))->shouldBeCalled();
     $this->taskScheduler->scheduleTasks();
 }
 /**
  * Create task-execution.
  *
  * @param TaskInterface $task
  * @param \DateTime $scheduleTime
  * @param string $status
  *
  * @return TaskExecutionInterface
  */
 protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
 {
     $execution = $this->taskExecutionRepository->create($task, $scheduleTime);
     $execution->setStatus($status);
     $this->taskExecutionRepository->save($execution);
     if (self::$kernel->getContainer()->has('doctrine')) {
         $this->getEntityManager()->flush();
     }
     return $execution;
 }
Example #6
0
 /**
  * Schedule execution for given task.
  *
  * @param TaskInterface $task
  */
 protected function scheduleTask(TaskInterface $task)
 {
     if (null !== ($execution = $this->taskExecutionRepository->findPending($task))) {
         return;
     }
     if ($task->getInterval() === null && 0 < count($this->taskExecutionRepository->findByTask($task))) {
         return;
     }
     $scheduleTime = $task->getInterval() ? $task->getInterval()->getNextRunDate() : $task->getFirstExecution();
     $execution = $this->taskExecutionRepository->create($task, $scheduleTime);
     $execution->setStatus(TaskStatus::PLANNED);
     $this->eventDispatcher->dispatch(Events::TASK_EXECUTION_CREATE, new TaskExecutionEvent($task, $execution));
     $this->taskExecutionRepository->save($execution);
 }