Skip to content

Commit a84d42b

Browse files
bug #52874 [Scheduler] Separate id and description in message providers (valtzu)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Scheduler] Separate id and description in message providers | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #52853 | License | MIT Separate id and description in message providers to keep `debug:schedule` output clean while allowing arbitrary distinct id. Commits ------- d73bf83 [Scheduler] Separate id and description in message providers
2 parents 4f3822e + d73bf83 commit a84d42b

File tree

9 files changed

+50
-27
lines changed

9 files changed

+50
-27
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Messenger/DummyTask.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#[AsCronTask(expression: '0 * * * *', timezone: 'Europe/Berlin', arguments: ['2'], schedule: 'dummy_task', method: 'method2')]
1010
#[AsPeriodicTask(frequency: 5, arguments: [3], schedule: 'dummy_task')]
1111
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', jitter: 60, arguments: ['4'], schedule: 'dummy_task', method: 'method4')]
12+
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', arguments: ['9'], schedule: 'dummy_task', method: 'method5')]
13+
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', arguments: ['9b'], schedule: 'dummy_task', method: 'method5')]
14+
#[AsPeriodicTask(frequency: '1 day', from: '2023-10-25 09:59:00Z', arguments: ['named' => '9'], schedule: 'dummy_task', method: 'method5')]
1215
class DummyTask
1316
{
1417
public static array $calls = [];

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SchedulerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function testAutoconfiguredScheduler()
8484
$this->assertCount(779, $calls['__invoke']);
8585
$this->assertSame([['2']], $calls['method2']);
8686
$this->assertSame([['4']], $calls['method4']);
87+
$this->assertSame([['9'], ['9b'], ['named' => '9']], $calls['method5']);
8788
$this->assertSame([['5', 6], ['7', 8]], $calls['attributesOnMethod']);
8889
}
8990

src/Symfony/Component/Scheduler/Command/DebugCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ private static function renderRecurringMessage(RecurringMessage $recurringMessag
114114
return null;
115115
}
116116

117-
return [(string) $trigger, $recurringMessage->getProvider()->getId(), $next];
117+
$provider = $recurringMessage->getProvider();
118+
$description = $provider instanceof \Stringable ? (string) $provider : $provider->getId();
119+
120+
return [(string) $trigger, $description, $next];
118121
}
119122
}

src/Symfony/Component/Scheduler/RecurringMessage.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ public static function trigger(TriggerInterface $trigger, object $message): self
7575
return new self($trigger, $message);
7676
}
7777

78-
$description = '';
79-
try {
80-
$description = $message instanceof \Stringable ? (string) $message : serialize($message);
81-
} catch (\Exception) {
78+
$description = $message::class;
79+
if ($message instanceof \Stringable) {
80+
try {
81+
$description .= " ($message)";
82+
} catch (\Exception) {
83+
}
8284
}
83-
$description = sprintf('%s(%s)', $message::class, $description);
8485

85-
return new self($trigger, new StaticMessageProvider([$message], $description));
86+
return new self($trigger, new StaticMessageProvider([$message], strtr(substr(base64_encode(hash('xxh128', serialize($message), true)), 0, 7), '/+', '._'), -7), $description));
8687
}
8788

8889
public function withJitter(int $maxSeconds = 60): self

src/Symfony/Component/Scheduler/Tests/Command/DebugCommandTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ public function testExecuteWithScheduleWithoutTriggerShowingNoNextRunWithAllOpti
106106
"schedule_name\n".
107107
"-------------\n".
108108
"\n".
109-
" --------- ------------------------------- ---------- \n".
110-
" Trigger Provider Next Run \n".
111-
" --------- ------------------------------- ---------- \n".
112-
" test stdClass(O:8:\"stdClass\":0:{}) - \n".
113-
" --------- ------------------------------- ---------- \n".
109+
" --------- ---------- ---------- \n".
110+
" Trigger Provider Next Run \n".
111+
" --------- ---------- ---------- \n".
112+
" test stdClass - \n".
113+
" --------- ---------- ---------- \n".
114114
"\n", $tester->getDisplay(true));
115115
}
116116

