lib/CrashTest/Plugin/CrashProcessor/Breakpad.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Sun, 14 Feb 2016 19:58:33 +0100
changeset 92 702e246e5c9f
parent 78 0ebef32c34af
child 122 8692800ec9ba
permissions -rw-r--r--
Fix config key when inserting

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package CrashTest::Plugin::CrashProcessor::Breakpad;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::JSON::MaybeXS;
use Mojo::JSON qw/decode_json/;
use Mojo::Util qw/dumper/;
use Mojolicious::Validator;

has [ qw/app config dumper_config/ ];
has task_name => "breakpad_decode_task";

sub register {
    my ($self, $app, $args) = @_;

    $self->app($app);
    $self->config($args->{config});
    $self->dumper_config($self->config->{Processor}->{CrashProcessor}->{Breakpad});

    $app->minion->add_task($self->task_name =>
        sub {
            my ($job, $uuid, $params, $files) = @_;
            #$job->app->log->debug(dumper $params);

            $job->on(failed => sub { $self->cleanup($files); });
            #$job->on(finished => sub { $self->cleanup($files); });

            $self->decode($uuid, $params, $files);
            $self->cleanup($files);
        }
    );

    $app->log->debug("Registered Breakpad");
    $args->{cb}->($self);
}

sub validate {
    my ($self, $req) = @_;

    my $hash = $req->params->to_hash;
    $hash->{$_} = $req->every_upload($_) for map { $_->name } @{$req->uploads};

    my $validation = Mojolicious::Validator->new->validation->input($hash);

    $validation->required('UserID');
    #$validation->required('Version');
    $validation->required('ProductName');

    $validation->required('upload_file_minidump')->upload->size(1024, 4 * 1024 * 1024);

    return $validation;
}

sub decode {
    my ($self, $uuidstr, $client_info, $files) = @_;

    my $dmp_file = $files->{upload_file_minidump}->[0];

    my $cmd = $self->dumper_config->{JSONStackwalker} . " $dmp_file " . $self->dumper_config->{SymbolsPath};
    my $out = qx($cmd 2>/dev/null) or die $!;

    my $pjson = decode_json($out);

    $self->app->crash_reports->create($uuidstr, $pjson, $client_info, $dmp_file);
}

sub cleanup {
    my ($self, $files) = @_;
    #$self->app->log->debug("cleanup " . dumper $files);
    foreach my $values(values %$files) {
        foreach my $f(@$values) {
            unlink $f or $self->app->log->warn("Failed to unlink $f: $!");
        }
    }
}

1;