Skip to content

[Scheduler] Separate id and description in message providers #52874

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

Merged
merged 1 commit into from
Jan 2, 2024
Merged
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
Expand Up @@ -9,6 +9,9 @@
#[AsCronTask(expression: '0 * * * *', timezone: 'Europe/Berlin', arguments: ['2'], schedule: 'dummy_task', method: 'method2')]
#[AsPeriodicTask(frequency: 5, arguments: [3], schedule: 'dummy_task')]
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', jitter: 60, arguments: ['4'], schedule: 'dummy_task', method: 'method4')]
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', arguments: ['9'], schedule: 'dummy_task', method: 'method5')]
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', arguments: ['9b'], schedule: 'dummy_task', method: 'method5')]
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', arguments: ['named' => '9'], schedule: 'dummy_task', method: 'method5')]
class DummyTask
{
public static array $calls = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function testAutoconfiguredScheduler()
$this->assertCount(779, $calls['__invoke']);
$this->assertSame([['2']], $calls['method2']);
$this->assertSame([['4']], $calls['method4']);
$this->assertSame([['9'], ['9b'], ['named' => '9']], $calls['method5']);
$this->assertSame([['5', 6], ['7', 8]], $calls['attributesOnMethod']);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Scheduler/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ private static function renderRecurringMessage(RecurringMessage $recurringMessag
return null;
}

return [(string) $trigger, $recurringMessage->getProvider()->getId(), $next];
$provider = $recurringMessage->getProvider();
$description = $provider instanceof \Stringable ? (string) $provider : $provider->getId();

return [(string) $trigger, $description, $next];
}
}
13 changes: 7 additions & 6 deletions src/Symfony/Component/Scheduler/RecurringMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ public static function trigger(TriggerInterface $trigger, object $message): self
return new self($trigger, $message);
}

$description = '';
try {
$description = $message instanceof \Stringable ? (string) $message : serialize($message);
} catch (\Exception) {
$description = $message::class;
if ($message instanceof \Stringable) {
try {
$description .= " ($message)";
} catch (\Exception) {
}
}
$description = sprintf('%s(%s)', $message::class, $description);

return new self($trigger, new StaticMessageProvider([$message], $description));
return new self($trigger, new StaticMessageProvider([$message], strtr(substr(base64_encode(hash('xxh128', serialize($message), true)), 0, 7), '/+', '._'), -7), $description));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, there was a syntax error on this line. I fixed that in a45f6cb. Please double-check that I didn't mess anything with the logic implemented here.

}

public function withJitter(int $maxSeconds = 60): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public function testExecuteWithScheduleWithoutTriggerShowingNoNextRunWithAllOpti
"schedule_name\n".
"-------------\n".
"\n".
" --------- ------------------------------- ---------- \n".
" Trigger Provider Next Run \n".
" --------- ------------------------------- ---------- \n".
" test stdClass(O:8:\"stdClass\":0:{}) - \n".
" --------- ------------------------------- ---------- \n".
" --------- ---------- ---------- \n".
" Trigger Provider Next Run \n".
" --------- ---------- ---------- \n".
" test stdClass - \n".
" --------- ---------- ---------- \n".
"\n", $tester->getDisplay(true));
}

Expand Down Expand Up @@ -143,11 +143,11 @@ public function testExecuteWithSchedule()
"schedule_name\n".
"-------------\n".
"\n".
" ------------------------------- ------------------------------- --------------------------------- \n".
" Trigger Provider Next Run \n".
" ------------------------------- ------------------------------- --------------------------------- \n".
" every first day of next month stdClass\(O:8:\"stdClass\":0:{}\) \w{3}, \d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2} (\+|-)\d{4} \n".
" ------------------------------- ------------------------------- --------------------------------- \n".
" ------------------------------- ---------- --------------------------------- \n".
" Trigger Provider Next Run \n".
" ------------------------------- ---------- --------------------------------- \n".
" every first day of next month stdClass \w{3}, \d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2} (\+|-)\d{4} \n".
" ------------------------------- ---------- --------------------------------- \n".
"\n/", $tester->getDisplay(true));
}
}
15 changes: 9 additions & 6 deletions src/Symfony/Component/Scheduler/Tests/RecurringMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ class RecurringMessageTest extends TestCase
{
public function testCanCreateHashedCronMessage()
{
$object = new class() {
public function __toString(): string
{
return 'my task';
}
};
$object = new DummyStringableMessage();

if (class_exists(Randomizer::class)) {
$this->assertSame('30 0 * * *', (string) RecurringMessage::cron('#midnight', $object)->getTrigger());
Expand All @@ -52,3 +47,11 @@ public function testUniqueId()
$this->assertNotSame($message1->getId(), $message2->getId());
}
}

class DummyStringableMessage implements \Stringable
{
public function __toString(): string
{
return 'my task';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function testToString()
$this->assertEquals([new \stdClass()], $messageProvider->getMessages($context));
$this->assertSame('', $messageProvider->getId());

$messageProvider = new CallbackMessageProvider(fn () => yield new \stdClass(), 'foo');
$messageProvider = new CallbackMessageProvider(fn () => yield new \stdClass(), 'foo', 'bar');
$this->assertInstanceOf(\Generator::class, $messageProvider->getMessages($context));
$this->assertSame('foo', $messageProvider->getId());
$this->assertSame('bar', (string) $messageProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

use Symfony\Component\Scheduler\Generator\MessageContext;

final class CallbackMessageProvider implements MessageProviderInterface
final class CallbackMessageProvider implements MessageProviderInterface, \Stringable
{
private \Closure $callback;

/**
* @param callable(MessageContext): iterable<object> $callback
*/
public function __construct(callable $callback, private string $id = '')
public function __construct(callable $callback, private string $id = '', private string $description = '')
{
$this->callback = $callback(...);
}
Expand All @@ -34,4 +34,9 @@ public function getId(): string
{
return $this->id;
}

public function __toString(): string
{
return $this->description ?: $this->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

use Symfony\Component\Scheduler\Generator\MessageContext;

final class StaticMessageProvider implements MessageProviderInterface
final class StaticMessageProvider implements MessageProviderInterface, \Stringable
{
/**
* @param array<object> $messages
*/
public function __construct(
private array $messages,
private string $id = '',
private string $description = '',
) {
}

Expand All @@ -33,4 +34,9 @@ public function getId(): string
{
return $this->id;
}

public function __toString(): string
{
return $this->description ?: $this->id;
}
}