-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-interrupt.php
75 lines (57 loc) · 1.34 KB
/
custom-interrupt.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
<?php
declare(strict_types=1);
use Zlikavac32\AlarmScheduler\AlarmHandler;
use Zlikavac32\AlarmScheduler\AlarmScheduler;
use Zlikavac32\AlarmScheduler\NaiveAlarmScheduler;
if (!extension_loaded('posix')) {
die('Extension posix missing');
}
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/common.php';
/*
* Example that demonstrates how to throw custom exception that should perform hard interrupt.
*/
pcntl_async_signals(true);
pcntl_signal(
SIGUSR1,
function (): void {
throw new RuntimeException('Our custom exception');
}
);
$scheduler = new NaiveAlarmScheduler();
$scheduler->start();
$scheduler->schedule(
2,
new class implements AlarmHandler
{
public function handle(AlarmScheduler $scheduler): void
{
posix_kill(getmypid(), SIGUSR1);
}
}
);
$scheduler->schedule(
4,
new class implements AlarmHandler
{
public function handle(AlarmScheduler $scheduler): void
{
printNow();
}
}
);
printNow();
while (true) {
try {
sleepWithoutInterrupt(5);
break;
} catch (RuntimeException $e) {
printf("Caught %s (%s) at %s\n", get_class($e), $e->getMessage(), nowAsString());
}
}
/*
Example output:
00m:08s
Caught RuntimeException (Our custom exception) at 00m:10s
00m:12s
*/