-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathScheduler.php
230 lines (212 loc) · 6.48 KB
/
Scheduler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
declare(strict_types=1);
namespace Psl\Async;
use Closure;
use Psl;
use Psl\DateTime;
use Revolt\EventLoop;
use Revolt\EventLoop\Driver;
use Revolt\EventLoop\InvalidCallbackError;
use Revolt\EventLoop\Suspension;
/**
* Psl wrapper around Revolt scheduler.
*
* @see EventLoop
*
* @codeCoverageIgnore
*
* @mago-expect best-practices/dont-catch-error
*/
final readonly class Scheduler
{
private function __construct() {}
/**
* Returns an object used to suspend and resume execution of the current fiber or {main}.
*
* Calls from the same fiber will return the same suspension object.
*
* @see EventLoop::getSuspension()
*/
public static function getSuspension(): Suspension
{
return EventLoop::getSuspension();
}
/**
* Execute a callback when a signal is received.
*
* @param int $signal_number The signal number to monitor.
* @param (Closure(string, int): void) $callback The callback to execute.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @see EventLoop::onSignal()
*/
public static function onSignal(int $signal_number, Closure $callback): string
{
/**
* @psalm-suppress MissingThrowsDocblock
*
* @var non-empty-string
*/
return EventLoop::onSignal($signal_number, $callback);
}
/**
* Execute a callback when a stream resource becomes readable or is closed for reading.
*
* @param resource $stream The stream to monitor.
* @param Closure(string, resource): void $callback The callback to execute.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public static function onReadable(mixed $stream, Closure $callback): string
{
/** @var non-empty-string */
return EventLoop::onReadable($stream, $callback);
}
/**
* Execute a callback when a stream resource becomes writable or is closed for writing.
*
* @param resource $stream The stream to monitor.
* @param Closure(string, resource): void $callback The callback to execute.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public static function onWritable(mixed $stream, Closure $callback): string
{
/** @var non-empty-string */
return EventLoop::onWritable($stream, $callback);
}
/**
* Queue a microtask.
*
* @param Closure(): void $callback The callback to queue for execution.
*
* @see EventLoop::queue()
*/
public static function queue(Closure $callback): void
{
EventLoop::queue($callback);
}
/**
* Defer the execution of a callback.
*
* @param Closure(string): void $callback The callback to defer.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @see EventLoop::defer()
*/
public static function defer(Closure $callback): string
{
/** @var non-empty-string */
return EventLoop::defer($callback);
}
/**
* Delay the execution of a callback.
*
* @param DateTime\Duration $delay The amount of time, to delay the execution for in seconds.
* @param Closure(string): void $callback The callback to delay.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @see EventLoop::delay()
*/
public static function delay(DateTime\Duration $delay, Closure $callback): string
{
/** @var non-empty-string */
return EventLoop::delay($delay->getTotalSeconds(), $callback);
}
/**
* Repeatedly execute a callback.
*
* @param DateTime\Duration $interval The time interval, to wait between executions in seconds.
* @param Closure(string): void $callback The callback to repeat.
*
* @return non-empty-string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @see EventLoop::repeat()
*/
public static function repeat(DateTime\Duration $interval, Closure $callback): string
{
/** @var non-empty-string */
return EventLoop::repeat($interval->getTotalSeconds(), $callback);
}
/**
* Enable a callback to be active starting in the next tick.
*
* @param non-empty-string $identifier The callback identifier.
*
* @throws Exception\InvalidArgumentException If the callback identifier is invalid.
*
* @see EventLoop::repeat()
*/
public static function enable(string $identifier): void
{
try {
EventLoop::enable($identifier);
} catch (InvalidCallbackError $error) {
throw new Exception\InvalidArgumentException($error->getMessage(), previous: $error);
}
}
/**
* Disable a callback immediately.
*
* @param string $identifier The callback identifier.
*
* @see EventLoop::disable()
*/
public static function disable(string $identifier): void
{
EventLoop::disable($identifier);
}
/**
* Cancel a callback.
*
* @param string $identifier The callback identifier.
*
* @see EventLoop::cancel()
*/
public static function cancel(string $identifier): void
{
EventLoop::cancel($identifier);
}
/**
* Reference a callback.
*
* @param non-empty-string $identifier The callback identifier.
*
* @throws Exception\InvalidArgumentException If the callback identifier is invalid.
*
* @see EventLoop::reference()
*/
public static function reference(string $identifier): void
{
try {
EventLoop::reference($identifier);
} catch (InvalidCallbackError $error) {
throw new Exception\InvalidArgumentException($error->getMessage(), previous: $error);
}
}
/**
* Unreference a callback.
*
* @param string $identifier The callback identifier.
*
* @see EventLoop::unreference()
*/
public static function unreference(string $identifier): void
{
EventLoop::unreference($identifier);
}
/**
* Run the event loop.
*
* @see Driver::run()
*
* @psalm-suppress MissingThrowsDocblock
*/
public static function run(): void
{
EventLoop::getDriver()->run();
}
}