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; }
/** * 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); }