shouldRun example

$newNextExecutionTime = $nextExecutionTime->modify(sprintf('+%d seconds', $taskEntity->getRunInterval()));

        if ($newNextExecutionTime < $now) {
            return $now;
        }

        return $newNextExecutionTime;
    }

    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());
        }
private function queueTask(ScheduledTaskEntity $taskEntity, Context $context): void
    {
        $taskClass = $taskEntity->getScheduledTaskClass();

        if (!\is_a($taskClass, ScheduledTask::class, true)) {
            throw new \RuntimeException(sprintf(
                'Tried to schedule "%s", but class does not extend ScheduledTask',
                $taskClass
            ));
        }

        if (!$taskClass::shouldRun($this->parameterBag)) {
            $this->scheduledTaskRepository->update([
                [
                    'id' => $taskEntity->getId(),
                    'nextExecutionTime' => $this->calculateNextExecutionTime($taskEntity),
                    'status' => ScheduledTaskDefinition::STATUS_SKIPPED,
                ],
            ]$context);

            return;
        }

        
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

/** * @internal * * @covers \Shopware\Elasticsearch\Framework\Indexing\CreateAliasTask */
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());
    }

class InvalidateCacheTaskTest extends TestCase
{
    public function testGetTaskName(): void
    {
        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());
    }
}
Home | Imprint | This part of the site doesn't use cookies.