ScheduledTaskCollection example

$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];
            static::assertArrayHasKey('id', $data);
            static::assertArrayHasKey('nextExecutionTime', $data);
            static::assertArrayHasKey('status', $data);
            static::assertEquals('1', $data['id']);
            static::assertEquals(ScheduledTaskDefinition::STATUS_SKIPPED, $data['status']);

            return new EntityWrittenContainerEvent($contextnew NestedEventCollection()[]);
        });
$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();

        static::assertSame(
            [
                [
                    [
                        'name' => InvalidateCacheTask::getTaskName(),
                        'scheduledTaskClass' => InvalidateCacheTask::class,
                        'runInterval' => InvalidateCacheTask::getDefaultInterval(),
                        

        $entity = new ScheduledTaskEntity();
        $entity->setId('test');
        $entity->setName('TestTask.ID');
        $entity->setNextExecutionTime(new \DateTime());
        $entity->setRunInterval(100);
        $entity->setStatus(ScheduledTaskDefinition::STATUS_QUEUED);

        $taskRegistry = $this->createMock(TaskRegistry::class);
        $taskRegistry
            ->method('getAllTasks')
            ->willReturn(new ScheduledTaskCollection([$entity]));

        $command = new ListScheduledTaskCommand($taskRegistry);

        $tester = new CommandTester($command);
        $tester->execute([]);

        static::assertStringContainsString('Name', $tester->getDisplay());
        static::assertStringContainsString('Next execution', $tester->getDisplay());
        static::assertStringContainsString('Last execution', $tester->getDisplay());
        static::assertStringContainsString('Run interval', $tester->getDisplay());
        static::assertStringContainsString('Status', $tester->getDisplay());
        
/** * @internal * * @covers \Shopware\Core\Framework\MessageQueue\ScheduledTask\Scheduler\TaskRunner */
class TaskRunnerTest extends TestCase
{
    public function testNonExistingTask(): void
    {
        /** @var StaticEntityRepository<ScheduledTaskCollection> $scheduledTaskRepository */
        $scheduledTaskRepository = new StaticEntityRepository([new ScheduledTaskCollection()]);
        $taskRunner = new TaskRunner([]$scheduledTaskRepository);

        $this->expectException(MessageQueueException::class);
        $this->expectExceptionMessage('Cannot find scheduled task by name "non-existing-task"');
        $taskRunner->runSingleTask('non-existing-task', Context::createDefaultContext());
    }

    public function testRunTaskTriggersHandler(): void
    {
        $handler = new TestTaskHandler();
        $handler2 = new TestTask2Handler();
        
Home | Imprint | This part of the site doesn't use cookies.