|
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
|
8 |
|
private function __construct(SchedulerInterface $scheduler, MutexInterface $mutex, ActionInterface ...$actions) |
|
23
|
|
|
{ |
|
24
|
8 |
|
$this->actions = $actions; |
|
|
|
|
|
|
25
|
8 |
|
$this->mutex = $mutex; |
|
26
|
|
|
|
|
27
|
|
|
$scheduler->schedule(function (): void { |
|
28
|
8 |
|
$this->tick(); |
|
29
|
8 |
|
}); |
|
30
|
8 |
|
} |
|
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
|
2 |
|
public static function createWithMutex(LoopInterface $loop, MutexInterface $mutex, ActionInterface ...$actions) |
|
43
|
|
|
{ |
|
44
|
2 |
|
return new self(new Scheduler($loop), $mutex, ...$actions); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public static function createHighPrecisionWithMutex(LoopInterface $loop, MutexInterface $mutex, ActionInterface ...$actions) |
|
48
|
|
|
{ |
|
49
|
2 |
|
return new self(new HighPrecisionScheduler($loop), $mutex, ...$actions); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
private function tick(): void |
|
53
|
|
|
{ |
|
54
|
8 |
|
foreach ($this->actions as $action) { |
|
55
|
8 |
|
$this->perform($action); |
|
56
|
|
|
} |
|
57
|
8 |
|
} |
|
58
|
|
|
|
|
59
|
8 |
|
private function perform(ActionInterface $action): void |
|
60
|
|
|
{ |
|
61
|
8 |
|
if ($action->isDue() === false) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->mutex->acquire($action->getKey())->then(function ($lock) use ($action) { |
|
66
|
8 |
|
if ($lock === false) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $action->perform()->then(function () use ($action, $lock) { |
|
71
|
8 |
|
return $this->mutex->release($lock); |
|
72
|
8 |
|
}); |
|
73
|
8 |
|
})->done(); |
|
74
|
8 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
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..