-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass
executable file
·69 lines (58 loc) · 1.59 KB
/
pass
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
#!/usr/bin/env perl
use v5.16;
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Redis;
use YAML qw(Load Dump LoadFile);
use Data::Dump qw(pp);
my %pid_of; my %dispatch;
my $config = LoadFile("config");
$config->{host} //= "localhost";
$config->{port} //= 6379;
$config->{timeout} //= 100;
my $redis = AnyEvent::Redis->new(
host => $config->{host},
port => $config->{port},
encoding => 'utf8',
on_error => sub { warn @_ },
on_cleanup => sub { warn "Connection closed: @_" },
);
open my $problemClassFile, "<", $ARGV[0]; my $class;
do { local $/; $class = <$problemClassFile>; };
%dispatch = (
solve => sub {
my %op = %{+shift};
my ($id,$instance) = @op{qw(id instance)};
chomp $instance;
use File::Temp qw(tempfile);
my ($fh,$filename) = tempfile;
my $res = `clingo 100 $filename`;
my @answers = ($res =~ m/Answer: \d+\n(.+)\n/g);
$redis->lpush("$id:result" => Dump(\@answers), sub {});
},
test => sub {
say "command ran";
my %op = %{+shift};
my ($id, $instance) = @op{qw(id instance)};
chomp $instance;
my @answers = (1..2);
say "output printed to $id:result";
$redis->lpush("$id:result" => Dump(\@answers));
},
);
sub handle {
my $result = shift;
my $op = Load($result->[1]);
say "received job: $op->{id}";
$redis->rpush('jobkeys', $op->{id}, sub {});
$dispatch{$op->{command}}->($op);
}
while (1) {
my $cv = $redis->brpop('command',0);
my $res; eval {
$res = $cv->recv;
handle($res);
};
warn $@ if $@;
}