bin/gearman_decode_worker.pl
author Vincent Tondellier <tonton+hg@team1664.org>
Sun, 03 Aug 2014 17:35:01 +0200
changeset 28 0df70b8735e3
parent 20 169c73eb8881
child 34 e0d6597078a5
permissions -rwxr-xr-x
Update Twitter bootstrap to 3.2.0

#!/usr/bin/env perl

# 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/>.

use Gearman::Worker;
use Mojo::JSON;
use Mojo::Util qw(decode slurp);
use File::Temp;
use lib 'lib';

sub load_config {
    my ($file) = @_;
    my $code = decode('UTF-8', slurp $file);
    return eval($code);
}

my $config = load_config($ARGV[0]);

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;

    my $json = Mojo::JSON->new();
    my $jsonin = $json->decode($args);

    my $file = File::Temp->new();
    binmode($file);
    print $file $jsonin->{file};

    my $cmd = $config->{Dumper}->{JSONStackwalker} . " " . $file->filename . " " . $config->{Dumper}->{SymbolsPath};
    my $out = qx($cmd 2>/dev/null) or die $!;

    my $pjson = $json->decode($out);

    # Create json for the params
    $pjson->{client_info} = $jsonin->{params};

    my $uuidstr = $jsonin->{uuid};
    $storage->store_dump($uuidstr, slurp $file);
    $storage->store_processed_data($uuidstr, $pjson);

    return $json->encode($pjson);
});

$worker->work() while 1;