Exemplo n.º 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);
     }
 }
Exemplo n.º 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());
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $handlerClass = $input->getArgument('handlerClass');
     $workload = $input->getArgument('workload');
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $output->writeln(sprintf('Run command "%s" with workload "%s"', $handlerClass, $workload));
     }
     $result = $this->handlerFactory->create($handlerClass)->handle($workload);
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         $output->writeln(sprintf('Result: %s', json_encode($result)));
     }
 }