1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React; |
4
|
|
|
|
5
|
|
|
use React\EventLoop\LoopInterface; |
6
|
|
|
use WyriHaximus\React\Mutex\Memory; |
7
|
|
|
use WyriHaximus\React\Mutex\MutexInterface; |
8
|
|
|
|
9
|
|
|
final class Cron |
10
|
|
|
{ |
11
|
|
|
/** @var ActionInterface[] */ |
12
|
|
|
private $actions; |
13
|
|
|
|
14
|
|
|
/** @var MutexInterface */ |
15
|
|
|
private $mutex; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param SchedulerInterface $scheduler |
19
|
|
|
* @param MutexInterface $mutex |
20
|
|
|
* @param ActionInterface[] $actions |
21
|
|
|
*/ |
22
|
4 |
|
private function __construct(SchedulerInterface $scheduler, MutexInterface $mutex, ActionInterface ...$actions) |
23
|
|
|
{ |
24
|
4 |
|
$this->actions = $actions; |
|
|
|
|
25
|
4 |
|
$this->mutex = $mutex; |
26
|
|
|
|
27
|
|
|
$scheduler->schedule(function (): void { |
28
|
4 |
|
$this->tick(); |
29
|
4 |
|
}); |
30
|
4 |
|
} |
31
|
|
|
|
32
|
2 |
|
public static function create(LoopInterface $loop, ActionInterface ...$actions) |
33
|
|
|
{ |
34
|
2 |
|
return new self(new Scheduler($loop), new Memory(), ...$actions); |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
public static function createHighPrecision(LoopInterface $loop, ActionInterface ...$actions) |
38
|
|
|
{ |
39
|
2 |
|
return new self(new HighPrecisionScheduler($loop), new Memory(), ...$actions); |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
private function tick(): void |
43
|
|
|
{ |
44
|
4 |
|
foreach ($this->actions as $action) { |
45
|
4 |
|
$this->perform($action); |
46
|
|
|
} |
47
|
4 |
|
} |
48
|
|
|
|
49
|
4 |
|
private function perform(ActionInterface $action): void |
50
|
|
|
{ |
51
|
4 |
|
if ($action->isDue() === false) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->mutex->acquire($action->getKey())->then(function ($lock) use ($action) { |
56
|
4 |
|
if ($lock === false) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $action->perform()->then(function () use ($action, $lock) { |
61
|
4 |
|
return $this->mutex->release($lock); |
62
|
4 |
|
}); |
63
|
4 |
|
})->done(); |
64
|
4 |
|
} |
65
|
|
|
} |
66
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..