Add ruby
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon;
2
3use 5.014000;
4use strict;
5use warnings;
6
3e7fd903 7our $VERSION = '5999.000_004';
5c5cd38a
MG
8
9use Gruntmaster::Daemon::Constants qw/ERR/;
fdb99417 10use Gruntmaster::Daemon::Format qw/prepare_files stopvms/;
5c5cd38a 11
ab436d78 12use File::Temp qw/tempdir/;
be3e5b32 13use JSON qw/decode_json encode_json/;
ab436d78 14use Sys::Hostname qw/hostname/;
5c5cd38a
MG
15use Time::HiRes qw/time/;
16use Try::Tiny;
5c5cd38a
MG
17use Log::Log4perl qw/get_logger/;
18
6bf57d23
MG
19use constant FORMAT_EXTENSION => {
20 C => 'c',
21 CPP => 'cpp',
5a4392d5
MG
22 GCCGO => 'go',
23 GOLANG => 'go',
24 HASKELL => 'hs',
6bf57d23
MG
25 MONO => 'cs',
26 JAVA => 'java',
27 PASCAL => 'pas',
28 PERL => 'pl',
29 PYTHON => 'py',
c4d90e6f 30 RUBY => 'rb',
c88da74d 31 SBCL => 'l',
6bf57d23 32};
adb44605 33
5c5cd38a
MG
34##################################################
35
7be5f054 36my $db;
064332cd 37
b4ac22ff 38sub safe_can {
a722431b 39 my ($type, $sub, $name) = @_;
5c5cd38a 40
fcfcfb93 41 get_logger->logdie("Invalid \l$type: '$name'") unless $name =~ /^\w+$/s;
a722431b
MG
42 my $pkg = __PACKAGE__ . "::${type}::${name}";
43 eval "require $pkg" or get_logger->warn("Error while requiring $pkg: $@");
b4ac22ff 44 $pkg->can($sub) or get_logger->logdie("No such \l$type: '$name'");
5c5cd38a
MG
45}
46
ab436d78 47sub process{
867a9dad 48 my ($meta) = @_;
a722431b
MG
49
50 my @results;
51 my @full_results = ();
a722431b
MG
52 our $errors = '';
53 try {
a722431b
MG
54 prepare_files $meta;
55 chomp $errors;
56
99d37110 57 my ($files, $generator, $runner, $judge, $testcnt) = map { $meta->{$_} or die "Required parameter missing: $_\n"} qw/files generator runner judge testcnt/;
a722431b
MG
58
59 $generator = safe_can Generator => generate => $generator;
60 $runner = safe_can Runner => run => $runner;
61 $judge = safe_can Judge => judge => $judge;
62
63 for my $test (1 .. $testcnt) {
64 my $start_time = time;
65 my $result;
66 try {
67 $generator->($test, $meta);
68 $result = $runner->($test, $meta);
69 } catch {
70 $result = $_;
71 unless (ref $result) {
72 chomp $result;
73 $result = [ERR, $result];
74 }
75 };
76
77 if (ref $result) {
78 get_logger->trace("Test $test result is " . $result->[1]);
79 push @full_results, {id => $test, result => $result->[0], result_text => $result->[1], time => time - $start_time}
80 } else {
81 get_logger->trace("Test $test result is $result");
82 push @full_results, {id => $test, result => 0, result_text => $result, time => time - $start_time}
83 }
84 push @results, $result;
85 last if $meta->{judge} eq 'Absolute' && ref $result
5c5cd38a 86 }
5c5cd38a 87
a722431b
MG
88 my %results = $judge->(@results);
89 $meta->{$_} = $results{$_} for keys %results;
7be5f054 90 $meta->{results} = \@full_results
a722431b 91 } catch {
99d37110 92 s/(.*) at .*/$1/s;
a722431b
MG
93 chomp;
94 $meta->{result} = -1;
95 $meta->{result_text} = $_;
96 };
fdb99417 97 stopvms;
867a9dad 98 $meta->{errors} = $errors;
a722431b 99
99d37110 100 get_logger->info('Job result: ' . $meta->{result_text});
867a9dad
MG
101}
102
103sub process_job {
104 my ($job) = @_;
105 my $meta = {
106 problem => $job->problem->id,
107 files => {
108 prog => {
109 name => 'prog.' . $job->extension,
110 format => $job->format,
111 content => $job->source,
112 },
113 },
da822e3c 114 map { $_ => $job->problem->get_column($_) } qw/generator runner judge precnt testcnt timeout olimit/
867a9dad
MG
115 };
116 $meta->{tests} = decode_json $job->problem->tests if $meta->{runner} eq 'File';
da822e3c
MG
117 delete $meta->{precnt} if $job->problem->is_in_archive;
118 $meta->{testcnt} = $meta->{precnt} if $meta->{precnt};
867a9dad
MG
119
120 $meta->{files}{ver} = {
121 name => 'ver.' . FORMAT_EXTENSION->{$job->problem->verformat},
122 format => $job->problem->verformat,
123 content => $job->problem->versource,
124 } if $job->problem->verformat;
125
126 process $meta;
127
da822e3c 128 $meta->{result_text} .= ' (pretests)' if $meta->{precnt};
867a9dad 129 $job->update({
be3e5b32
MG
130 result => $meta->{result},
131 result_text => $meta->{result_text},
6555bf80 132 ($meta->{results} ? (results => encode_json $meta->{results}) : ()),
867a9dad 133 $meta->{errors} ? (errors => $meta->{errors}) : ()
be3e5b32
MG
134 });
135
b335c4bb 136 my $solved = $meta->{result} ? 0 : 1;
867a9dad 137 my $status = $db->problem_statuses->find($job->owner, $job->problem);
b335c4bb 138 $db->problem_statuses->update_or_create({
867a9dad
MG
139 problem => $job->problem,
140 owner => $job->owner,
141 job => $job->id,
142 solved => $solved}) if !$job->problem->is_private;
ab436d78
MG
143}
144
145sub got_job{
be3e5b32
MG
146 my $job = $_[0];
147 my $id = $job->id;
148 get_logger->debug("Taking job $id...");
149 my $daemon = hostname . ":$$";
19f0465b
MG
150 $job->update({daemon => $daemon}, {where => {daemon => undef}})->discard_changes;
151 if ($job->daemon eq $daemon) {
be3e5b32 152 get_logger->debug("Succesfully taken job $id");
867a9dad 153 process_job $job;
be3e5b32 154 get_logger->debug("Job $id done");
ab436d78 155 } else {
19f0465b 156 get_logger->debug("Job $id already taken by " . $job->daemon);
ab436d78 157 }
5c5cd38a
MG
158}
159
160sub run{
55e61ae8 161 require Gruntmaster::Data;
9a69bbad 162 $db = Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
a722431b
MG
163 Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf');
164 get_logger->info("gruntmasterd $VERSION started");
01d59515
MG
165 my $dir = tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1;
166 chmod 0711, $dir;
167 chdir $dir;
be3e5b32
MG
168 while (1) {
169 my $job = $db->jobs->search({daemon => undef}, {rows => 1})->first;
170 got_job $job if defined $job;
171 sleep 2 unless defined $job;
172 }
5c5cd38a
MG
173}
174
1751;
176__END__
5c5cd38a
MG
177
178=head1 NAME
179
bc372959 180Gruntmaster::Daemon - Gruntmaster 6000 Online Judge -- daemon
5c5cd38a
MG
181
182=head1 SYNOPSIS
183
184 use Gruntmaster::Daemon;
bc372959 185 Gruntmaster::Daemon->run;
5c5cd38a
MG
186
187=head1 DESCRIPTION
188
bc372959 189Gruntmaster::Daemon is the daemon component of the Gruntmaster 6000 online judge.
5c5cd38a
MG
190
191=head1 AUTHOR
192
bc372959 193Marius Gavrilescu E<lt>marius@ieval.roE<gt>
5c5cd38a
MG
194
195=head1 COPYRIGHT AND LICENSE
196
bc372959 197Copyright (C) 2014 by Marius Gavrilescu
5c5cd38a 198
bc372959
MG
199This library is free software: you can redistribute it and/or modify
200it under the terms of the GNU Affero General Public License as published by
201the Free Software Foundation, either version 3 of the License, or
202(at your option) any later version.
5c5cd38a
MG
203
204
205=cut
This page took 0.049249 seconds and 4 git commands to generate.