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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
package Pipeliner;
use strict;
use warnings;
use Carp;
use Cwd;
################################
## Verbose levels:
## 1: see CMD string
## 2: see stderr during process
################################
####################
## Static methods:
####################
####
sub ensure_full_path {
my ($path, $ADD_GZ_FIFO_FLAG) = @_;
unless ($path =~ m|^/|) {
$path = cwd() . "/$path";
}
if ($ADD_GZ_FIFO_FLAG && $path =~ /\.gz$/) {
$path = "<(zcat $path)";
}
return($path);
}
####
sub process_cmd {
my ($cmd) = @_;
print STDERR "CMD: $cmd\n";
my $ret = system($cmd);
if ($ret) {
die "Error, CMD: $cmd died with ret $ret";
}
return;
}
################
## Obj methods:
################
####
sub new {
my $packagename = shift;
my %params = @_;
my $VERBOSE = 0;
if ($params{-verbose}) {
$VERBOSE = $params{-verbose};
}
my $cmds_log = $params{-cmds_log};
my $self = {
cmd_objs => [],
checkpoint_dir => undef,
cmds_log_ofh => undef,
VERBOSE => $VERBOSE,
};
bless ($self, $packagename);
if (my $checkpoint_dir = $params{-checkpoint_dir}) {
$self->set_checkpoint_dir($checkpoint_dir);
}
unless ($cmds_log) {
$cmds_log = "pipeliner.$$.cmds";
if (my $checkpoint_dir = $self->get_checkpoint_dir) {
$cmds_log = "$checkpoint_dir/$cmds_log";
}
}
# open cmds log
open (my $ofh, ">$cmds_log") or confess "Error, cannot write to $cmds_log";
$self->{cmds_log_ofh} = $ofh;
return($self);
}
sub add_commands {
my $self = shift;
my @cmds = @_;
foreach my $cmd (@cmds) {
unless (ref($cmd) =~ /Command/) {
confess "Error, need Command object as param";
}
my $checkpoint_file = $cmd->get_checkpoint_file();
if ($checkpoint_file !~ m|^/|) {
if (my $checkpoint_dir = $self->get_checkpoint_dir()) {
$checkpoint_file = "$checkpoint_dir/$checkpoint_file";
$cmd->reset_checkpoint_file($checkpoint_file);
}
}
push (@{$self->{cmd_objs}}, $cmd);
}
return $self;
}
sub set_checkpoint_dir {
my $self = shift;
my ($checkpoint_dir) = @_;
$checkpoint_dir = &ensure_full_path($checkpoint_dir);
if (! -d $checkpoint_dir) {
mkdir($checkpoint_dir) or die "Error, cannot mkdir $checkpoint_dir";
}
$self->{checkpoint_dir} = $checkpoint_dir;
}
sub get_checkpoint_dir {
my $self = shift;
return($self->{checkpoint_dir});
}
sub has_commands {
my $self = shift;
if ($self->_get_commands()) {
return(1);
}
else {
return(0);
}
}
sub run {
my $self = shift;
my $VERBOSE = $self->{VERBOSE};
my $cmds_log_ofh = $self->{cmds_log_ofh};
foreach my $cmd_obj ($self->_get_commands()) {
my $cmdstr = $cmd_obj->get_cmdstr();
print $cmds_log_ofh "$cmdstr\n";
my $msg = $cmd_obj->{msg};
my $checkpoint_file = $cmd_obj->get_checkpoint_file();
if (-e $checkpoint_file) {
print STDERR "-- Skipping CMD: $cmdstr, checkpoint [$checkpoint_file] exists.\n" if $VERBOSE;
}
else {
my $datestamp = localtime();
print STDERR "* [$datestamp] Running CMD: $cmdstr\n" if $VERBOSE;
my $tmp_stderr = "tmp.$$." . time() . ".stderr";
if (-e $tmp_stderr) {
unlink($tmp_stderr);
}
if ($VERBOSE < 2 && $cmdstr !~ / 2>/ ) {
$cmdstr .= " 2>$tmp_stderr";
}
print STDERR $msg if $msg;
my $ret = system($cmdstr);
if ($ret) {
if (-e $tmp_stderr) {
my $errmsg = `cat $tmp_stderr`;
if ($errmsg =~ /\w/) {
print STDERR "\n\nError encountered:: <!----\nCMD: $cmdstr\n\nErrmsg:\n$errmsg\n--->\n\n";
}
unlink($tmp_stderr);
}
confess "Error, cmd: $cmdstr died with ret $ret $!";
}
else {
`touch $checkpoint_file`;
if ($?) {
confess "Error creating checkpoint file: $checkpoint_file";
}
}
if (-e $tmp_stderr) {
unlink($tmp_stderr);
}
}
}
# reset in case reusing the pipeline obj
$self->{cmd_objs} = []; # reinit
return;
}
sub _get_commands {
my $self = shift;
return(@{$self->{cmd_objs}});
}
package Command;
use strict;
use warnings;
use Carp;
sub new {
my $packagename = shift;
my ($cmdstr, $checkpoint_file, $message) = @_;
unless ($cmdstr && $checkpoint_file) {
confess "Error, need cmdstr and checkpoint filename as params";
}
my $self = { cmdstr => $cmdstr,
checkpoint_file => $checkpoint_file,
msg => $message,
};
bless ($self, $packagename);
return($self);
}
####
sub get_cmdstr {
my $self = shift;
return($self->{cmdstr});
}
####
sub get_checkpoint_file {
my $self = shift;
return($self->{checkpoint_file});
}
####
sub reset_checkpoint_file {
my $self = shift;
my $checkpoint_file = shift;
$self->{checkpoint_file} = $checkpoint_file;
}
1; #EOM
|