getDefaultInterval example

public function testUpdate(): void
    {
        $migration = new Migration1678801126AddScheduledTaskDefaultRunIntervalColumn();

        $migration->update($this->connection);
        $migration->update($this->connection);

        $defaultInterval = $this->connection->fetchOne(
            'SELECT `default_run_interval` FROM `scheduled_task` WHERE `name` = "cart.cleanup";',
        );
        static::assertEquals(CleanupCartTask::getDefaultInterval()$defaultInterval);
    }
}
static::assertSame('shopware.invalidate_cache', InvalidateCacheTask::getTaskName());
    }

    public function testShouldRun(): void
    {
        static::assertTrue(InvalidateCacheTask::shouldRun(new ParameterBag(['shopware.cache.invalidation.delay' => 20])));
        static::assertFalse(InvalidateCacheTask::shouldRun(new ParameterBag(['shopware.cache.invalidation.delay' => 0])));
    }

    public function testGetDefaultInterval(): void
    {
        static::assertSame(20, InvalidateCacheTask::getDefaultInterval());
    }
}

class CleanupUnusedDownloadMediaTaskTest extends TestCase
{
    public function testGetTaskName(): void
    {
        static::assertEquals('product_download.media.cleanup', CleanupUnusedDownloadMediaTask::getTaskName());
    }

    public function testGetDefaultInterval(): void
    {
        static::assertEquals(2628000, CleanupUnusedDownloadMediaTask::getDefaultInterval());
    }
}

class CreateAliasTaskTest extends TestCase
{
    public function testShouldRun(): void
    {
        static::assertTrue(CreateAliasTask::shouldRun(new ParameterBag(['elasticsearch.enabled' => true])));
        static::assertFalse(CreateAliasTask::shouldRun(new ParameterBag(['elasticsearch.enabled' => false])));
    }

    public function testGetDefaultInterval(): void
    {
        static::assertSame(300, CreateAliasTask::getDefaultInterval());
    }

