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(); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $handlerClass = $input->getArgument('handlerClass'); $workload = $input->getArgument('workload'); $cronExpression = $input->getOption('cron-expression'); $executionDateString = $input->getOption('execution-date'); $endDateString = $input->getOption('end-date'); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handlerClass, $workload)); } $taskBuilder = $this->scheduler->createTask($handlerClass, $workload); if ($cronExpression !== null) { $endDate = null; if ($endDateString !== null) { $endDate = new \DateTime($endDateString); } $taskBuilder->cron($cronExpression, new \DateTime(), $endDate); } if ($executionDateString !== null) { $taskBuilder->executeAt(new \DateTime($executionDateString)); } $taskBuilder->schedule(); }
/** * {@inheritdoc} */ public function schedule() { $this->taskScheduler->addTask($this->task); return $this->task; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->runner->runTasks(); $this->scheduler->scheduleTasks(); }