line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HeliosX::Job::JSON; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22727
|
use 5.008; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
37
|
|
6
|
1
|
|
|
1
|
|
4
|
use base 'Helios::Job'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
825
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use JSON::Tiny qw(decode_json); |
9
|
|
|
|
|
|
|
$JSON::Tiny::TRUE = 1; |
10
|
|
|
|
|
|
|
$JSON::Tiny::FALSE = 0; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use HeliosX::Job::JSON::Error; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $self = shift; |
94
|
|
|
|
|
|
|
my $arg_string = $self->job()->arg()->[0]; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my $args_hash = $self->parseArgString($arg_string); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
unless ( defined($args_hash->{args}) ) { |
99
|
|
|
|
|
|
|
HeliosX::Job::JSON::Error->throw("HeliosX::Job::JSON->parseArgs(): args object is missing!"); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $args = $args_hash->{args}; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$self->setArgs( $args ); |
105
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $self = shift; |
118
|
|
|
|
|
|
|
my $arg_string = shift; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $arg_hash; |
121
|
|
|
|
|
|
|
eval { |
122
|
|
|
|
|
|
|
$arg_hash = decode_json($arg_string); |
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
} or do { |
125
|
|
|
|
|
|
|
my $E = $@; |
126
|
|
|
|
|
|
|
HeliosX::Job::JSON::Error->throw("HeliosX::Job::JSON->parseArgString(): $E"); |
127
|
|
|
|
|
|
|
}; |
128
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $self = shift; |
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
|
|
|
|
|
|
|
unless ( $self->job()->{__funcname} ) { |
149
|
|
|
|
|
|
|
my $args = $self->parseArgString( $self->getArgString() ); |
150
|
|
|
|
|
|
|
if ( defined($args->{jobtype}) ){ |
151
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
HeliosX::Job::JSON::Error->throw("HeliosX::Job::JSON::Error->throw(): No jobtype specified!"); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
return $self->SUPER::submit(); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
__END__ |