From 5855b3bddcd8aaddd915ef008295d873d35bf99a Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Tue, 14 Jan 2014 23:07:31 +0200 Subject: [PATCH] Update Gruntmaster::Handlers to use Redis --- lib/Gruntmaster/Handlers.pm | 66 ++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/lib/Gruntmaster/Handlers.pm b/lib/Gruntmaster/Handlers.pm index 2ce155b..c77e923 100644 --- a/lib/Gruntmaster/Handlers.pm +++ b/lib/Gruntmaster/Handlers.pm @@ -5,10 +5,6 @@ use strict; use warnings; our $VERSION = '0.001'; -use constant FORMAT_EXTENSION => { - CPP => 'cpp', -}; - use Apache2::Access; use Apache2::AuthzCaps qw/hascaps/; use Apache2::RequestRec; @@ -22,45 +18,49 @@ use Cwd qw/cwd/; use File::Basename qw/fileparse/; use File::Temp qw/tempdir/; use File::Copy qw/move/; -use YAML::Any qw/LoadFile DumpFile/; +use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta/; +use Gruntmaster::Page::Log; +use Redis; + +my $redis = Redis->new; sub submit{ my $r = shift; chdir $r->dir_config('root'); my $req = Apache2::Request->new($r); - my ($problem, $format, $contest) = map {scalar $req->param($_)} 'problem', 'prog_format', 'contest'; - my $ext = FORMAT_EXTENSION->{$format}; - my $prog = $req->upload('prog'); - die if defined $contest && $contest !~ /^\w+$/; - - if (defined $contest) { - my $meta = LoadFile "ct/$contest/meta.yml"; - die unless time >= $meta->{start} && time <= $meta->{end} - } - - my $dir = tempdir; - $prog->link("$dir/prog.$ext"); - DumpFile "$dir/meta.yml", { - files => { - prog => { - format => $format, - name => 'prog.cpp', + my ($problem, $format, $contest, $private) = map {scalar $req->param($_)} 'problem', 'prog_format', 'contest', 'private'; + my $prog; + $req->upload('prog')->slurp($prog); + die if defined $contest && $contest !~ /^\w+$/ ; + die if defined $contest && (time < contest_start $contest || time > contest_end $contest); + + my $job = push_job ( + date => time, + problem => $problem, + user => $r->user, + defined $private ? (private => $private) : (), + defined $contest ? (contest => $contest, private => 1) : (), + filesize => length $prog, + extension => Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format}, + ); + + set_job_inmeta $job, { + files => { + prog => { + format => $format, + name => 'prog.' . Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format}, + content => $prog, + } } - }, - problem => $problem, - user => $r->user, - date => time, - defined $contest ? (contest => $contest) : (), }; - my $jobname = int rand 1_000_000_000; - $jobname = int rand 1_000_000_000 while -d "jobs/$jobname"; - move $dir, "jobs/$jobname" or die $!; - + $redis->publish('jobs', $job); $r->print("Job submitted"); OK } +=begin comment + sub private{ my $r = shift; my $dir = (fileparse $r->uri)[1]; @@ -82,6 +82,10 @@ sub private{ DECLINED } +=end comment + +=cut + 1; __END__ # Below is stub documentation for your module. You'd better edit it! -- 2.39.2