Skip to content

Commit c55989a

Browse files
committed
Switch to use the global event-loop
1 parent 1810f22 commit c55989a

File tree

6 files changed

+82
-55
lines changed

6 files changed

+82
-55
lines changed

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": "^7.4",
77
"dragonmantank/cron-expression": "^3.1",
8-
"react/event-loop": "^1.1",
8+
"react/event-loop": "dev-global-event-loop-accessor-part-four as 1.2.0",
99
"react/promise": "^2.7",
1010
"thecodingmachine/safe": "^1.3",
1111
"wyrihaximus/constants": "^1.6",
@@ -31,6 +31,12 @@
3131
"WyriHaximus\\Tests\\React\\Cron\\": "tests/"
3232
}
3333
},
34+
"repositories": [
35+
{
36+
"type": "vcs",
37+
"url": "https://github.com/wyrihaximus-labs/event-loop"
38+
}
39+
],
3440
"scripts": {
3541
"post-install-cmd": [
3642
"composer normalize"

composer.lock

Lines changed: 47 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
<?php
22

33
use React\EventLoop\Factory;
4+
use React\EventLoop\Loop;
45
use React\Promise\PromiseInterface;
56
use function React\Promise\resolve;
6-
use WyriHaximus\React\Action;
77
use WyriHaximus\React\Cron;
88

99
require 'vendor/autoload.php';
1010

11-
$loop = Factory::create();
12-
13-
Cron::createHighPrecision(
14-
$loop,
15-
new Action(
11+
Cron::create(
12+
new Cron\Action(
1613
'Hour', // Identifier used for mutex locking locking
14+
1, // TTL for Mutex Locking (usually a few times the maximum time this cron should take)
1715
'@hourly', // The cron expression used to schedule this action
1816
function (): PromiseInterface { // The callable ran when this action is due according to it's schedule
1917
echo 'Another hour has passed!', PHP_EOL;
2018

2119
return resolve(true); // This callable MUST return a promise, which is used for releasing the mutex lock
2220
}
2321
),
24-
new Action(
22+
new Cron\Action(
2523
'Minute',
24+
300,
2625
'* * * * *',
2726
function (): PromiseInterface {
2827
echo 'Another minute has passed!', PHP_EOL;
@@ -32,4 +31,4 @@ function (): PromiseInterface {
3231
)
3332
);
3433

35-
$loop->run();
34+
Loop::get()->run();

src/Cron.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace WyriHaximus\React;
66

7-
use React\EventLoop\LoopInterface;
87
use React\Promise\PromiseInterface;
98
use WyriHaximus\React\Cron\ActionInterface;
109
use WyriHaximus\React\Cron\Scheduler;
@@ -21,9 +20,9 @@ final class Cron
2120

2221
private MutexInterface $mutex;
2322

24-
private function __construct(LoopInterface $loop, MutexInterface $mutex, ActionInterface ...$actions)
23+
private function __construct(MutexInterface $mutex, ActionInterface ...$actions)
2524
{
26-
$this->scheduler = new Scheduler($loop);
25+
$this->scheduler = new Scheduler();
2726
$this->actions = $actions;
2827
$this->mutex = $mutex;
2928

@@ -32,14 +31,14 @@ private function __construct(LoopInterface $loop, MutexInterface $mutex, ActionI
3231
});
3332
}
3433

35-
public static function create(LoopInterface $loop, ActionInterface ...$actions): self
34+
public static function create(ActionInterface ...$actions): self
3635
{
37-
return new self($loop, new Memory(), ...$actions);
36+
return new self(new Memory(), ...$actions);
3837
}
3938

40-
public static function createWithMutex(LoopInterface $loop, MutexInterface $mutex, ActionInterface ...$actions): self
39+
public static function createWithMutex(MutexInterface $mutex, ActionInterface ...$actions): self
4140
{
42-
return new self($loop, $mutex, ...$actions);
41+
return new self($mutex, ...$actions);
4342
}
4443

4544
public function stop(): void

src/Cron/Scheduler.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace WyriHaximus\React\Cron;
66

7-
use React\EventLoop\LoopInterface;
7+
use React\EventLoop\Loop;
88
use React\EventLoop\TimerInterface;
99

1010
use function microtime;
@@ -23,18 +23,14 @@ final class Scheduler
2323
private const ACTIVE = true;
2424
private const INACTIVE = false;
2525

26-
private LoopInterface $loop;
27-
2826
/** @var callable[] */
2927
private array $ticks = [];
3028

3129
private ?TimerInterface $timer = null;
3230
private bool $active = self::ACTIVE;
3331

34-
public function __construct(LoopInterface $loop)
32+
public function __construct()
3533
{
36-
$this->loop = $loop;
37-
3834
$this->align();
3935
}
4036

@@ -79,14 +75,14 @@ private function tick(): void
7975
private function align(): void
8076
{
8177
if ($this->timer instanceof TimerInterface) {
82-
$this->loop->cancelTimer($this->timer);
78+
Loop::get()->cancelTimer($this->timer);
8379
$this->timer = null;
8480
}
8581

8682
$currentSecond = (int) date('s', (int) $this->time());
8783

8884
if ($currentSecond >= ONE && $currentSecond <= self::TIER_SLOW) {
89-
$this->timer = $this->loop->addTimer(self::TIER_SLOW - $currentSecond, function (): void {
85+
$this->timer = Loop::get()->addTimer(self::TIER_SLOW - $currentSecond, function (): void {
9086
$this->timer = null;
9187
$this->align();
9288
});
@@ -95,7 +91,7 @@ private function align(): void
9591
}
9692

9793
if ($currentSecond > self::TIER_SLOW && $currentSecond <= self::TIER_MEDIUM) {
98-
$this->timer = $this->loop->addTimer(ONE, function (): void {
94+
$this->timer = Loop::get()->addTimer(ONE, function (): void {
9995
$this->timer = null;
10096
$this->align();
10197
});
@@ -104,7 +100,7 @@ private function align(): void
104100
}
105101

106102
if ($currentSecond === self::TIER_FAST) {
107-
$this->timer = $this->loop->addTimer(0.001, function (): void {
103+
$this->timer = Loop::get()->addTimer(0.001, function (): void {
108104
$this->timer = null;
109105
$this->align();
110106
});
@@ -114,7 +110,7 @@ private function align(): void
114110

115111
$this->tick();
116112

117-
$this->timer = $this->loop->addPeriodicTimer(self::MINUTE_SECONDS, function (): void {
113+
$this->timer = Loop::get()->addPeriodicTimer(self::MINUTE_SECONDS, function (): void {
118114
$this->tick();
119115
});
120116
}
@@ -127,6 +123,6 @@ public function stop(): void
127123
return;
128124
}
129125

130-
$this->loop->cancelTimer($this->timer);
126+
Loop::get()->cancelTimer($this->timer);
131127
}
132128
}

tests/CronFunctionalTest.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44

55
namespace WyriHaximus\Tests\React\Cron;
66

7-
use React\EventLoop\Factory;
8-
use React\EventLoop\LoopInterface;
7+
use React\EventLoop\Loop;
98
use React\Promise\Deferred;
109
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
1110
use WyriHaximus\React\Cron;
1211
use WyriHaximus\React\Cron\Action;
1312
use WyriHaximus\React\Mutex\Memory;
1413

15-
use function array_unshift;
16-
1714
final class CronFunctionalTest extends AsyncTestCase
1815
{
1916
/**
@@ -23,13 +20,11 @@ public function provideFactoryMethods(): iterable
2320
{
2421
yield 'default' => [
2522
'create',
26-
Factory::create(),
2723
[],
2824
];
2925

3026
yield 'default_mutex' => [
3127
'createWithMutex',
32-
Factory::create(),
3328
[
3429
new Memory(),
3530
],
@@ -42,7 +37,7 @@ public function provideFactoryMethods(): iterable
4237
* @test
4338
* @dataProvider provideFactoryMethods
4439
*/
45-
public function scheduling(string $factoryMethod, LoopInterface $loop, array $args): void
40+
public function scheduling(string $factoryMethod, array $args): void
4641
{
4742
$ran = false;
4843
$ranTimes = 0;
@@ -63,11 +58,10 @@ public function scheduling(string $factoryMethod, LoopInterface $loop, array $ar
6358
$deferred->resolve();
6459
});
6560

66-
array_unshift($args, $loop);
6761
$args[] = $action;
6862
/** @phpstan-ignore-next-line */
6963
$cron = Cron::$factoryMethod(...$args);
70-
$this->await($deferred->promise(), $loop, 150);
64+
$this->await($deferred->promise(), Loop::get(), 150);
7165

7266
self::assertTrue($ran);
7367
self::assertSame(2, $ranTimes);
@@ -79,14 +73,14 @@ public function scheduling(string $factoryMethod, LoopInterface $loop, array $ar
7973
* @test
8074
* @dataProvider provideFactoryMethods
8175
*/
82-
public function mutexLockOnlyAllowsTheSameActionOnce(string $factoryMethod, LoopInterface $loop, array $args): void
76+
public function mutexLockOnlyAllowsTheSameActionOnce(string $factoryMethod, array $args): void
8377
{
8478
$ran = false;
8579
$ranTimes = 0;
8680
$cron = null;
8781
$deferred = new Deferred();
88-
$action = new Action('name', 0.1, '* * * * *', static function () use (&$ran, &$ranTimes, &$cron, $loop, $deferred): void {
89-
$loop->futureTick(static function () use (&$ran, &$ranTimes, &$cron, $deferred): void {
82+
$action = new Action('name', 0.1, '* * * * *', static function () use (&$ran, &$ranTimes, &$cron, $deferred): void {
83+
Loop::get()->futureTick(static function () use (&$ran, &$ranTimes, &$cron, $deferred): void {
9084
$ran = true;
9185
$ranTimes++;
9286

@@ -102,12 +96,11 @@ public function mutexLockOnlyAllowsTheSameActionOnce(string $factoryMethod, Loop
10296
});
10397
});
10498

105-
array_unshift($args, $loop);
10699
$args[] = $action;
107100
$args[] = $action;
108101
/** @phpstan-ignore-next-line */
109102
$cron = Cron::$factoryMethod(...$args);
110-
$this->await($deferred->promise(), $loop, 150);
103+
$this->await($deferred->promise(), Loop::get(), 150);
111104

112105
self::assertTrue($ran);
113106
self::assertSame(2, $ranTimes);

0 commit comments

Comments
 (0)