-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.raku
58 lines (48 loc) · 1.57 KB
/
service.raku
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
use Cro::HTTP::Server;
use Cro::HTTP::Client;
use Cro::HTTP::Router;
use Python2::Compiler::Service;
$*ERR.out-buffer = False;
$*OUT.out-buffer = False;
sub spawn_server (Int $port!) {
my Cro::Service $service = Cro::HTTP::Server.new:
:host<localhost>, :port($port), :application(
route {
include Python2::Compiler::Service.routes();
});
$service.start;
note "Listening on port $port";
my $notify-proc = run 'systemd-notify', 'READY=1';
note "systemd-notify failed with exit code {$notify-proc.exitcode}"
unless $notify-proc.exitcode == 0;
return $service;
}
sub spawn_watchdog (Int $port!) {
my $watchdog = Supply.interval(10);
$watchdog.tap({
my $resp = await Cro::HTTP::Client.post: 'http://localhost:' ~ $port ~ '/compile',
content-type => 'application/json',
body => {
code => '1',
embedded => 'watchdog',
type => 'expression',
};
my $response = await $resp.body;
if ($response ~~ /Python2/) {
my $notify-proc = run 'systemd-notify', 'WATCHDOG=1';
note "systemd-notify failed with exit code {$notify-proc.exitcode}"
unless $notify-proc.exitcode == 0;
}
});
}
multi sub MAIN (
Int :$port = 27099,
Bool :$watchdog = False,
) {
my $service = spawn_server($port);
if $watchdog {
note 'systemd watchdog enabled';
spawn_watchdog($port);
}
react whenever signal(SIGINT) { $service.stop; exit; }
}