JSON.pm

filter: Statementstmt 46.6% 21 / 45 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 100% 3 / 3 totaltotal 46.1% 30 / 65 SCAR 23.5
CC
5
Cov
40%
submit
12.0
parseArgs
6.0
parseArgString
6.0
CRAP 10.5
↑ Summary JSON/TestService.pm »
1 package HeliosX::Job::JSON;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Helios::Job';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use JSON::Tiny qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $JSON::Tiny::TRUE = 1;
10 1 $JSON::Tiny::FALSE = 0;
11  
12 1 ▶use HeliosX::Job::JSON::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.01_3460';
15  
16 =head1 NAME
17  
18 HeliosX::Job::JSON - Helios::Job subclass using JSON to specify job arguments
19  
20 =head1 SYNOPSIS
21  
22 # in your Helios::Service class
23 package MyService;
24 use parent 'Helios::Service';
25 use HeliosX::Job::JSON;
26
27 sub JobClass { 'HeliosX::Job::JSON' }
28
29 sub run {
30 ... run code here ...
31 }
32
33 1;
34
35 # in your job submission code, use HeliosX::Job::JSON just like Helios::Job
36 my $config = Helios::Config->parseConfig();
37 my $arg_json = qq/{ "args" : { "arg1": "value1", "arg2": "string2" } }/;
38 my $job = HeliosX::Job::JSON->new();
39 $job->setConfig($config);
40 $job->setJobType('MyService');
41 $job->setArgString($arg_json);
42 my $jobid = $job->submit();
43
44 # or use the included helios_job_submit_json command
45 helios_job_submit_json MyService '{ "args" : { "arg1": "value1", "arg2": "string2" } }'
46  
47  
48 =head1 DESCRIPTION
49  
50 HeliosX::Job::JSON is a Helios::Job subclass allowing you to specify Helios
51 job arguments in JSON format instead of Helios's default XML format. If parts
52 of your application or system use the JSON data format, or your Helios job
53 arguments are difficult to express in XML, you can change your Helios service
54 to use HeliosX::Job::JSON to specify your job arguments in JSON.
55  
56 =head1 JSON JOB ARGUMENT FORMAT
57  
58 To specify a Helios job's arguments in JSON, use the following JSON object
59 as an example:
60  
61 {
62 "jobtype" : "Helios::TestService",
63 "args": {
64 "arg1" : "value1",
65 "arg2" : "value2",
66 "original_file" : "photo.jpg",
67 "size" : "125x125"
68 }
69 }
70  
71 Your JSON object will define a "jobtype" string and an "args" object. The
72 name and value pairs of the args object will become the job's argument hash.
73  
74 The jobtype value is optional if you specify a jobtype another way i.e. using
75 the --jobtype option with helios_job_submit_json or using HeliosX::Job::JSON's
76 setJobType() method.
77  
78 =head1 NOTE ABOUT METAJOBS
79  
80 HeliosX::Job::JSON does not yet support Helios metajobs. Specifying metajob
81 arguments in JSON may be supported in a future release.
82  
83 =head1 METHODS
84  
85 =head2 parseArgs()
86  
87 HeliosX::Job::JSON's parseArgs() method is much simpler than Helios::Job's
88 because JSON's object format is very close to Perl's concept of a hash.
89
90 =cut
91  
92 sub parseArgs {
93 0 ▶ my $self = shift;
Subroutine parseArgs: not called CC=2 SCAR=17.9
Pod documented
94 0 my $arg_string = $self->job()->arg()->[0];
95  
96 0 my $args_hash = $self->parseArgString($arg_string);
97  
98 0 ▶ unless ( defined($args_hash->{args}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args_hash->{"args"}) 0 0 0
99 0 HeliosX::Job::JSON::Error->throw("HeliosX::Job::JSON->parseArgs(): args object is missing!");
100 }
101  
102 0 my $args = $args_hash->{args};
103
104 0 $self->setArgs( $args );
105 0 return $args;
106 }
107  
108  
109 =head2 parseArgString($json_string)
110  
111 The parseArgString() method does the actual parsing of the JSON object string
112 into the Perl hash using JSON::Tiny.
113  
114 =cut
115  
116 sub parseArgString {
117 0 ▶ my $self = shift;
Subroutine parseArgString: not called CC=2 SCAR=17.9
Pod documented
118 0 my $arg_string = shift;
119
120 0 my $arg_hash;
121 eval {
122 0 $arg_hash = decode_json($arg_string);
123 0 1;
124 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $arg_hash = decode_json($arg_string); 1 } } or do { my $E = $@; "HeliosX::Job::JSON::Error"->throw("HeliosX::Job::JSON->parseArgString(): $E") } 0 0 0
125 0 my $E = $@;
126 0 HeliosX::Job::JSON::Error->throw("HeliosX::Job::JSON->parseArgString(): $E");
127 };
128 0 return $arg_hash;
129 }
130  
131  
132 =head2 submit()
133  
134 HeliosX::Job::JSON's submit() method is actually a shell around Helios::Job's
135 submit() to allow specifying the jobtype via the JSON object instead of
136 requiring a separate call to setJobType(). If the jobtype wasn't explicitly
137 specified and submit() cannot determine the jobtype from the JSON object,
138 it will throw a HeliosX::Job::JSON::Error exception.
139  
140 =cut
141  
142 sub submit {
143 0 ▶ my $self = shift;
Subroutine submit: not called CC=3 SCAR=24.8
Pod documented
144
145 # if setJobType() wasn't used to specify the jobtype
146 # try to get it from the JSON object
147 # ugh: we're exposing some of Helios::Job's guts here :(
148 0 ▶ unless ( $self->job()->{__funcname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->job->{"__funcname"}) 0 0 0
149 0 my $args = $self->parseArgString( $self->getArgString() );
150 0 ▶ if ( defined($args->{jobtype}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'jobtype'}) { } 0 0 0
151 0 $self->setJobType( $args->{jobtype} );
152 } else {
153 # uhoh, if the JSON object didn't have the jobtype,
154 # and the user didn't use setJobType(),
155 # we can't submit!!
156 0 HeliosX::Job::JSON::Error->throw("HeliosX::Job::JSON::Error->throw(): No jobtype specified!");
157 }
158 }
159
160 0 return $self->SUPER::submit();
161 }
162  
163  
164 1 1;
165 __END__
↑ Summary JSON/TestService.pm »