-
Notifications
You must be signed in to change notification settings - Fork 3
/
Launch.pm
78 lines (65 loc) · 1.88 KB
/
Launch.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package Launch;
use version; our $VERSION = qv('0.0.1');
use strict;
use warnings;
use Data::Dumper;
use feature 'say';
use Carp;
use Cwd;
use File::Spec::Functions;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use IPC::Open3;
use Log::Log4perl qw/get_logger/;
use Parallel::ForkManager;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw();
our @EXPORT = qw(launch plaunch);
=head2 launch
expected - This is a file (or arrayref of files) which we expect to be produce.
force - Run even if file exists.
=cut
sub launch{
my ($cmd, %opt) = @_;
my $logger = get_logger("Launch");
my $hash = md5_hex($cmd);
my $force = delete $opt{force} // 0;
$logger->info("running [$cmd], force = $force");
my @expected;
if (exists $opt{expected}){
if (ref $opt{expected} eq 'ARRAY'){
@expected = @{$opt{expected}}
} else {
@expected = ($opt{expected});
}
delete $opt{expected}
}
die "unknown parameters passed to doit" . Dumper \%opt if (%opt);
if (!$force){
if (! scalar(@expected) || grep {-f} @expected){
$logger->info("Already done, skipping: '$cmd' ");
return 1;
}
}
if (0==system($cmd)){
if (! @expected || grep {-f} @expected){
$logger->info("Successfully launched and finished [$cmd]");
} else {
$logger->logdie("command seems to have run but expected files not produced [$cmd]");
}
} else {
$logger->logdie("failed to run, dying: [$cmd]");
}
}
sub plaunch{
my ($numprocs, @jobs) = @_;
# Max 30 processes for parallel download
my $pm = new Parallel::ForkManager($numprocs);
foreach my $j (@jobs) {
$pm->start and next; # do the fork
launch(@$j);
$pm->finish; # do the exit in the child process
}
$pm->wait_all_children;
}
1;