@@ -143,11 +143,11 @@ public function testExecuteWithSchedule()
143143
"schedule_name\n".
144144
"-------------\n".
145145
"\n".
146-
" ------------------------------- ------------------------------- --------------------------------- \n".
147-
" Trigger Provider Next Run \n".
148-
" ------------------------------- ------------------------------- --------------------------------- \n".
149-
" 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".
150-
" ------------------------------- ------------------------------- --------------------------------- \n".
146+
" ------------------------------- ---------- --------------------------------- \n".
147+
" Trigger Provider Next Run \n".
148+
" ------------------------------- ---------- --------------------------------- \n".
149+
" 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".
150+
" ------------------------------- ---------- --------------------------------- \n".
151151
"\n/", $tester->getDisplay(true));
152152
}
153153
}

src/Symfony/Component/Scheduler/Tests/RecurringMessageTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ class RecurringMessageTest extends TestCase
2020
{
2121
public function testCanCreateHashedCronMessage()
2222
{
23-
$object = new class() {
24-
public function __toString(): string
25-
{
26-
return 'my task';
27-
}
28-
};
23+
$object = new DummyStringableMessage();
2924

3025
if (class_exists(Randomizer::class)) {
3126
$this->assertSame('30 0 * * *', (string) RecurringMessage::cron('#midnight', $object)->getTrigger());
@@ -52,3 +47,11 @@ public function testUniqueId()
5247
$this->assertNotSame($message1->getId(), $message2->getId());
5348
}
5449
}
50+
51+
class DummyStringableMessage implements \Stringable
52+
{
53+
public function __toString(): string
54+
{
55+
return 'my task';
56+
}
57+
}

src/Symfony/Component/Scheduler/Tests/Trigger/CallbackMessageProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public function testToString()
2929
$this->assertEquals([new \stdClass()], $messageProvider->getMessages($context));
3030
$this->assertSame('', $messageProvider->getId());
3131

32-
$messageProvider = new CallbackMessageProvider(fn () => yield new \stdClass(), 'foo');
32+
$messageProvider = new CallbackMessageProvider(fn () => yield new \stdClass(), 'foo', 'bar');
3333
$this->assertInstanceOf(\Generator::class, $messageProvider->getMessages($context));
3434
$this->assertSame('foo', $messageProvider->getId());
35+
$this->assertSame('bar', (string) $messageProvider);
3536
}
3637
}

src/Symfony/Component/Scheduler/Trigger/CallbackMessageProvider.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
use Symfony\Component\Scheduler\Generator\MessageContext;
1515

16-
final class CallbackMessageProvider implements MessageProviderInterface
16+
final class CallbackMessageProvider implements MessageProviderInterface, \Stringable
1717
{
1818
private \Closure $callback;
1919

2020
/**
2121
* @param callable(MessageContext): iterable<object> $callback
2222
*/
23-
public function __construct(callable $callback, private string $id = '')
23+
public function __construct(callable $callback, private string $id = '', private string $description = '')
2424
{
2525
$this->callback = $callback(...);
2626
}
@@ -34,4 +34,9 @@ public function getId(): string
3434
{
3535
return $this->id;
3636
}
37+
38+
public function __toString(): string
39+
{
40+
return $this->description ?: $this->id;
41+
}
3742
}

src/Symfony/Component/Scheduler/Trigger/StaticMessageProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
use Symfony\Component\Scheduler\Generator\MessageContext;
1515

16-
final class StaticMessageProvider implements MessageProviderInterface
16+
final class StaticMessageProvider implements MessageProviderInterface, \Stringable
1717
{
1818
/**
1919
* @param array<object> $messages
2020
*/
2121
public function __construct(
2222
private array $messages,
2323
private string $id = '',
24+
private string $description = '',
2425
) {
2526
}
2627

@@ -33,4 +34,9 @@ public function getId(): string
3334
{
3435
return $this->id;
3536
}
37+
38+
public function __toString(): string
39+
{
40+
return $this->description ?: $this->id;
41+
}
3642
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy