| 1 | package Launcher::Cascade; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Launcher::Cascade - a framework for launching processes that depend on one another | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Launcher::Cascade::Simple; | ||||||||||||||||||||||||||||
| 10 | use Launcher::Cascade::Container; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | my $launcher1 = Launcher::Cascade::Simple->new( | ||||||||||||||||||||||||||||
| 13 | -launch_hook => sub { ... }, # what to do to launch | ||||||||||||||||||||||||||||
| 14 | -test_hook => sub { ... }, # what to do to test that it succeeded | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | my $launcher2 = Launcher::Cascade::Simple->new( | ||||||||||||||||||||||||||||
| 17 | -launch_hook => sub { ... }, # what to do to launch | ||||||||||||||||||||||||||||
| 18 | -test_hook => sub { ... }, # what to do to test that it succeeded | ||||||||||||||||||||||||||||
| 19 | -dependencies => [ $launcher1 ], # second launcher depends on success of first | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $container = new Launcher::Cascade::Container | ||||||||||||||||||||||||||||
| 23 | -launchers => [ $launcher1, $launcher2 ]; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | $container->run_session(); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | This module provides a framework to launch processes, test whether they | ||||||||||||||||||||||||||||
| 30 | succeeded or not and report on that. Each process is modeled as an object and | ||||||||||||||||||||||||||||
| 31 | can depend on other processes to start, i.e., a process will not be started | ||||||||||||||||||||||||||||
| 32 | until all the processes it depends upon have successfully been started. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Process launchers must be implemented as C<Launcher::Cascade::Base> objects (or | ||||||||||||||||||||||||||||
| 35 | subclassses thereof). Their launch() method will actually launch the process, | ||||||||||||||||||||||||||||
| 36 | and their test() method will check whether it succeeded or not. Each launcher | ||||||||||||||||||||||||||||
| 37 | can be made to depend on one or more other launchers: it will then refuse to | ||||||||||||||||||||||||||||
| 38 | launch() until the others have been test()ed successfully. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | All the launchers should be given to a C<Launcher::Cascade::Container>, which will | ||||||||||||||||||||||||||||
| 41 | run them in turn and test their status when applicable, until either all of the | ||||||||||||||||||||||||||||
| 42 | Launchers have succeeded or one of them has failed. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | The distribution provides C<Launcher::Cascade::FileReader> to ease the launching | ||||||||||||||||||||||||||||
| 45 | of external (and possibly remote, by means of ssh) commands and the reading of | ||||||||||||||||||||||||||||
| 46 | files. FileReaders should be used in launch() or test() methods of Launchers. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | This base class provides a constructor for its subclasses that accepts named | ||||||||||||||||||||||||||||
| 49 | arguments, as well as function to easily create attributes and their accessors. | ||||||||||||||||||||||||||||
| 50 | All the real functionality has to be implemented in subclasses. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 Constructor | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =over 4 | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =item B<new> I<LIST> | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Creates and returns an instance. I<LIST> should be a list of named parameters. | ||||||||||||||||||||||||||||
| 64 | The values will be passed to the accessors of the same name. Leading dashes | ||||||||||||||||||||||||||||
| 65 | will be removed from the names, and they will be converted to lowercase before | ||||||||||||||||||||||||||||
| 66 | invoking the accessor. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =back | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 29 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 29 | my $proto = shift; | |||||||||||||||||||||||||||
| 75 | 29 | ▶ | my $class = ref($proto)||$proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 29 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 29 | ▶ | while ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 56 | my ($key, $value) = (lc(shift), shift); | |||||||||||||||||||||||||||
| 81 | 56 | $key =~ s/^-+//; | |||||||||||||||||||||||||||
| 82 | 56 | $self->$key($value); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 29 | return $self; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 Functions | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =over 4 | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item B<make_accessors> I<LIST> | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Make accessors in the caller's namespace. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | I<LIST> should contain names of accessors. make_accessors() will generate an | ||||||||||||||||||||||||||||
| 97 | accessor for each name in I<LIST>, that will return the corresponding | ||||||||||||||||||||||||||||
| 98 | attribute's value when called without argument, and set the attribute's value | ||||||||||||||||||||||||||||
| 99 | when called with an argument (in that latter case, the former value is | ||||||||||||||||||||||||||||
| 100 | returned). Example: | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | package MyPackage; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | use Launcher::Cascade; | ||||||||||||||||||||||||||||
| 105 | our @ISA = qw/ Launcher::Cascade /; # inherits constructor | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Launcher::Cascade::make_accessors qw/ first_name last_name /; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Meanwhile, in a nearby piece of code: | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | use MyPackage; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | my $object = new MyPackage -first_name => 'Zaphod'; | ||||||||||||||||||||||||||||
| 116 | print $object->first_name(); # Zaphod | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | print $object->last_name('Beeblebrox'); # undef | ||||||||||||||||||||||||||||
| 119 | print $object->last_name(); # Beeblebrox | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 27 | ▶ | sub make_accessors { | ||||||||||||||||||||||||||
|
Subroutine
make_accessors: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 27 | my ($package) = caller(); | |||||||||||||||||||||||||||
| 126 | 27 | foreach my $name ( @_ ) { | |||||||||||||||||||||||||||
| 127 | 71 | my $method = join '::', $package, $name; | |||||||||||||||||||||||||||
| 128 | 13 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | 305 | ▶ | *$method = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 130 | 305 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 305 | my $old = $self->{"_$name"}; | |||||||||||||||||||||||||||
| 132 | 305 | ▶ | $self->{"_$name"} = $_[0] if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 305 | return $old; | |||||||||||||||||||||||||||
| 134 | 71 | }; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item B<read_default_file> I<FILENAME> | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Reads a file containing the definition of default values for subclasses | ||||||||||||||||||||||||||||
| 141 | attributes. The file should contain name, value pairs, one by line, separated | ||||||||||||||||||||||||||||
| 142 | by an equal sign. Whitespace is ignored at the beginning or end of the line and | ||||||||||||||||||||||||||||
| 143 | on either side of the equal sign. Lines starting with a hash sign are ignored, | ||||||||||||||||||||||||||||
| 144 | as well as blank lines. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | The name should be the fully qualified accessor method name, i.e., the package | ||||||||||||||||||||||||||||
| 147 | and name of the accessor separated by a double colon. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # This is an example | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | MyPackage::first_name = Zaphod | ||||||||||||||||||||||||||||
| 152 | MyPackage::last_name = Beeblebrox | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 13 | my %default; | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub read_default_file { | ||||||||||||||||||||||||||
|
Subroutine
read_default_file: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $filename = shift; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | open my $fh, '<', $filename or die "Cannot read $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | while ( <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | chomp; | |||||||||||||||||||||||||||
| 164 | 0 | s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | next if /^#/ || /^$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^#/ or /^$/
MC/DC0%0/2
/^#/ or /^$/
/^#/
/^$/
Truth table0%0/3
A or B
A= /^#/B= /^$/
| |||||||||||||||||||||||||||||
| 166 | 0 | my ($k, $v) = split /\s*=\s*/, $_, 2; | |||||||||||||||||||||||||||
| 167 | 0 | $default{$k} = $v; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 0 | close $fh; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item B<make_accessors_with_defaults> I<LIST> | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Does the same as make_accessors(), but elements in I<LIST> should go in pairs, | ||||||||||||||||||||||||||||
| 175 | accessor names together with their default values. If the attribute's value has | ||||||||||||||||||||||||||||
| 176 | not explicitly been set, the accessor will return the default value as read | ||||||||||||||||||||||||||||
| 177 | from the defaults file (see read_default_file()) or the default value provided | ||||||||||||||||||||||||||||
| 178 | in I<LIST>. The defaults file overrides the value from I<LIST>. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | package MyPackage; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | use Launcher::Cascade; | ||||||||||||||||||||||||||||
| 183 | our @ISA = qw/ Launcher::Cascade /; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Launcher::Cascade::make_accessors_with_defaults( | ||||||||||||||||||||||||||||
| 186 | first_name => 'Zaphod', | ||||||||||||||||||||||||||||
| 187 | last_name => 'Beeblebrox', | ||||||||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Meanwhile, in a nearby piece of code: | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | use MyPackage; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | my $o = new MyPackage; | ||||||||||||||||||||||||||||
| 195 | print $o->first_name(); # Zaphod | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 45 | ▶ | sub make_accessors_with_defaults { | ||||||||||||||||||||||||||
|
Subroutine
make_accessors_with_defaults: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 45 | my ($package) = caller(); | |||||||||||||||||||||||||||
| 202 | 45 | my %attr = @_; | |||||||||||||||||||||||||||
| 203 | 45 | ▶ | while ( my ($name, $default) = each %attr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 168 | my $method = join '::', $package, $name; | |||||||||||||||||||||||||||
| 205 | 13 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 206 | 1167566 | ▶ | *$method = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 207 | 1167566 | my $self = shift; | |||||||||||||||||||||||||||
| 208 | 1167566 | ▶ | my $old = defined($self->{"_$name"}) ? $self->{"_$name"} : $default{$method} || $default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$default{$method} or $default
MC/DC50%1/2
$default{$method} or $default
$default{$method}
$default
Truth table66%2/3
A or B
A= $default{$method}B= $default
| |||||||||||||||||||||||||||||
| 209 | 1167566 | ▶ | $self->{"_$name"} = $_[0] if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 1167566 | return $old; | |||||||||||||||||||||||||||
| 211 | 168 | }; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =back | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 VERSION | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0.02 | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 13 | our $VERSION = 0.02; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Cédric Bouvier C<< <cbouvi@cpan.org> >> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Copyright (C) 2006 Cédric Bouvier, All Rights Reserved. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 236 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =cut | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 13 | 1; # end of Launcher::Cascade | |||||||||||||||||||||||||||