Agent.pm

filter: Statementstmt 15.7% 30 / 191 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 37.5% 6 / 16 Podpod 0.0% 0 / 7 totaltotal 10.8% 36 / 333 CC 60 SCAR 55.3
CC
60
Cov
10%
run_entry
600.0
has_conflicts
240.0
get_entry
56.0
CRAP 251.3
↑ Summary Agent/Runner.pm »
1 package Google::Spreadsheet::Agent;
2  
3 23 ▶use Net::SMTP::TLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use IO::CaptureOutput qw/capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 our $VERSION = '0.02';
11  
12 23 extends ('Google::Spreadsheet::Agent::DB');
13  
14 23 has 'bind_key_fields' => (
15 is => 'ro',
16 isa => 'HashRef',
17 required => 1
18 );
19  
20 23 has 'agent_name' => (
21 is => 'ro',
22 isa => 'Str',
23 required => 1
24 );
25  
26 23 has 'page_name' => (
27 is => 'ro',
28 required => 1
29 );
30  
31 23 has 'prerequisites' => (
32 is => 'ro',
33 isa => 'ArrayRef'
34 );
35  
36 23 has 'debug' => (
37 is => 'ro',
38 isa => 'Bool'
39 );
40  
41 23 has 'max_selves' => (
42 is => 'ro',
43 isa => 'Int'
44 );
45  
46 23 has 'conflicts_with' => (
47 is => 'ro',
48 isa => 'HashRef'
49 );
50 23 has 'subsumes' => (
51 is => 'ro',
52 isa => 'ArrayRef'
53 );
54  
55 #### BUILDERS
56 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
57 0 my $self = shift;
58  
59 0 my @required_key_fields = grep { $self->config->{key_fields}->{$_}->{required} } keys %{$self->config->{key_fields}};
60 0 foreach my $required_query_field (@required_key_fields) {
61 croak ("You must provide a bind_key_fields ${required_query_field} key - value pair!\n")
62 0 ▶ unless ($self->bind_key_fields->{$required_query_field});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bind_key_fields->{$required_query_field} 0 0 0
63 }
64 }
65  
66 #### METHODS
67  
68 0 ▶around 'run_my' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
69 0 my ($orig, $self, @args) = @_;
70  
71 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
72 0 return $self->$orig(@args);
73 }
74 else {
75 0 my ($capture_output, $capture_error);
76 0 ▶ my $no_problems = capture {
Subroutine __ANON__: not called CC=2 SCAR=17.9
77 0 my $ret;
78 0 eval {
79 0 $ret = $self->$orig(@args);
80 };
81 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
82 0 print STDERR $@;
83 0 return;
84 }
85 0 return $ret;
86 0 } \$capture_output, \$capture_error;
87  
88 0 ▶ if (!$no_problems && ($capture_output || $capture_error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $no_problems and $capture_output || $capture_error) 0 0 0
Condition0%0/6
not $no_problems and $capture_output || $capture_error
!ll&&!rl&&r
000
$capture_output or $capture_error
l!l&&r!l&&!r
000
MC/DC0%0/3
not $no_problems and $capture_output || $capture_error
not $no_problems $capture_output $capture_error
Truth table0%0/4
not(A) and B or C
A= not $no_problemsB= $capture_outputC= $capture_error
ABCresult
0XX0
1000
11X1
1011
89 0 $self->mail_error("Output:\n${capture_output}\nError:\n${capture_error}\n");
90 }
91 0 return $no_problems;
92 }
93 23 };
94  
95 0 ▶sub run_my {
Subroutine run_my: not called CC=5 SCAR=34.0
96 0 my ($self, $agent_code) = @_;
97 0 ▶ return 1 if ($self->has_conflicts);
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_conflicts 0 0 0
98 0 my $entry = $self->run_entry();
99
100 0 ▶ return unless ($entry);
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 0 0
101 0 ▶ return 1 if ($entry->{'not_runnable'}); # this is one that is not ready, already running, or already run, or the entry is complete
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"not_runnable"} 0 0 0
102  
103 0 my ($success, $update_entry) = $agent_code->($entry->content);
104 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
105 0 $self->complete_entry($update_entry);
106 0 return 1;
107 }
108 else {
109 0 $self->fail_entry($update_entry);
110 0 return;
111 }
112 }
113  
114 0 ▶sub has_conflicts {
Subroutine has_conflicts: not called CC=15 SCAR=54.8
Pod undocumented
115 0 my $self = shift;
116  
117 0 ▶ return unless ($self->max_selves || $self->conflicts_with); # nothing conflicts here
Branch: 1 branch
BranchTrueFalseTotal
unless $self->max_selves or $self->conflicts_with 0 0 0
Condition0%0/3
$self->max_selves or $self->conflicts_with
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->max_selves or $self->conflicts_with
$self->max_selves $self->conflicts_with
Truth table0%0/3
A or B
A= $self->max_selvesB= $self->conflicts_with
ABresult
1X1
011
000
118  
119 0 my $has_conflicts;
120 my %running_conflicters;
121  
122 0 my $self_name = File::Basename::basename($0);
123  
124 0 my $conflict_search_open = open (my $conflicting_in, '-|', 'ps', '-eo', 'pid,command');
125 0 ▶ unless ($conflict_search_open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conflict_search_open) 0 0 0
126 0 print STDERR "Couldnt check conflicts $!\n";
127 0 return 1; # conflict to be safe
128 }
129  
130 0 ▶ CONFIN: while (my $in = <$conflicting_in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $in = readline $conflicting_in)) 0 0 0
131 0 ▶ next if ($in =~ m/emacs|vi|screen|SCREEN/); # skip editing and screen
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /emacs|vi|screen|SCREEN/ 0 0 0
132 0 ▶ next if ($in =~ m/\s*$$/); # skip this agent
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /\s*$$/ 0 0 0
133 0 ▶ next if ($in =~ m/(\[|\])/); # skip daemons
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /(\[|\])/ 0 0 0
134  
135  
136 0 ▶ if ($self->max_selves
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_selves and $in =~ /$self_name/) 0 0 0
Condition0%0/3
$self->max_selves and $in =~ /$self_name/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->max_selves and $in =~ /$self_name/
$self->max_selves $in =~ /$self_name/
Truth table0%0/3
A and B
A= $self->max_selvesB= $in =~ /$self_name/
ABresult
0X0
100
111
137 && $in =~ m/$self_name/) {
138 0 $running_conflicters{$self->agent_name}++;
139 0 ▶ if ($running_conflicters{$self->agent_name} == $self->max_selves) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running_conflicters{$self->agent_name} == $self->max_selves) 0 0 0
140 0 print STDERR "max_selves limit reached\n";
141 0 $has_conflicts = 1;
142 0 last CONFIN;
143 }
144 }
145  
146 0 ▶ if ($self->conflicts_with) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conflicts_with) 0 0 0
147 0 foreach my $conflicter (keys %{$self->conflicts_with}) {
148 0 ▶ if ($in =~ m/$conflicter/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /$conflicter/) 0 0 0
149 0 $running_conflicters{$conflicter}++;
150 0 ▶ if ($running_conflicters{$conflicter} == $self->conflicts_with->{$conflicter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running_conflicters{$conflicter} == $self->conflicts_with->{$conflicter}) 0 0 0
151 0 print STDERR "conflicts with ${conflicter}\n";
152 0 $has_conflicts = 1;
153 0 last CONFIN;
154 }
155 }
156 }
157 }
158 }
159 0 close $conflicting_in;
160  
161 0 return $has_conflicts;
162 }
163  
164 0 ▶sub get_entry {
Subroutine get_entry: not called CC=7 SCAR=40.3
Pod undocumented
165 0 my $self = shift;
166 0 my $entry;
167  
168 0 my $worksheet = $self->google_db->worksheet({
169 title => $self->page_name
170 });
171  
172 # note, the Google Spreadsheet Data API does supply an sq query operator
173 # which could be used here, but, as of 0.04 of Net::Google::Spreadsheets
174 # this did not prove to be reliable during the tests. This may be
175 # a limitation of the Google API rather than Net::Google::Spreadsheets
176 # as it appeared that Net::Google::Spreadsheets was submitting valid,
177 # url encoded queries that the Google system rejected. Instead this software
178 # conducts a full worksheet scan to ensure the correct row is returned
179 0 ▶ if ($worksheet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($worksheet) 0 0 0
180 0 my @rows = $worksheet->rows();
181 0 ROW: foreach my $row (@rows) {
182 0 ARG: foreach my $arg (keys %{$self->config->{key_fields}}) {
183 next ARG if (
184 !($self->config->{key_fields}->{$arg}->{required})
185 0 ▶ && !($self->bind_key_fields->{$arg})
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"key_fields"}{$arg}{"required"} or $self->bind_key_fields->{$arg} 0 0 0
Condition0%0/3
$self->config->{"key_fields"}{$arg}{"required"} or $self->bind_key_fields->{$arg}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->config->{"key_fields"}{$arg}{"required"} or $self->bind_key_fields->{$arg}
$self->config->{"key_fields"}{$arg}{"required"} $self->bind_key_fields->{$arg}
Truth table0%0/3
A or B
A= $self->config->{"key_fields"}{$arg}{"required"}B= $self->bind_key_fields->{$arg}
ABresult
1X1
011
000
186 ); # skip args that are not required and not bound
187 0 ▶ next ROW unless ($row->content->{$arg} eq $self->bind_key_fields->{$arg});
Branch: 1 branch
BranchTrueFalseTotal
unless $row->content->{$arg} eq $self->bind_key_fields->{$arg} 0 0 0
188 }
189 0 $entry = $row;
190 0 last ROW;
191 }
192 }
193  
194 0 return $entry;
195 }
196  
197 # this call initiates a race resistant attempt to make sure that there is only 1
198 # clear 'winner' among N potential agents attempting to run the same goal on the
199 # same spreadsheet agent's cell
200 0 ▶sub run_entry {
Subroutine run_entry: not called CC=24 SCAR=64.0
Pod undocumented
201 0 my $self = shift;
202  
203 0 my $entry = $self->get_entry();
204  
205 0 my $output = '';
206 0 foreach my $bound_arg (keys %{$self->bind_key_fields}) {
207 0 ▶ next if (!($self->config->{key_fields}->{$bound_arg}) && !($self->bind_key_fields->{$bound_arg}));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"key_fields"}{$bound_arg} or $self->bind_key_fields->{$bound_arg} 0 0 0
Condition0%0/3
$self->config->{"key_fields"}{$bound_arg} or $self->bind_key_fields->{$bound_arg}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->config->{"key_fields"}{$bound_arg} or $self->bind_key_fields->{$bound_arg}
$self->config->{"key_fields"}{$bound_arg} $self->bind_key_fields->{$bound_arg}
Truth table0%0/3
A or B
A= $self->config->{"key_fields"}{$bound_arg}B= $self->bind_key_fields->{$bound_arg}
ABresult
1X1
011
000
208 0 $output .= join(' ', $bound_arg, $self->bind_key_fields->{$bound_arg})." ";
209 }
210  
211 0 ▶ unless ($entry) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry) 0 0 0
212 0 ▶ print STDERR $output." is not supported on ".$self->page_name."\n" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
213 0 return;
214 }
215  
216 0 ▶ unless ($entry->content->{ready}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->content->{"ready"}) 0 0 0
217 0 ▶ print STDERR $output." is not ready to run ".$self->agent_name."\n" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
218 0 return {'not_runnable' => 1};
219 }
220  
221 0 ▶ if ($entry->content->{complete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->content->{"complete"}) 0 0 0
222 0 ▶ print STDERR "All goals are completed for ".$output."\n" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
223 0 return {'not_runnable' => 1};
224 }
225  
226 0 ▶ if ($entry->content->{$self->agent_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->content->{$self->agent_name}) 0 0 0
227 0 my ($status, $running_hostname) = split /\:/, $entry->content->{$self->agent_name};
228 0 ▶ if ($status eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "r") 0 0 0
229 0 ▶ print STDERR $output." is already running ".$self->agent_name." on ${running_hostname}\n" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
230 0 return {'not_runnable' => 1};
231 }
232
233 0 ▶ if ($status == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 1) 0 0 0
234 0 ▶ print STDERR $output." has already run ".$self->agent_name."\n" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
235 0 return {'not_runnable' => 1};
236 }
237  
238 0 ▶ if ($status eq 'F') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "F") 0 0 0
239 0 ▶ print STDERR $output." has already Failed ".$self->agent_name." on a previous run and must be investigated on ${running_hostname}\n" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
240 0 return {'not_runnable' => 1};
241 }
242 }
243  
244 0 ▶ if ($self->prerequisites) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prerequisites) 0 0 0
245 0 foreach my $prereq_field (@{$self->prerequisites}) {
246 0 ▶ unless ($entry->content->{$prereq_field} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry->content->{$prereq_field} == 1) 0 0 0
247 0 ▶ print STDERR $output." has not finished ${prereq_field}\n" if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
248 0 return {'not_runnable' => 1};
249 }
250 }
251 }
252  
253 # first attempt to set the hostname of the machine as the value of the agent
254 0 my $hostname = Sys::Hostname::hostname;
255 0 eval {
256 0 $entry->param({ $self->agent_name => 'r:'.$hostname });
257 };
258 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
259 # this is a collision, which is to be treated as if it is not runnable
260 0 print STDERR $output." lost ".$self->agent_name." on ${hostname}\n";
261 0 return {'not_runnable' => 1};
262 }
263  
264 0 sleep 3;
265 0 my $nentry;
266 0 eval {
267 0 $nentry = $self->get_entry();
268 };
269 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
270 # this is a collision, which is to be treated as if it is not runnable
271 0 print STDERR $output." lost ".$self->agent_name." on ${hostname}\n";
272 0 return {'not_runnable' => 1};
273 }
274  
275 0 my $check = $nentry->content->{$self->agent_name};
276 0 my ($status, $running_hostname) = split /\:/, $check;
277 0 ▶ return $nentry if ($hostname eq $running_hostname);
Branch: 1 branch
BranchTrueFalseTotal
if $hostname eq $running_hostname 0 0 0
278 0 print STDERR $output." lost ".$self->agent_name." on ${hostname}\n";
279 0 return {'not_runnable' => 1};
280 }
281  
282 0 ▶sub fail_entry {
Subroutine fail_entry: not called CC=2 SCAR=17.9
Pod undocumented
283 0 my $self = shift;
284 0 my $update_entry = shift;
285  
286 0 my $entry = $self->get_entry();
287 0 my $hostname = Sys::Hostname::hostname;
288 0 ▶ if ($update_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update_entry) { } 0 0 0
289 0 print STDERR "Updating entry\n";
290 }
291 else {
292 0 $update_entry = {};
293 }
294 0 $update_entry->{$self->agent_name} = 'F:'.$hostname;
295 0 $entry->param($update_entry);
296 }
297  
298 0 ▶sub complete_entry {
Subroutine complete_entry: not called CC=4 SCAR=30.0
Pod undocumented
299 0 my $self = shift;
300 0 my $update_entry = shift;
301  
302 0 print STDERR "All Complete\n";
303 0 my $entry = $self->get_entry();
304 0 ▶ if ($update_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update_entry) { } 0 0 0
305 0 print STDERR "Updating entry\n";
306 }
307 else {
308 0 $update_entry = {};
309 }
310  
311 0 ▶ if ($self->subsumes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subsumes) 0 0 0
312 0 foreach my $subsumed_agent (@{$self->subsumes}) {
313 0 $update_entry->{$subsumed_agent} = 1;
314 }
315 }
316  
317 0 $update_entry->{$self->agent_name} = 1;
318 0 $entry->param($update_entry);
319 }
320  
321 0 ▶sub mail_error {
Subroutine mail_error: not called CC=2 SCAR=17.9
Pod undocumented
322 0 my ($self, $error) = @_;
323  
324 0 my $output = '';
325 0 foreach my $bound_arg (keys %{$self->bind_key_fields}) {
326 0 $output .= join(' ', $bound_arg, $self->bind_key_fields->{$bound_arg})." ";
327 }
328  
329 0 my $prefix = join(' ', Sys::Hostname::hostname, $output, $self->agent_name);
330 0 eval {
331 my $mailer = new Net::SMTP::TLS(
332 'smtp.gmail.com',
333 Hello => 'smtp.gmail.com',
334 Port => 587,
335 User => $self->config->{guser},
336 0 Password => $self->config->{gpass});
337 0 $mailer->mail($self->config->{reply_email});
338 0 $mailer->to($self->config->{send_to});
339 0 $mailer->data;
340 0 $mailer->datasend(join("\n", $prefix,$error));
341 0 $mailer->dataend;
342 0 $mailer->quit;
343 };
344 }
345  
346 23 1; # End of Google::Spreadsheet::Agent
347 __END__
↑ Summary Agent/Runner.pm »