-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Use attributes to schedule task executions
- Loading branch information
1 parent
376a0a7
commit 274525e
Showing
16 changed files
with
198 additions
and
136 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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
16 changes: 16 additions & 0 deletions
16
packages/bluesprints/src/Scheduler/Attribute/ScheduledTask.php
This file contains 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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueSprints\Scheduler\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
final readonly class ScheduledTask | ||
{ | ||
public function __construct(public string $identifier, public int $interval, public array $config) | ||
{ | ||
} | ||
|
||
} |
This file contains 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
44 changes: 44 additions & 0 deletions
44
packages/bluesprints/src/Scheduler/DependencyInjection/SchedulerTaskCompilerPass.php
This file contains 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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueSprints\Scheduler\DependencyInjection; | ||
|
||
use ReflectionClass; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
use Symfony\Component\DependencyInjection\Reference; | ||
use VerteXVaaR\BlueSprints\Scheduler\Attribute\ScheduledTask; | ||
|
||
use VerteXVaaR\BlueSprints\Scheduler\SchedulerTaskRegistry; | ||
|
||
use function array_keys; | ||
|
||
class SchedulerTaskCompilerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$scheduledTasks = []; | ||
|
||
$tasks = $container->findTaggedServiceIds('vertexvaar.bluesprints.scheduler.scheduled_task'); | ||
foreach (array_keys($tasks) as $task) { | ||
$taskDefinition = $container->getDefinition($task); | ||
$taskClass = $taskDefinition->getClass(); | ||
$taskReflection = new ReflectionClass($taskClass); | ||
$attributes = $taskReflection->getAttributes(ScheduledTask::class); | ||
foreach ($attributes as $attribute) { | ||
/** @var ScheduledTask $scheduledTask */ | ||
$scheduledTask = $attribute->newInstance(); | ||
$scheduledTasks[$taskClass][$scheduledTask->identifier] = [ | ||
'service' => new Reference($taskClass), | ||
'config' => $scheduledTask->config, | ||
'interval' => $scheduledTask->interval, | ||
]; | ||
} | ||
} | ||
|
||
$schedulerTaskRegistry = $container->getDefinition(SchedulerTaskRegistry::class); | ||
$schedulerTaskRegistry->setArgument('$tasks', $scheduledTasks); | ||
} | ||
} |
This file contains 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 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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueSprints\Scheduler; | ||
|
||
use VerteXVaaR\BlueSprints\Mvcr\Repository\Repository; | ||
use VerteXVaaR\BlueSprints\Scheduler\Model\TaskExecution; | ||
use VerteXVaaR\BlueSprints\Scheduler\Task\AbstractTask; | ||
|
||
use function strtr; | ||
use function time; | ||
|
||
use const PHP_INT_MAX; | ||
|
||
readonly class Scheduler | ||
{ | ||
|
||
public function __construct( | ||
private SchedulerTaskRegistry $schedulerTaskRegistry, | ||
private Repository $repository, | ||
) { | ||
} | ||
|
||
/** | ||
* Starts all the scheduled tasks. They are not executed in parallel. | ||
*/ | ||
public function run(CliRequest $cliRequest): void | ||
{ | ||
$now = time(); | ||
|
||
foreach ($this->schedulerTaskRegistry->tasks as $taskClass => $identifiers) { | ||
foreach ($identifiers as $identifier => $taskConfiguration) { | ||
$taskExecution = $this->getTaskExecution($taskClass . '->' . $identifier); | ||
if (($now - $taskExecution->lastExecution) >= $taskConfiguration['interval']) { | ||
/** @var AbstractTask $task */ | ||
$task = $taskConfiguration['service']; | ||
$task->run($identifier, $cliRequest, $taskConfiguration['config'] ?? []); | ||
$taskExecution->lastExecution = $now; | ||
$this->repository->persist($taskExecution); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function getTaskExecution(string $taskName): TaskExecution | ||
{ | ||
$identifier = strtr($taskName, '\\', '_'); | ||
$taskExecution = $this->repository->findByIdentifier(TaskExecution::class, $identifier); | ||
if (null === $taskExecution) { | ||
$taskExecution = new TaskExecution($identifier); | ||
$taskExecution->lastExecution = -PHP_INT_MAX; | ||
$this->repository->persist($taskExecution); | ||
} | ||
return $taskExecution; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/bluesprints/src/Scheduler/SchedulerTaskRegistry.php
This file contains 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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueSprints\Scheduler; | ||
|
||
readonly class SchedulerTaskRegistry | ||
{ | ||
public function __construct(public array $tasks) | ||
{ | ||
} | ||
} |
This file contains 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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueSprints\Scheduler\Task; | ||
|
||
use VerteXVaaR\BlueSprints\Scheduler\CliRequest; | ||
|
||
abstract class AbstractTask implements Task | ||
{ | ||
/** | ||
* Does not return anything. Write with ->printLine() instead. | ||
*/ | ||
abstract public function run(string $identifier, CliRequest $cliRequest, array $config): void; | ||
|
||
final protected function printLine(string $line): void | ||
{ | ||
echo $line . PHP_EOL; | ||
} | ||
} |
This file contains 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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueSprints\Scheduler\Task; | ||
|
||
interface Task | ||
{ | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueDist\Scheduler; | ||
|
||
use VerteXVaaR\BlueSprints\Scheduler\Attribute\ScheduledTask; | ||
use VerteXVaaR\BlueSprints\Scheduler\CliRequest; | ||
use VerteXVaaR\BlueSprints\Scheduler\Task\AbstractTask; | ||
|
||
use function var_export; | ||
|
||
#[ScheduledTask(identifier: 'First schedule of example task', interval: 10, config: ['first', 'second'])] | ||
#[ScheduledTask(identifier: 'Second schedule of example task', interval: 5, config: ['foo', 'bar'])] | ||
class ExampleScheduledTask extends AbstractTask | ||
{ | ||
public function run(string $identifier, CliRequest $cliRequest, array $config): void | ||
{ | ||
$verbose = $cliRequest->flagExists('--verbose'); | ||
$this->printLine(sprintf("Running %s with options: %s", $identifier, var_export($config, true))); | ||
$verbose ? $this->printLine('Checking if arg "string" exists') : null; | ||
if ($cliRequest->hasArgument('string')) { | ||
$verbose ? $this->printLine('arg "string" exists, printing "string" value') : null; | ||
$this->printLine($cliRequest->getArgument('string')); | ||
} else { | ||
$verbose ? $this->printLine('arg "string" does not exist') : null; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.