Allow synonyms for localhost
[spider.git] / perl / DXSubprocess.pm
1 #
2 # A light shim over Mojo::IOLoop::Subprocess (or Mojo::IOLoop::ForkCall, if we need to go back to that)
3 #
4 # But we stop using Storable!
5 #
6
7 package DXSubprocess;
8
9 use DXUtil;
10 use DXDebug;
11 use DXLog;
12 use Mojo::IOLoop;
13 use     Mojo::IOLoop::Subprocess;
14 use JSON;
15
16 our @ISA = qw(Mojo::IOLoop::Subprocess);
17
18 sub new
19 {
20         my $pkg = shift;
21         my $class = ref $pkg || __PACKAGE__;
22         my $ref = Mojo::IOLoop::Subprocess->new->serialize(\&freeze)->deserialize(\&thaw);
23         return bless $ref, $class;
24 }
25
26 sub freeze
27 {
28         my $r;
29         my $j = shift;
30         unless ($j) {
31                 LogDbg('DXUser', "DXSubcommand::freeze: undefined or empty input");
32                 return q{[null, '']};
33         }
34         
35         eval { $r = encode_json($j) };
36         if ($@) {
37                 my $dd = dd($j);
38                 LogDbg('DXUser', "DXSubcommand::freeze: json error on '$dd': $@");
39                 $r = qq{['$@','']};
40         }
41         return $r;
42 }
43
44 sub thaw
45 {
46         my $r;
47         my $j = shift;
48         unless ($j) {
49                 LogDbg('DXUser', "DXSubcommand::thaw: empty string on input");
50                 return [undef,[0]];
51         }
52
53         return [undef, [1]] unless $j; 
54         eval { $r = decode_json($j) };
55         if ($@) {
56                 LogDbg('DXUser', "DXSubcommand::thaw: json error on '$j': $@");
57                 $r = [$@,[0]];
58         }
59         return $r;
60 }
61 1;
62
63