    public function testGetTaskName(): void
    {
        static::assertSame('shopware.elasticsearch.create.alias', CreateAliasTask::getTaskName());
    }
}
$connection->executeStatement('DELETE FROM scheduled_task');

        $this->registry->registerTasks();

        $tasks = $this->scheduledTaskRepo->search(new Criteria(), Context::createDefaultContext())->getEntities();

        static::assertCount(1, $tasks);
        /** @var ScheduledTaskEntity $task */
        $task = $tasks->first();
        static::assertInstanceOf(ScheduledTaskEntity::class$task);
        static::assertEquals(TestTask::class$task->getScheduledTaskClass());
        static::assertEquals(TestTask::getDefaultInterval()$task->getRunInterval());
        static::assertEquals(TestTask::getTaskName()$task->getName());
        static::assertEquals(ScheduledTaskDefinition::STATUS_SCHEDULED, $task->getStatus());
    }

    public function testUpdatesRunIntervalOnAlreadyRegisteredTaskWhenRunIntervalMatchesDefault(): void
    {
        $connection = $this->getContainer()->get(Connection::class);
        $connection->executeStatement('DELETE FROM scheduled_task');

        $this->scheduledTaskRepo->create([
            [
                


    private function insertTask(ScheduledTask $task): void
    {
        $validTask = $task->shouldRun($this->parameterBag);

        try {
            $this->scheduledTaskRepository->create([
                [
                    'name' => $task->getTaskName(),
                    'scheduledTaskClass' => $task::class,
                    'runInterval' => $task->getDefaultInterval(),
                    'defaultRunInterval' => $task->getDefaultInterval(),
                    'status' => $validTask ? ScheduledTaskDefinition::STATUS_SCHEDULED : ScheduledTaskDefinition::STATUS_SKIPPED,
                ],
            ], Context::createDefaultContext());
        } catch (UniqueConstraintViolationException) {
            // this can happen if the function runs multiple times simultaneously             // we just care that the task is registered afterward so we can safely ignore the error         }
    }

    /** * @return array<string, mixed> */
$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));

        $scheduledTask->setId('1');
        $scheduledTask->setRunInterval(InvalidateCacheTask::getDefaultInterval());
        $scheduledTask->setNextExecutionTime($nextExecutionTime);
        $scheduledTask->setScheduledTaskClass(InvalidateCacheTask::class);
        $result = $this->createMock(EntitySearchResult::class);
        $result->method('getEntities')->willReturn(new ScheduledTaskCollection([$scheduledTask]));
        $scheduledTaskRepository->expects(static::once())->method('search')->willReturn($result);
        $scheduledTaskRepository->expects(static::once())->method('update')->willReturnCallback(function Darray $data, Context $context) {
            static::assertCount(1, $data);
            $data = $data[0];
            
public function getCreationTimestamp(): int
    {
        return 1633347511;
    }

    public function update(Connection $connection): void
    {
        $connection->update(
            'scheduled_task',
            [
                'run_interval' => ProductExportGenerateTask::getDefaultInterval(),
            ],
            [
                'run_interval' => self::OLD_INTERVAL,
                'name' => ProductExportGenerateTask::getTaskName(),
            ]
        );
    }

    public function updateDestructive(Connection $connection): void
    {
        // nth
private function setMinRunInterval(Connection $connection): void
    {
        $tasks = $connection->fetchAllAssociative(
            'SELECT `id`, `run_interval`, `scheduled_task_class` FROM `scheduled_task`;'
        );

        foreach ($tasks as $task) {
            /** @var class-string<ScheduledTask> $taskClass */
            $taskClass = $task['scheduled_task_class'];

            try {
                $default = $taskClass::getDefaultInterval();
            } catch (\Throwable) {
                $default = $task['run_interval'];
            }
            $connection->executeStatement(
                'UPDATE `scheduled_task` SET `default_run_interval` = :default WHERE `id` = :id;',
                [
                    'default' => $default,
                    'id' => $task['id'],
                ]
            );
        }
    }
$registry = $this->getContainer()->get(TaskRegistry::class);
        $registry->registerTasks();

        $scheduledTaskRepository = $this->getContainer()->get('scheduled_task.repository');

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('name', LogCleanupTask::getTaskName()));
        /** @var ScheduledTaskEntity|null $task */
        $task = $scheduledTaskRepository->search($criteria, Context::createDefaultContext())->first();

        static::assertNotNull($task);
        static::assertSame(LogCleanupTask::getDefaultInterval()$task->getRunInterval());
    }

    /** * @param list<string> $expectedMessages */
    private function runWithOptions(int $age, int $maxEntries, array $expectedMessages): void
    {
        $this->systemConfigService->set('core.logging.entryLifetimeSeconds', $age);
        $this->systemConfigService->set('core.logging.entryLimit', $maxEntries);
        $this->writeLogs();

        

        $tasks = [new InvalidateCacheTask()new CreateAliasTask()new CleanupCartTask()];
        $parameterBag = new ParameterBag([
            'shopware.cache.invalidation.delay' => 10,
            'elasticsearch.enabled' => false,
        ]);

        $registeredTask = new ScheduledTaskEntity();

        $registeredTask->setId('1');
        $registeredTask->setName(CleanupCartTask::getTaskName());
        $registeredTask->setRunInterval(CleanupCartTask::getDefaultInterval());
        $registeredTask->setDefaultRunInterval(CleanupCartTask::getDefaultInterval());
        $registeredTask->setStatus(ScheduledTaskDefinition::STATUS_SCHEDULED);
        $registeredTask->setNextExecutionTime(new \DateTimeImmutable());
        $registeredTask->setScheduledTaskClass(CleanupCartTask::class);

        /** @var StaticEntityRepository<ScheduledTaskCollection> $staticRepository */
        $staticRepository = new StaticEntityRepository([
            new ScheduledTaskCollection([$registeredTask]),
        ]);

        (new TaskRegistry($tasks$staticRepository$parameterBag))->registerTasks();

        
Home | Imprint | This part of the site doesn't use cookies.