| 1 | package Schedule::Pluggable; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use Carp qw/ croak /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | our $VERSION = '0.0.6'; | ||||||||||
| 8 | with 'MooseX::Workers'; | ||||||||||
| 9 | with 'MooseX::Object::Pluggable'; | ||||||||||
| 10 | |||||||||||
| 11 | with 'Schedule::Pluggable::Config'; | ||||||||||
| 12 | with 'Schedule::Pluggable::Run'; | ||||||||||
| 13 | with 'Schedule::Pluggable::EventHandler'; | ||||||||||
| 14 | with 'Schedule::Pluggable::Status'; | ||||||||||
| 15 | |||||||||||
| 16 | our %imports = (); # Anything supplied in the use statement goes in here e.g. use Schedule::Pluggable (something => value); | ||||||||||
| 17 | |||||||||||
| 18 | # merge anything specied in %imports with any parameters passed on object creation | ||||||||||
| 19 | sub BUILDARGS { | ||||||||||
| 20 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
BUILDARGS: not called CC=1 SCAR=6.9
| |||||||||||
| 21 | |||||||||||
| 22 | 0 | my %params = ( %imports, @_ ); | |||||||||
| 23 | 0 | return \%params; | |||||||||
| 24 | } | ||||||||||
| 25 | sub BUILD { | ||||||||||
| 26 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
| |||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | # Populate %imports with whatever gets supplied on the use line | ||||||||||
| 30 | sub import { | ||||||||||
| 31 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
import: not called CC=2 SCAR=17.9
| |||||||||||
| 32 | 0 | ▶ | if (scalar(@_) % 2 == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | %imports = @_; | |||||||||
| 34 | } | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 3 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | 1; # Magic true value required at end of module | ||||||||||
| 41 | __END__ | ||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | =head1 NAME | ||||||||||
| 45 | |||||||||||
| 46 | Schedule::Pluggable - Flexible Perl Process Scheduler | ||||||||||
| 47 | |||||||||||
| 48 | =head1 SYNOPSIS | ||||||||||
| 49 | |||||||||||
| 50 | EXAMPLE #1: Simple Run in Series | ||||||||||
| 51 | use Schedule::Pluggable; | ||||||||||
| 52 | my $p = Schedule::Pluggable->new; | ||||||||||
| 53 | my $status = $p->run_in_series( [ qw/command1 command2 command3/ ] ); | ||||||||||
| 54 | |||||||||||
| 55 | EXAMPLE #2: Simple Run in Parallel | ||||||||||
| 56 | use Schedule::Pluggable; | ||||||||||
| 57 | my $p = Schedule::Pluggable->new; | ||||||||||
| 58 | my $status = $p->run_in_parallel( [ qw/command1 command2 command3/ ] ); | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | EXAMPLE #3: With Job Names | ||||||||||
| 62 | use Schedule::Pluggable; | ||||||||||
| 63 | my $p = Schedule::Pluggable->new; | ||||||||||
| 64 | my @jobs = ( | ||||||||||
| 65 | { name => "FirstJob", command => "somescript.sh" }, | ||||||||||
| 66 | { name => "2nd Job", command => sub { do_something; } }, | ||||||||||
| 67 | ); | ||||||||||
| 68 | my $status = $p->run_schedule( \@jobs ); | ||||||||||
| 69 | |||||||||||
| 70 | EXAMPLE #4: With Prerequsites | ||||||||||
| 71 | use Schedule::Pluggable; | ||||||||||
| 72 | my $p = Schedule::Pluggable->new; | ||||||||||
| 73 | my $jobs = [ { name => "FirstJob", | ||||||||||
| 74 | command => "somescript.sh" }, | ||||||||||
| 75 | { name => "SecondJob", | ||||||||||
| 76 | command => sub { do_something; }, | ||||||||||
| 77 | prerequisites => [qw/FirstJob/] }, | ||||||||||
| 78 | ]; | ||||||||||
| 79 | my $status = $p->run_schedule( $jobs ); | ||||||||||
| 80 | |||||||||||
| 81 | EXAMPLE #5: Same as #4 but with dependencies | ||||||||||
| 82 | use Schedule::Pluggable; | ||||||||||
| 83 | my $p = Schedule::Pluggable->new; | ||||||||||
| 84 | my $jobs = [ { name => "FirstJob", | ||||||||||
| 85 | command => "somescript.sh", | ||||||||||
| 86 | dependencies => [qw/SecondJob/] }, | ||||||||||
| 87 | { name => "SecondJob", | ||||||||||
| 88 | command => sub { do_something; } }, | ||||||||||
| 89 | ]; | ||||||||||
| 90 | my $status = $ps->run_schedule( $jobs ); | ||||||||||
| 91 | |||||||||||
| 92 | EXAMPLE #5: With Groups | ||||||||||
| 93 | use Schedule::Pluggable; | ||||||||||
| 94 | my $p = Schedule::Pluggable->new; | ||||||||||
| 95 | my $jobs = ( { name => "one", command => "one.sh", dependencies => [ qw/Reports/ ] }, | ||||||||||
| 96 | { name => "two", command => "two.pl" }, groups => [ qw/Reports/] }, | ||||||||||
| 97 | { name => "three", command => "three.pl" }, groups => [ qw/Reports/] }, | ||||||||||
| 98 | { name => "four", command => "four.ksh" }, prerequisites => [ qw/Reports/] }, | ||||||||||
| 99 | ); | ||||||||||
| 100 | my $status = $p->run_schedule( $jobs ); | ||||||||||
| 101 | |||||||||||
| 102 | EXAMPLE #6: Getting the config from an XML file | ||||||||||
| 103 | |||||||||||
| 104 | use Schedule::Pluggable (JobsConfig => 'JobsFromXML'); | ||||||||||
| 105 | my $p = Schedule::Pluggable->new; | ||||||||||
| 106 | my $status = $p->run_schedule({XMLFile => 'path to xml file'}); | ||||||||||
| 107 | |||||||||||
| 108 | XMlFile in following format :- | ||||||||||
| 109 | <?xml version="1.0"?> | ||||||||||
| 110 | <Jobs> | ||||||||||
| 111 | <Job name='Job1' command='succeed.pl'> | ||||||||||
| 112 | <params>3</params> | ||||||||||
| 113 | <dependencies>second</dependencies> | ||||||||||
| 114 | </Job> | ||||||||||
| 115 | <Job name='Job2' command='fail.pl'> | ||||||||||
| 116 | <params>3</params> | ||||||||||
| 117 | <group>second</group> | ||||||||||
| 118 | </Job> | ||||||||||
| 119 | ... | ||||||||||
| 120 | <Jobs> | ||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | =head1 DESCRIPTION | ||||||||||
| 124 | |||||||||||
| 125 | Schedule::Pluggable is a perl module which provides a simple but powerful way of running processes in a controlled way. | ||||||||||
| 126 | In true perl fashion it makes simple things easy and complicated things possible. | ||||||||||
| 127 | It also uses a system of plugins so you can change it's behaviour to suit your requirements by supplying your own plugins. | ||||||||||
| 128 | For most cases the default plugins will suffice however. | ||||||||||
| 129 | |||||||||||
| 130 | =head1 OPTIONS | ||||||||||
| 131 | |||||||||||
| 132 | You can override the default behaviour of Schedule::Pluggable by supplying options with the use statement in for form of a hash | ||||||||||
| 133 | |||||||||||
| 134 | i.e. | ||||||||||
| 135 | |||||||||||
| 136 | use Schedule::Pluggable ( Option => "value' ); | ||||||||||
| 137 | |||||||||||
| 138 | The Following options are supported :- | ||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | =head2 B<JobsConfig> | ||||||||||
| 142 | |||||||||||
| 143 | Specifies which plugin to use to provide the job configuration - defaults to JobsFromData which expects you to supply the job configuration in an array | ||||||||||
| 144 | |||||||||||
| 145 | Each plugin is expected to | ||||||||||
| 146 | |||||||||||
| 147 | e.g. | ||||||||||
| 148 | |||||||||||
| 149 | use Schedule::Pluggable ( JobsConfig => 'JobsFromSomeWhere' ); | ||||||||||
| 150 | |||||||||||
| 151 | Currently the available values are :- | ||||||||||
| 152 | |||||||||||
| 153 | =over | ||||||||||
| 154 | |||||||||||
| 155 | =item B<JobsFromArray> | ||||||||||
| 156 | |||||||||||
| 157 | The default which activates the role 'Schedule::Pluggable::Plugin::JobsFromData' which as the name suggests expects the job configuration to be be supplied as an reference to an array of jobs to run. | ||||||||||
| 158 | |||||||||||
| 159 | =item B<JobsFromXML> | ||||||||||
| 160 | |||||||||||
| 161 | Activates plugin Schedule::Pluggable::Plugin::JobsFromXML which obtains the jobs configuration from an XML file | ||||||||||
| 162 | |||||||||||
| 163 | =back | ||||||||||
| 164 | |||||||||||
| 165 | This enables you to specify a different source for the config by supplying an appropriate plugin for it - see writing Plugins for details | ||||||||||
| 166 | |||||||||||
| 167 | |||||||||||
| 168 | =head2 B<EventHandler> | ||||||||||
| 169 | |||||||||||
| 170 | Controls what happens when an event happens like a jobs starting a job failing e.t.c | ||||||||||
| 171 | Defaults to DefaultEventHandler which is a plugin Schedule::Pluggable::Plugin::DefaultEventHandler | ||||||||||
| 172 | Here is what is passed depending on event type | ||||||||||
| 173 | |||||||||||
| 174 | =over | ||||||||||
| 175 | |||||||||||
| 176 | =item JobName - Always passed | ||||||||||
| 177 | |||||||||||
| 178 | =item Command - Always passed | ||||||||||
| 179 | |||||||||||
| 180 | =item Stdout - Passed on JobStdout and JobSucceeded only | ||||||||||
| 181 | |||||||||||
| 182 | =item Stderr - Passed on JobStderr and JobFailed | ||||||||||
| 183 | |||||||||||
| 184 | =item ReturnValue - Passed on JobFailed | ||||||||||
| 185 | |||||||||||
| 186 | =back | ||||||||||
| 187 | |||||||||||
| 188 | This handler uses other configuration options to control it's behaviour as follows :- | ||||||||||
| 189 | |||||||||||
| 190 | =head3 B<EventsToReport> | ||||||||||
| 191 | |||||||||||
| 192 | Comma separated list of events to report on or 'all' for al of them of 'none' for none of them | ||||||||||
| 193 | Defaults to qq/JobFailed,JobSucceeded,JobStderr/ | ||||||||||
| 194 | |||||||||||
| 195 | e.g. | ||||||||||
| 196 | |||||||||||
| 197 | use Schedule::Pluggable ( EventsToReport => qw/JobQueued,JobFailed,JobSucceeded,JobStderr/ ); | ||||||||||
| 198 | |||||||||||
| 199 | =head3 B<PrefixWithTimeStamp> | ||||||||||
| 200 | |||||||||||
| 201 | whether to prefix messages with the current time in dd/mm/yyyy HH::MM::SS format. | ||||||||||
| 202 | |||||||||||
| 203 | Defaults to 1 (timestamp is produced) | ||||||||||
| 204 | |||||||||||
| 205 | =head2 B<MessagesTo> | ||||||||||
| 206 | |||||||||||
| 207 | where messages are sent - stdout by default | ||||||||||
| 208 | |||||||||||
| 209 | If supplied a filehandle, will call the print method on it and pass the details, for anything else | ||||||||||
| 210 | will call directly. | ||||||||||
| 211 | So this could be a Log::Log4perl method e.g. $log->info or $log->{ Category }->info | ||||||||||
| 212 | |||||||||||
| 213 | =head2 B<ErrorsTo> | ||||||||||
| 214 | |||||||||||
| 215 | where error messages are sent - stderr by default | ||||||||||
| 216 | |||||||||||
| 217 | If supplied a filehandle, will call the print method on it and pass the details, for anything else | ||||||||||
| 218 | will call directly. | ||||||||||
| 219 | So this could be a Log::Log4perl method e.g. $log->error or $log->{ Category }->error | ||||||||||
| 220 | |||||||||||
| 221 | |||||||||||
| 222 | e.g. | ||||||||||
| 223 | |||||||||||
| 224 | use Schedule::Pluggable ( ErrorsTo => \&my_logger ); | ||||||||||
| 225 | |||||||||||
| 226 | or | ||||||||||
| 227 | |||||||||||
| 228 | use Schedule::Pluggable; | ||||||||||
| 229 | my $p = Schedule::Pluggable->new( MessagesTo => \&my_logger ); | ||||||||||
| 230 | |||||||||||
| 231 | |||||||||||
| 232 | =head1 JOB CONIFIGURATION FORMAT | ||||||||||
| 233 | |||||||||||
| 234 | A Job entry can be a scalar value in which case it is assumed to contain a command to run or a hash containing some or all of the following :- | ||||||||||
| 235 | |||||||||||
| 236 | =over | ||||||||||
| 237 | |||||||||||
| 238 | =item name | ||||||||||
| 239 | |||||||||||
| 240 | the name of the job | ||||||||||
| 241 | |||||||||||
| 242 | =item command - command to run | ||||||||||
| 243 | |||||||||||
| 244 | =item params - array of parameters to the command | ||||||||||
| 245 | |||||||||||
| 246 | =item groups - array of groups to which the job belongs | ||||||||||
| 247 | |||||||||||
| 248 | =item prerequisites - array of jobs or groups which must have completed successfully before job with start | ||||||||||
| 249 | |||||||||||
| 250 | =item dependencies - array of jobs or groups which must wait until this job has completed successfully before they will start | ||||||||||
| 251 | |||||||||||
| 252 | =back | ||||||||||
| 253 | |||||||||||
| 254 | Obviously the bare minimum is to supply a command to run | ||||||||||
| 255 | If a name is not supplied, it will be allocated one in the format Jobn where n is an incrementing number starting at 1 and increases with each job specified | ||||||||||
| 256 | |||||||||||
| 257 | =head1 METHODS | ||||||||||
| 258 | |||||||||||
| 259 | =over | ||||||||||
| 260 | |||||||||||
| 261 | =item run_in_series ( $job_specification ) | ||||||||||
| 262 | |||||||||||
| 263 | Utility method to run the supplied jobs in series by creating dependencies where each job is dependant on the previous one and then calls run_schedule with the revised definition | ||||||||||
| 264 | |||||||||||
| 265 | =item run_in_parallel ( $job_specification ) | ||||||||||
| 266 | |||||||||||
| 267 | Runs the supplied jobs in parallel | ||||||||||
| 268 | |||||||||||
| 269 | Utility method to run the supplied jobs in parallel by removing and dependencies which are defined and the call run_schedule | ||||||||||
| 270 | |||||||||||
| 271 | =item run_schedule ( $job_specification ) | ||||||||||
| 272 | |||||||||||
| 273 | The main method of the module - takes a supplied job definition - processes the information to validate and expand the definition and then runs the jobs as specified. | ||||||||||
| 274 | When any event occurs, the appropriate callback is called if required to report on progress and on completion returns a structure detailing what happened in the following format :- | ||||||||||
| 275 | |||||||||||
| 276 | $status = { TotalJobs => <total number of jobs in schedule>, | ||||||||||
| 277 | TotalFailed => <number of jobs which failed>, | ||||||||||
| 278 | TotalFinished => <number of jobs which finished>, | ||||||||||
| 279 | TotalSucceeded' => <number of successfull jobs>' | ||||||||||
| 280 | LastUpdate => 'dd/mm/yyyy hh::mm::ss', | ||||||||||
| 281 | Failed => { | ||||||||||
| 282 | <Job which failed> => { | ||||||||||
| 283 | status => <return value of job>, | ||||||||||
| 284 | stderr => [ | ||||||||||
| 285 | 'error line 1', | ||||||||||
| 286 | .... | ||||||||||
| 287 | ], | ||||||||||
| 288 | }, | ||||||||||
| 289 | }, | ||||||||||
| 290 | Jobs => { | ||||||||||
| 291 | <Job Name> => { | ||||||||||
| 292 | name => <Job Name>, | ||||||||||
| 293 | command => <command> | ||||||||||
| 294 | status => <return value of command> | ||||||||||
| 295 | Pid => <Process Id>, | ||||||||||
| 296 | timestarted => 'dd/mm/yyyy hh::mm::ss', | ||||||||||
| 297 | timefinished=> 'dd/mm/yyyy hh::mm::ss', | ||||||||||
| 298 | stderr => [ | ||||||||||
| 299 | 'error line 1', | ||||||||||
| 300 | .... | ||||||||||
| 301 | ], | ||||||||||
| 302 | stdout => [ | ||||||||||
| 303 | 'output line 1', | ||||||||||
| 304 | .... | ||||||||||
| 305 | ], | ||||||||||
| 306 | }, | ||||||||||
| 307 | ........ | ||||||||||
| 308 | }, | ||||||||||
| 309 | }; | ||||||||||
| 310 | |||||||||||
| 311 | =item BUILDARGS Handles module options via import or passed on objet creation | ||||||||||
| 312 | |||||||||||
| 313 | =item BUILD Handles loading plugins | ||||||||||
| 314 | |||||||||||
| 315 | =back | ||||||||||
| 316 | |||||||||||
| 317 | =head1 JOB DEFINITIONS | ||||||||||
| 318 | |||||||||||
| 319 | Jobs are specified as reference to an array which can contain either a list of commands ot run or as hash values | ||||||||||
| 320 | |||||||||||
| 321 | =over | ||||||||||
| 322 | |||||||||||
| 323 | =item scalar values containing commands to run | ||||||||||
| 324 | |||||||||||
| 325 | =item hashes containing at least one key 'command' with the value containing the command to run | ||||||||||
| 326 | |||||||||||
| 327 | =back | ||||||||||
| 328 | |||||||||||
| 329 | |||||||||||
| 330 | |||||||||||
| 331 | =head1 AUTHOR - | ||||||||||
| 332 | |||||||||||
| 333 | Tony Edwardson <Tony@Edwardson.co.uk> | ||||||||||
| 334 | |||||||||||
| 335 | =head1 KNOWN ERRORS | ||||||||||
| 336 | |||||||||||
| 337 | None yet - let me know if you find any | ||||||||||
| 338 | |||||||||||
| 339 | =head1 TODO | ||||||||||
| 340 | |||||||||||
| 341 | =over | ||||||||||
| 342 | |||||||||||
| 343 | =item Improve Test Suite | ||||||||||
| 344 | |||||||||||
| 345 | =item Improve Error Handling | ||||||||||
| 346 | |||||||||||
| 347 | =item Add more Plugins | ||||||||||
| 348 | |||||||||||
| 349 | =item Handle Job Timeouts | ||||||||||
| 350 | |||||||||||
| 351 | =back | ||||||||||
| 352 | |||||||||||
| 353 | =cut | ||||||||||