/** * {@inheritdoc} */ public function run() { /** @var TaskInterface $task */ foreach ($this->storage->findScheduled() as $task) { if (!$this->registry->has($task->getTaskName())) { $this->warning($this->getNoHandlerFoundMessage($task)); continue; } $this->eventDispatcher->dispatch(Events::TASK_BEFORE, new TaskEvent($task)); try { $result = $this->registry->run($task->getTaskName(), $task->getWorkload()); $task->setResult($result); $task->setCompleted(); $this->storage->persist($task); $this->eventDispatcher->dispatch(Events::TASK_PASSED, new TaskEvent($task)); } catch (\Exception $ex) { // TODO set task failed. failed tasks, can be restarted by adding a flag to run command. $this->eventDispatcher->dispatch(Events::TASK_FAILED, new TaskFailedEvent($task, $ex)); throw $ex; } $this->eventDispatcher->dispatch(Events::TASK_AFTER, new TaskEvent($task)); // TODO move to event-dispatcher if ($task instanceof FrequentTaskInterface) { $task->scheduleNext($this); } } }
public function testRun() { $this->storage->clear(); $this->scheduler->schedule(new Task('test', 'workload')); $this->scheduler->run(); $scheduled = $this->storage->findScheduled(); $this->assertCount(0, $scheduled); $all = $this->storage->findAll(); $this->assertCount(1, $all); $this->assertEquals('test', $all[0]->getTaskName()); $this->assertEquals('workload', $all[0]->getWorkload()); $this->assertTrue($all[0]->isCompleted()); $this->assertEquals('daolkrow', $all[0]->getResult()); }