-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger][Scheduler] Add AsCronTask & AsPeriodicTask attributes #51525
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Messenger/DummyTask.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger; | ||
|
||
use Symfony\Component\Scheduler\Attribute\AsCronTask; | ||
use Symfony\Component\Scheduler\Attribute\AsPeriodicTask; | ||
|
||
#[AsCronTask(expression: '* * * * *', arguments: [1], schedule: 'dummy')] | ||
#[AsCronTask(expression: '0 * * * *', timezone: 'Europe/Berlin', arguments: ['2'], schedule: 'dummy', method: 'method2')] | ||
#[AsPeriodicTask(frequency: 5, arguments: [3], schedule: 'dummy')] | ||
#[AsPeriodicTask(frequency: 'every day', from: '00:00:00', jitter: 60, arguments: ['4'], schedule: 'dummy', method: 'method4')] | ||
class DummyTask | ||
{ | ||
public static array $calls = []; | ||
|
||
#[AsPeriodicTask(frequency: 'every hour', from: '09:00:00', until: '17:00:00', arguments: ['b' => 6, 'a' => '5'], schedule: 'dummy')] | ||
#[AsCronTask(expression: '0 0 * * *', arguments: ['7', 8], schedule: 'dummy')] | ||
public function attributesOnMethod(string $a, int $b): void | ||
{ | ||
self::$calls[__FUNCTION__][] = [$a, $b]; | ||
} | ||
|
||
public function __call(string $name, array $arguments) | ||
{ | ||
self::$calls[$name][] = $arguments; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\Attribute; | ||
|
||
/** | ||
* A marker to call a service method from scheduler. | ||
* | ||
* @author valtzu <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class AsCronTask | ||
{ | ||
public function __construct( | ||
public readonly string $expression, | ||
public readonly ?string $timezone = null, | ||
public readonly ?int $jitter = null, | ||
public readonly array|string|null $arguments = null, | ||
public readonly string $schedule = 'default', | ||
public readonly ?string $method = null, | ||
public readonly array|string|null $transports = null, | ||
) { | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Scheduler/Attribute/AsPeriodicTask.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\Attribute; | ||
|
||
/** | ||
* A marker to call a service method from scheduler. | ||
* | ||
* @author valtzu <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class AsPeriodicTask | ||
{ | ||
public function __construct( | ||
public readonly string|int $frequency, | ||
public readonly ?string $from = null, | ||
public readonly ?string $until = null, | ||
public readonly ?int $jitter = null, | ||
public readonly array|string|null $arguments = null, | ||
public readonly string $schedule = 'default', | ||
public readonly ?string $method = null, | ||
public readonly array|string|null $transports = null, | ||
) { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Scheduler/Messenger/ServiceCallMessage.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\Messenger; | ||
|
||
/** | ||
* Represents a service call. | ||
* | ||
* @author valtzu <[email protected]> | ||
*/ | ||
class ServiceCallMessage implements \Stringable | ||
{ | ||
public function __construct( | ||
private readonly string $serviceId, | ||
private readonly string $method = '__invoke', | ||
private readonly array $arguments = [], | ||
) { | ||
} | ||
|
||
public function getServiceId(): string | ||
{ | ||
return $this->serviceId; | ||
} | ||
|
||
public function getMethod(): string | ||
{ | ||
return $this->method; | ||
} | ||
|
||
public function getArguments(): array | ||
{ | ||
return $this->arguments; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return "@$this->serviceId".('__invoke' !== $this->method ? "::$this->method" : ''); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Scheduler/Messenger/ServiceCallMessageHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\Messenger; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Handler to call any service. | ||
* | ||
* @author valtzu <[email protected]> | ||
*/ | ||
class ServiceCallMessageHandler | ||
{ | ||
public function __construct(private readonly ContainerInterface $serviceLocator) | ||
{ | ||
} | ||
|
||
public function __invoke(ServiceCallMessage $message): void | ||
{ | ||
$this->serviceLocator->get($message->getServiceId())->{$message->getMethod()}(...$message->getArguments()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no tests associated with this, can you add some @valtzu?
I've moved these to a new schedule to avoid breaking the existing tests (#51802).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabpot I'm not too familiar with the framework tests yet, and I'm also a bit busy now. I would need a week or two, if that's ok then I can try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, there is no rush. Ping me if you need help.