| author | Vincent Tondellier <tonton+hg@team1664.org> |
| Fri, 11 Sep 2015 23:56:35 +0200 | |
| changeset 71 | 61fa9d626914 |
| parent 70 | dfc2f094a04f |
| permissions | -rwxr-xr-x |
|
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 |
|
|
55
8d9daa16ccad
Mojo::JSON OO api is deprecated
Vincent Tondellier <tonton+hg@team1664.org>
parents:
43
diff
changeset
|
19 |
use Mojo::JSON qw/decode_json/; |
|
8d9daa16ccad
Mojo::JSON OO api is deprecated
Vincent Tondellier <tonton+hg@team1664.org>
parents:
43
diff
changeset
|
20 |
use Mojo::Util qw/decode slurp/; |
|
70
dfc2f094a04f
Fix compatibility with Mojolicious > 6
Vincent Tondellier <tonton+hg@team1664.org>
parents:
55
diff
changeset
|
21 |
use Mojo::Loader qw/load_class/; |
|
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 $storage_class = $config->{Storage}->{Type};
|
|
70
dfc2f094a04f
Fix compatibility with Mojolicious > 6
Vincent Tondellier <tonton+hg@team1664.org>
parents:
55
diff
changeset
|
37 |
if (my $e = load_class($storage_class)) {
|
|
34
e0d6597078a5
Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents:
20
diff
changeset
|
38 |
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
|
39 |
} |
|
e0d6597078a5
Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents:
20
diff
changeset
|
40 |
|
|
e0d6597078a5
Cleanup: Mojo-ize old code, simplifying perl OO code
Vincent Tondellier <tonton+hg@team1664.org>
parents:
20
diff
changeset
|
41 |
my $storage = $storage_class->new(config => $config->{Storage});
|
|
19
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
42 |
|
|
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
43 |
my $worker = Gearman::Worker->new(job_servers => $config->{DecodeQueue}->{GearmanServers});
|
|
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
44 |
|
|
18
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
45 |
$worker->register_function("dump_decode", 60, sub {
|
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
46 |
my $args = $_[0]->arg; |
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
47 |
|
|
55
8d9daa16ccad
Mojo::JSON OO api is deprecated
Vincent Tondellier <tonton+hg@team1664.org>
parents:
43
diff
changeset
|
48 |
my $jsonin = decode_json($args); |
|
18
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 $file = File::Temp->new(); |
|
19
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
51 |
binmode($file); |
|
18
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
52 |
print $file $jsonin->{file};
|
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
53 |
|
|
19
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
54 |
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
|
55 |
my $out = qx($cmd 2>/dev/null) or die $!; |
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
56 |
|
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
57 |
my $pjson = $json->decode($out); |
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
58 |
|
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
59 |
# Create json for the params |
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
60 |
$pjson->{client_info} = $jsonin->{params};
|
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
61 |
|
|
19
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
62 |
my $uuidstr = $jsonin->{uuid};
|
|
20
169c73eb8881
Store file content, not temporary file name
Vincent Tondellier <tonton+hg@team1664.org>
parents:
19
diff
changeset
|
63 |
$storage->store_dump($uuidstr, slurp $file); |
|
19
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
64 |
$storage->store_processed_data($uuidstr, $pjson); |
|
300b902b5461
Reorganize config
Vincent Tondellier <tonton+hg@team1664.org>
parents:
18
diff
changeset
|
65 |
|
|
18
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
66 |
return $json->encode($pjson); |
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
67 |
}); |
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
68 |
|
|
60db1090095b
Add Gearman decode worker
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff
changeset
|
69 |
$worker->work() while 1; |