queueScheduledTasks example

 Context::createDefaultContext());

        $this->messageBus->expects(static::once())
            ->method('dispatch')
            ->with(static::callback(function DTestTask $task) use ($taskId) {
                static::assertEquals($taskId$task->getTaskId());

                return true;
            }))
            ->willReturn(new Envelope(new TestTask()));

        $this->scheduler->queueScheduledTasks();

        /** @var ScheduledTaskEntity $task */
        $task = $this->scheduledTaskRepo->search(new Criteria([$taskId]), Context::createDefaultContext())->get($taskId);
        static::assertEquals(ScheduledTaskDefinition::STATUS_QUEUED, $task->getStatus());
    }

    public function testScheduleTasksDoesntScheduleFutureTask(): void
    {
        $this->connection->executeStatement('DELETE FROM scheduled_task');

        $taskId = Uuid::randomHex();
        
$scheduledTaskRepository = $this->createMock(EntityRepository::class);
        $scheduledTaskRepository->expects(static::never())->method('update');

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(static::never())->method('dispatch');
        $scheduler = new TaskScheduler(
            $scheduledTaskRepository,
            $bus,
            new ParameterBag()
        );

        $scheduler->queueScheduledTasks();
    }

    public function testScheduleShouldNotRunTask(): void
    {
        $scheduledTaskRepository = $this->createMock(EntityRepository::class);

        $scheduledTask = new ScheduledTaskEntity();

        $nextExecutionTime = new \DateTimeImmutable();
        $nextExecutionTime = $nextExecutionTime->modify(sprintf('-%d seconds', InvalidateCacheTask::getDefaultInterval() + 100));

        

    /** * @internal */
    public function __construct(private readonly TaskScheduler $taskScheduler)
    {
    }

    #[Route(path: '/api/_action/scheduled-task/run', name: 'api.action.scheduled-task.run', methods: ['POST'])]     public function runScheduledTasks(): JsonResponse
    {
        $this->taskScheduler->queueScheduledTasks();

        return new JsonResponse(['message' => 'Success']);
    }

    #[Route(path: '/api/_action/scheduled-task/min-run-interval', name: 'api.action.scheduled-task.min-run-interval', methods: ['GET'])]     public function getMinRunInterval(): JsonResponse
    {
        return new JsonResponse(['minRunInterval' => $this->taskScheduler->getMinRunInterval()]);
    }
}
protected function configure(): void
    {
        $this
            ->addOption('memory-limit', 'm', InputOption::VALUE_REQUIRED, 'The memory limit the worker can consume')
            ->addOption('time-limit', 't', InputOption::VALUE_REQUIRED, 'The time limit in seconds the worker can run')
            ->addOption('no-wait', null, InputOption::VALUE_NONE, 'Do not wait for next cycle of scheduled tasks');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        if ($input->getOption('no-wait')) {
            $this->scheduler->queueScheduledTasks();

            $output->writeln('Scheduled tasks has been queued');

            return Command::SUCCESS;
        }

        $startTime = microtime(true);
        $endTime = null;
        $timeLimit = (int) $input->getOption('time-limit');
        if ($timeLimit) {
            $endTime = $startTime + $timeLimit;
        }
Home | Imprint | This part of the site doesn't use cookies.