Skip to content

[Scheduler] Fix AsCronTask not passing arguments to command #60741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Scheduler\Attribute\AsCronTask;

#[AsCronTask(expression: '* * * * *', schedule: 'dummy_command')]
#[AsCronTask(expression: '0 * * * *', arguments: 'test', schedule: 'dummy_command')]
#[AsCommand(name: 'test:dummy-command')]
class DummyCommand extends Command
{
public static array $calls = [];

public function configure(): void
{
$this->addArgument('dummy-argument', InputArgument::OPTIONAL);
}

public function execute(InputInterface $input, ?OutputInterface $output = null): int
{
self::$calls[__FUNCTION__][] = $input->getArgument('dummy-argument');

return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\BarMessage;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyCommand;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummySchedule;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyTask;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\FooMessage;
Expand Down Expand Up @@ -88,6 +89,29 @@ public function testAutoconfiguredScheduler()
$this->assertSame([['5', 6], ['7', 8]], $calls['attributesOnMethod']);
}

public function testAutoconfiguredSchedulerCommand()
{
$container = self::getContainer();
$container->set('clock', $clock = new MockClock('2023-10-26T08:59:59Z'));

$this->assertTrue($container->get('receivers')->has('scheduler_dummy_command'));
$this->assertInstanceOf(SchedulerTransport::class, $cron = $container->get('receivers')->get('scheduler_dummy_command'));
$bus = $container->get(MessageBusInterface::class);

$getCalls = static function (float $sleep) use ($clock, $cron, $bus) {
DummyCommand::$calls = [];
$clock->sleep($sleep);
foreach ($cron->get() as $message) {
$bus->dispatch($message->with(new ReceivedStamp('scheduler_dummy_command')));
}

return DummyCommand::$calls;
};

$this->assertSame([], $getCalls(0));
$this->assertSame(['execute' => [0 => null, 1 => 'test']], $getCalls(1));
}

public function testSchedulerWithCustomTransport()
{
$container = self::getContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ services:
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyTaskWithCustomReceiver:
autoconfigure: true

Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger\DummyCommand:
autoconfigure: true

clock:
synthetic: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function process(ContainerBuilder $container): void
if ($serviceDefinition->hasTag('console.command')) {
/** @var AsCommand|null $attribute */
$attribute = ($container->getReflectionClass($serviceDefinition->getClass())->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
$message = new Definition(RunCommandMessage::class, [$attribute?->name ?? $serviceDefinition->getClass()::getDefaultName().(empty($tagAttributes['arguments']) ? '' : " {$tagAttributes['arguments']}")]);
$commandName = $attribute?->name ?? $serviceDefinition->getClass()::getDefaultName();

$message = new Definition(RunCommandMessage::class, [$commandName.(empty($tagAttributes['arguments']) ? '' : " {$tagAttributes['arguments']}")]);
} else {
$message = new Definition(ServiceCallMessage::class, [$serviceId, $tagAttributes['method'] ?? '__invoke', (array) ($tagAttributes['arguments'] ?? [])]);
}
Expand Down
Loading