Pluggable/Run.pm

filter: Statementstmt 100% 15 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 20 / 20 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Pluggable/Plugin/JobsFromXML.pm ↑ Summary
1 package Schedule::Pluggable::Run;
2  
3 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use POSIX qw/ tmpnam strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 $Data::Dumper::Sortkeys = 1;
9  
10 sub run_in_series {
11 my $self = shift;
12  
13 my @jobs = ref $_[0] eq 'ARRAY' ? @{ $_[0] }
14 : @_;
15 my $job_no = 1;
16 my $last_job = undef;
17 foreach my $job (@jobs) {
18 if (ref($job) eq 'HASH') {
19 $job->{name} ||= "Job".$job_no++;
20 delete $job->{dependencies} if $job->{dependencies};
21 delete $job->{prerequisites} if $job->{prerequisites};
22 }
23 elsif (ref($job) eq 'ARRAY') {
24 my $command = join(" ", @{ $job });
25 $job = { name => "Job".$job_no++,
26 command => $command };
27
28 }
29 else {
30 my $command = $job;
31 $job = { name => "Job".$job_no++,
32 command => $command
33 };
34 }
35 $job->{prerequisites} = $last_job->{name} if $last_job;
36 $last_job= { %{ $job } };
37 }
38 $self->run_schedule({ Jobs => [ @jobs ]});
39 }
40 sub run_in_parallel {
41 my $self = shift;
42 my @jobs = ref $_[0] eq 'ARRAY' ? @{ $_[0] }
43 : @_;
44 my $job_no = 1;
45  
46 foreach my $job (@jobs) {
47 if (ref($job) eq 'HASH') {
48 $job->{name} ||= "Job".$job_no++;
49 }
50 elsif (ref($job) eq 'ARRAY') {
51 my $command = join(" ", @{ $job });
52 $job = { name => "Job".$job_no++,
53 command => $command };
54
55 }
56 else {
57 my $command = $job;
58 $job = { name => "Job".$job_no++,
59 command => $command
60 };
61 }
62 delete $job->{dependencies} if $job->{dependencies};
63 delete $job->{prerequisites} if $job->{prerequisites};
64 }
65 $self->run_schedule({ Jobs => [ @jobs ]});
66 }
67 sub run_schedule {
68 my ($self, $params) = @_;
69 my $status = $self->_validate_config($params);
70 # warn Data::Dumper->Dump([$status],[qw/$status/]);
71 my @jobs_ready_to_run = sort { $a->{name} cmp $b->{name} } values %{ $status->{Ready_to_Run} };
72 delete $status->{Ready_to_Run};
73 $status->{EndOfScheduleFile} = POSIX::tmpnam();
74 $self->_set_status($status);
75 my ($id, $pid) = $self->enqueue( MooseX::Workers::Job->new(name => 'MonitorJobs', command => sub { _monitor_jobs($self); } ));
76 $status->{Pids}{MonitorJobs} = $pid;
77 $status->{Ids}{$id} = 'MonitorJobs';
78 $self->_set_status($status);
79 $self->_queue_jobs(@jobs_ready_to_run);
80 POE::Kernel->run;
81 $status = $self->_get_status;
82 delete $status->{$_} foreach qw/ Queued Running TotalQueued TotalStarted Ids Pids Groups Config EndOfScheduleFile LastUpdate/;
83 delete $status->{Jobs}{MonitorJobs};
84 foreach my $k (qw/groups pending_prerequisites prerequisites Id Queued group/) {
85 delete $status->{Jobs}{$_}{$k} foreach keys %{ $status->{Jobs} };
86 }
87
88 return $status->{TotalFailed}, $status;
89 }
90 sub _monitor_jobs {
91 my $self = shift;
92 my $status = $self->_get_status();
93 my $tempfile = $status->{EndOfScheduleFile};
94 while (1) {
95 sleep(10);
96 last if -f $tempfile;
97 }
98 unlink( $tempfile );
99 }
100 sub _queue_jobs {
101 my ($self, @jobs) = @_;
102 foreach my $job (@jobs) {
103 if ($job->{params}) {
104 if (ref $job->{params} eq 'ARRAY') {
105 $job->{command} = join(" ", $job->{command}, @{ $job->{params} });
106 }
107 else {
108 $job->{command} = join(" ", $job->{command}, $job->{params});
109 }
110 }
111 my $j = MooseX::Workers::Job->new(
112 name => $job->{name},
113 command => $job->{command}
114 );
115 $self->event_handler(Event => 'JobQueued',
116 JobName => $job->{name},
117 Command => $job->{command},
118 );
119 try {
120 $self->enqueue( $j );
121 }
122 catch {
123 # warn "oops - can't queue $job->{name} - $job->{command}: $_";
124 my $error = $_;
125 $error =~ s! at\s+\S+\s+line\s+\d+.!!;
126 $self->event_handler(Event => 'JobFailed',
127 JobName => $job->{name},
128 Command => $job->{command},
129 ReturnValue => -1,
130 Stderr => "<$error>",
131 );
132  
133 $self->_update_status({name => $job->{name}},
134 {
135 command => $job->{command},
136 status => -1,
137 stderr => "<$error>",
138 });
139 return;
140 }
141 finally {
142 $self->_update_status({name => $job->{name}},
143 {Queued => strftime('%d/%m/%Y %H:%M:%S', localtime(time())),
144 });
145 };
146 }
147 }
148  
149 1;
150 __END__
151  
152 =head1 NAME
153  
154 Schedule::Pluggable::Run - Moose Role to provide methods to run schedules
155  
156 =head1 DESCRIPTION
157  
158 Moose Role to add method to run schedules to Schedule::Pluggable - not runnable on it's own
159  
160 =head1 METHODS
161  
162 =over
163  
164 =item run_in_series
165  
166 Utility method to build a schedule where each job depends on the previous one and so they run in series
167  
168 =item run_in_parallel
169  
170 Utility method which removes any dependencies from a schedule causing all the jobs to run in parallel
171  
172 =item run_schedule
173  
174 The main method of this role - takes a schedule definition, validates and reformats it and then starts off the job manager to run them
175  
176 =back
177  
178 =cut
179  
« Pluggable/Plugin/JobsFromXML.pm ↑ Summary