Reorganize config
authorVincent Tondellier <tonton+hg@team1664.org>
Sun, 07 Apr 2013 00:02:00 +0200
changeset 19 300b902b5461
parent 18 60db1090095b
child 20 169c73eb8881
Reorganize config Delegate storage processing to the queue
CrashTest.conf
CrashTest.pl
bin/gearman_decode_worker.pl
lib/CrashTest/Decode/Queue/Gearman.pm
lib/CrashTest/Decode/Queue/NoQueue.pm
lib/CrashTest/Storage/FileSystem.pm
--- a/CrashTest.conf	Sat Apr 06 22:50:55 2013 +0200
+++ b/CrashTest.conf	Sun Apr 07 00:02:00 2013 +0200
@@ -1,9 +1,19 @@
 {
-  DataDir => 'data/',
-  MinidumpStackwalkJSON => './stackwalker',
-  SymbolsPath => 'breakpad-debug-symbols/*',
-  Storage => "CrashTest::Storage::FileSystem",
-  DecodeQueue => "CrashTest::Decode::Queue::NoQueue",
+  Dumper => {
+      JSONStackwalker => './stackwalker',
+      SymbolsPath => 'breakpad-debug-symbols/*',
+  },
+  Storage => {
+      Type => "CrashTest::Storage::FileSystem",
+      DataDir => 'data/'
+  },
+  DecodeQueue => {
+      Type => "CrashTest::Decode::Queue::NoQueue"
+  },
+#  DecodeQueue => {
+#      Type => "CrashTest::Decode::Queue::Gearman",
+#      GearmanServers => [ 'localhost:4730' ]
+#  },
   ScmLinks => {
     "svn:svn.example.org/testproject" => 'https://redmine.example.org/projects/testproject/repository/entry/<%= $scmpath =%>?rev=<%= $rev =%>#L<%= $line =%>',
   },
--- a/CrashTest.pl	Sat Apr 06 22:50:55 2013 +0200
+++ b/CrashTest.pl	Sun Apr 07 00:02:00 2013 +0200
@@ -25,14 +25,14 @@
 
 app->attr(storage => sub {
     my $self = shift;
-    eval "require $config->{Storage}";
-    return $config->{Storage}->new($config->{DataDir});
+    eval "require $config->{Storage}->{Type}";
+    return $config->{Storage}->{Type}->new($config->{Storage});
 });
 
 app->attr(decode_queue => sub {
     my $self = shift;
-    eval "require $config->{DecodeQueue}";
-    return $config->{DecodeQueue}->new($config);
+    eval "require $config->{DecodeQueue}->{Type}";
+    return $config->{DecodeQueue}->{Type}->new($config->{DecodeQueue}, $config->{Dumper}, app->storage);
 });
 
 helper scm_file_link => sub {
@@ -76,7 +76,6 @@
     return b($self->t(span => (title => $signature, class => "shortened-signature") => $short_signature));
 };
 
-# Upload form in DATA section
 get '/' => sub {
     my $self = shift;
     $self->stash(files => $self->app->storage->index());
@@ -96,14 +95,10 @@
     my $file = $self->req->upload('upload_file_minidump');
     my %paramshash = map { $_ => $self->req->param($_) } $self->req->param;
 
-    my $pjson = $self->app->decode_queue->decode($file, \%paramshash);
-
     my ($uuid, $uuidstr);
     UUID::generate($uuid);
     UUID::unparse($uuid, $uuidstr);
-
-    $self->app->storage->store_dump($uuidstr, $file);
-    $self->app->storage->store_processed_data($uuidstr, $pjson);
+    my $pjson = $self->app->decode_queue->decode($file, \%paramshash, $uuidstr);
 
     # reply
     $self->render_text($pjson->{status});
--- a/bin/gearman_decode_worker.pl	Sat Apr 06 22:50:55 2013 +0200
+++ b/bin/gearman_decode_worker.pl	Sun Apr 07 00:02:00 2013 +0200
@@ -17,6 +17,7 @@
 use Mojo::JSON;
 use Mojo::Util qw(decode slurp);
 use File::Temp;
+use lib 'lib';
 
 sub load_config {
     my ($file) = @_;
@@ -25,7 +26,12 @@
 }
 
 my $config = load_config($ARGV[0]);
-my $worker = Gearman::Worker->new(job_servers => $config->{GearmanServers});
+
+eval "require $config->{Storage}->{Type}";
+my $storage = $config->{Storage}->{Type}->new($config->{Storage});
+
+my $worker = Gearman::Worker->new(job_servers => $config->{DecodeQueue}->{GearmanServers});
+
 
 $worker->register_function("dump_decode", 60, sub {
     my $args = $_[0]->arg;
@@ -34,9 +40,10 @@
     my $jsonin = $json->decode($args);
 
     my $file = File::Temp->new();
+    binmode($file);
     print $file $jsonin->{file};
 
-    my $cmd = $config->{MinidumpStackwalkJSON} . " " . $file->filename . " " . $config->{SymbolsPath};
+    my $cmd = $config->{Dumper}->{JSONStackwalker} . " " . $file->filename . " " . $config->{Dumper}->{SymbolsPath};
     my $out = qx($cmd 2>/dev/null) or die $!;
 
     my $pjson = $json->decode($out);
@@ -44,6 +51,10 @@
     # Create json for the params
     $pjson->{client_info} = $jsonin->{params};
 
+    my $uuidstr = $jsonin->{uuid};
+    $storage->store_dump($uuidstr, $file);
+    $storage->store_processed_data($uuidstr, $pjson);
+
     return $json->encode($pjson);
 });
 
--- a/lib/CrashTest/Decode/Queue/Gearman.pm	Sat Apr 06 22:50:55 2013 +0200
+++ b/lib/CrashTest/Decode/Queue/Gearman.pm	Sun Apr 07 00:02:00 2013 +0200
@@ -19,24 +19,18 @@
 use warnings;
 
 sub new {
-    my ($class, $config) = @_;
+    my ($class, $config, $dumperconfig, $storage) = @_;
     my $self  = {};
-    $self->{config} = $config;
     $self->{gearman} = Gearman::Client->new(job_servers => $config->{GearmanServers});
     bless ($self, $class);
     return $self;
 }
 
-sub config {
-    my ($self, $key) = @_;
-    return $self->{config}->{$key};
-}
-
 sub decode {
-    my ($self, $file, $paramshash) = @_;
+    my ($self, $file, $paramshash, $uuidstr) = @_;
 
     my $json = Mojo::JSON->new;
-    my $args = $json->encode({ file => $file->slurp, params => $paramshash });
+    my $args = $json->encode({ file => $file->slurp, params => $paramshash, uuid => $uuidstr });
     my $taskres = $self->{gearman}->do_task('dump_decode', $args);
 
     return $json->decode($$taskres);
--- a/lib/CrashTest/Decode/Queue/NoQueue.pm	Sat Apr 06 22:50:55 2013 +0200
+++ b/lib/CrashTest/Decode/Queue/NoQueue.pm	Sun Apr 07 00:02:00 2013 +0200
@@ -19,25 +19,21 @@
 use warnings;
 
 sub new {
-    my ($class, $config) = @_;
+    my ($class, $config, $dumperconfig, $storage) = @_;
     my $self  = {};
-    $self->{config} = $config;
+    $self->{Dumper} = $dumperconfig;
+    $self->{storage} = $storage;
     bless ($self, $class);
     return $self;
 }
 
-sub config {
-    my ($self, $key) = @_;
-    return $self->{config}->{$key};
-}
-
 sub decode {
-    my ($self, $file, $paramshash) = @_;
+    my ($self, $file, $paramshash, $uuidstr) = @_;
     my $fh = File::Temp->new(SUFFIX => '.dmp');
     my $dmp_file = $fh->filename;
     $file->move_to($dmp_file);
 
-    my $cmd = $self->config("MinidumpStackwalkJSON") . " $dmp_file " . $self->config("SymbolsPath");
+    my $cmd = $self->{Dumper}->{JSONStackwalker} . " $dmp_file " . $self->{Dumper}->{SymbolsPath};
     my $out = qx($cmd 2>/dev/null) or die $!;
 
     my $json = Mojo::JSON->new;
@@ -46,6 +42,9 @@
     # Create json for the params
     $pjson->{client_info} = $paramshash;
 
+    $self->{storage}->store_dump($uuidstr, $file->slurp);
+    $self->{storage}->store_processed_data($uuidstr, $pjson);
+
     return $pjson;
 }
 
--- a/lib/CrashTest/Storage/FileSystem.pm	Sat Apr 06 22:50:55 2013 +0200
+++ b/lib/CrashTest/Storage/FileSystem.pm	Sun Apr 07 00:02:00 2013 +0200
@@ -17,9 +17,9 @@
 use warnings;
 
 sub new {
-    my ($class, $data_path) = @_;
+    my ($class, $config) = @_;
     my $self  = {};
-    $self->{data_path} = $data_path;
+    $self->{data_path} = $config->{DataDir};
     bless ($self, $class);
     return $self;
 }
@@ -79,7 +79,10 @@
     my ($self, $uuid, $file) = @_;
 
     my $dmp_file = File::Spec->catfile($self->{data_path}, "$uuid.dmp");
-    $file->move_to($dmp_file);
+    my $fh = IO::File->new($dmp_file, "w") or die($!);
+    $fh->binmode;
+    print $fh $file;
+    undef $fh;
 }
 
 1;