bin/gearman_decode_worker.pl
author Vincent Tondellier <tonton+hg@team1664.org>
Thu, 28 Aug 2014 00:54:41 +0200
changeset 49 c088af64f31f
parent 43 6070307efd38
child 55 8d9daa16ccad
permissions -rwxr-xr-x
Drop dependency on aliased (only used in 1 file)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     1
#!/usr/bin/env perl
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     2
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     3
# This program is free software: you can redistribute it and/or modify
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     4
# it under the terms of the GNU General Public License as published by
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     5
# the Free Software Foundation, either version 3 of the License, or
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     6
# (at your option) any later version.
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     7
#
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     8
# This program is distributed in the hope that it will be useful,
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
     9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    11
# GNU General Public License for more details.
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    12
#
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    13
# You should have received a copy of the GNU General Public License
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    15
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    16
use Mojo::Base -strict;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    17
use lib 'lib';
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    18
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    19
use Mojo::JSON;
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    20
use Mojo::Util qw(decode slurp);
34
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    21
use Mojo::Loader;
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    22
use File::Temp;
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    23
use Gearman::Worker;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    24
use Mojolicious;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    25
use Mojo::Home;
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    26
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    27
sub load_config {
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    28
    my $app = Mojolicious->new;
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    29
    $app->home(Mojo::Home->new("$FindBin::Bin/../"));
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    30
    $app->moniker("CrashTest");
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    31
    return $app->plugin('Config');
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    32
}
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    33
43
6070307efd38 Use the mojolicious plugin instead of rolling our own config loader.
Vincent Tondellier <tonton+hg@team1664.org>
parents: 34
diff changeset
    34
my $config = load_config();
19
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    35
34
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    36
my $loader = Mojo::Loader->new;
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    37
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    38
my $storage_class = $config->{Storage}->{Type};
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    39
if (my $e = $loader->load($storage_class)) {
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    40
    die ref $e ? "Exception: $e" : 'Not found!';
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    41
}
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    42
e0d6597078a5 Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents: 20
diff changeset
    43
my $storage = $storage_class->new(config => $config->{Storage});
19
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    44
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    45
my $worker = Gearman::Worker->new(job_servers => $config->{DecodeQueue}->{GearmanServers});
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    46
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    47
$worker->register_function("dump_decode", 60, sub {
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    48
    my $args = $_[0]->arg;
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    49
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    50
    my $json = Mojo::JSON->new();
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    51
    my $jsonin = $json->decode($args);
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    52
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    53
    my $file = File::Temp->new();
19
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    54
    binmode($file);
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    55
    print $file $jsonin->{file};
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    56
19
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    57
    my $cmd = $config->{Dumper}->{JSONStackwalker} . " " . $file->filename . " " . $config->{Dumper}->{SymbolsPath};
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    58
    my $out = qx($cmd 2>/dev/null) or die $!;
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    59
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    60
    my $pjson = $json->decode($out);
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    61
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    62
    # Create json for the params
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    63
    $pjson->{client_info} = $jsonin->{params};
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    64
19
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    65
    my $uuidstr = $jsonin->{uuid};
20
169c73eb8881 Store file content, not temporary file name
Vincent Tondellier <tonton+hg@team1664.org>
parents: 19
diff changeset
    66
    $storage->store_dump($uuidstr, slurp $file);
19
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    67
    $storage->store_processed_data($uuidstr, $pjson);
300b902b5461 Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents: 18
diff changeset
    68
18
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    69
    return $json->encode($pjson);
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    70
});
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    71
60db1090095b Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    72
$worker->work() while 1;