Skip to content

Commit

Permalink
Added ReactPHP EventLoop and standalone cron runner engine
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Aug 18, 2023
1 parent 46fec47 commit 72b84b7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Okvpn\Bundle\CronBundle\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
class AsPeriodicalTask
class AsPeriodicTask
{
public $interval;
public $lock;
Expand All @@ -15,7 +15,7 @@ class AsPeriodicalTask
public $jitter;

public function __construct(
int $interval,
/*int|string */ $interval,
bool $lock = null,
bool $async = null,
bool $messenger = null,
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getConfigTreeBuilder(): TreeBuilder
// this will leave the search functionality and if you need to update the index, you can do it manually
$rootNode->children()
->scalarNode('timezone')->defaultNull()->end()
->scalarNode('loop_engine')->defaultNull()->end()
->arrayNode('messenger')
->children()
->booleanNode('enable')->defaultFalse()->end()
Expand Down Expand Up @@ -63,6 +64,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->integerNode('timeout')->end()
->scalarNode('interval')->end()
->end()
->end()
->end();
Expand Down
7 changes: 5 additions & 2 deletions src/DependencyInjection/OkvpnCronExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Okvpn\Bundle\CronBundle\DependencyInjection;

use Okvpn\Bundle\CronBundle\Attribute\AsCron;
use Okvpn\Bundle\CronBundle\Attribute\AsPeriodicalTask;
use Okvpn\Bundle\CronBundle\Attribute\AsPeriodicTask;
use Okvpn\Bundle\CronBundle\CronServiceInterface;
use Okvpn\Bundle\CronBundle\CronSubscriberInterface;
use Okvpn\Bundle\CronBundle\Loader\ScheduleLoaderInterface;
use Okvpn\Bundle\CronBundle\Middleware\MiddlewareEngineInterface;
use Okvpn\Bundle\CronBundle\Model;
use Okvpn\Bundle\CronBundle\Runner\ScheduleLoopInterface;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -53,6 +54,8 @@ public function load(array $configs, ContainerBuilder $container): void
->replaceArgument(0, new Reference($config['lock_factory']));
}

$container->setAlias(ScheduleLoopInterface::class, $config['loop_engine'] ?? 'okvpn_cron.standalone_loop');

$tasks = [];
foreach (($config['tasks'] ?? []) as $task) {
$task['shell'] = $task['shell'] ?? true;
Expand Down Expand Up @@ -93,7 +96,7 @@ public function load(array $configs, ContainerBuilder $container): void
$definition->addTag('okvpn.cron', $cron->getAttributes());
});

$container->registerAttributeForAutoconfiguration(AsPeriodicalTask::class, static function (ChildDefinition $definition, AsPeriodicalTask $cron) {
$container->registerAttributeForAutoconfiguration(AsPeriodicTask::class, static function (ChildDefinition $definition, AsPeriodicTask $cron) {
$definition->addTag('okvpn.cron', $cron->getAttributes());
});
}
Expand Down
23 changes: 12 additions & 11 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ services:
- '@okvpn_cron.checker'
- '%okvpn.config.cron_timezone%'
- '@?clock'
- '@okvpn_cron.schedule_loop'
- '@Okvpn\Bundle\CronBundle\Runner\ScheduleLoopInterface'
- '@Okvpn\Bundle\CronBundle\Runner\TimerStorage'
tags:
- { name: okvpn_cron.middleware, priority: 20 }
Expand Down Expand Up @@ -94,17 +94,18 @@ services:

Okvpn\Bundle\CronBundle\Runner\TimerStorage: ~

# okvpn_cron.schedule_loop:
# class: Okvpn\Bundle\CronBundle\Runner\StandaloneLoop
# arguments:
# - '@?clock'
# - '%okvpn.config.cron_timezone%'
#
okvpn_cron.schedule_loop:
alias: Okvpn\Bundle\CronBundle\React\ReactLoopAdapter
okvpn_cron.standalone_loop:
class: Okvpn\Bundle\CronBundle\Runner\StandaloneLoop
arguments:
- '@?clock'
- '%okvpn.config.cron_timezone%'

okvpn_cron.react_loop:
class: Okvpn\Bundle\CronBundle\React\ReactLoopAdapter
arguments: [~, '%okvpn.config.cron_timezone%']

Okvpn\Bundle\CronBundle\Runner\ScheduleLoopInterface:
alias: okvpn_cron.schedule_loop
alias: okvpn_cron.standalone_loop

Okvpn\Bundle\CronBundle\React\ReactLoopAdapter:
arguments:
Expand All @@ -116,7 +117,7 @@ services:
- '@okvpn_cron.runner_default'
- '@okvpn_cron.loader_default'
- '@?event_dispatcher'
- '@okvpn_cron.schedule_loop'
- '@Okvpn\Bundle\CronBundle\Runner\ScheduleLoopInterface'
- '@?clock'
- '%okvpn.config.cron_timezone%'
tags:
Expand Down
4 changes: 2 additions & 2 deletions src/Runner/TimerStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Okvpn\Bundle\CronBundle\Model\ScheduleEnvelope;

/**
* Storage all running timers for demand crond.
* Storage of all running timers for a crond demand.
*
* Simply access to schedule tasks
* Gives easy access to active scheduled tasks
*/
class TimerStorage
{
Expand Down

0 comments on commit 72b84b7

Please sign in to comment.