| 1 |
296 |
ahitrov |
#!/usr/bin/perl -w |
| 2 |
|
|
use strict; |
| 3 |
|
|
|
| 4 |
|
|
umask (002); |
| 5 |
|
|
|
| 6 |
|
|
my $ROOT=$ENV{HOME}."/Contenido/usr/projects/promosuite/services/loader"; |
| 7 |
|
|
my $DATAROOT=$ENV{HOME}."/Contenido/var/projects/promosuite/loader"; |
| 8 |
|
|
my $WDIR="$DATAROOT/incom/"; |
| 9 |
|
|
my $EDIR="$DATAROOT/error/"; |
| 10 |
|
|
my $PDIR="$DATAROOT/done/"; |
| 11 |
|
|
my $PIDF="$DATAROOT/logs/smaild.lock"; |
| 12 |
|
|
my $LOGF="$DATAROOT/logs/smaild.log"; |
| 13 |
|
|
#my $EXTCOM="$ROOT/bin/msgbody \| $ROOT/bin/loader"; |
| 14 |
|
|
my $EXTCOM="$ROOT/bin/load.sh"; |
| 15 |
|
|
|
| 16 |
|
|
my $MAXDTM=900; |
| 17 |
|
|
my $N2proc=150; |
| 18 |
|
|
|
| 19 |
|
|
|
| 20 |
|
|
## Locking |
| 21 |
|
|
my ($errcode,$errmsg)=proc_lock($PIDF,$MAXDTM); |
| 22 |
|
|
if ($errcode==1){ |
| 23 |
|
|
die $errmsg; |
| 24 |
|
|
} |
| 25 |
|
|
elsif ($errcode==2){ |
| 26 |
|
|
warn $errmsg; |
| 27 |
|
|
exit; |
| 28 |
|
|
} |
| 29 |
|
|
elsif ($errcode==3){ |
| 30 |
|
|
# warn "previous incarnation is running"; |
| 31 |
|
|
exit; |
| 32 |
|
|
|
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
|
| 36 |
|
|
#open log file |
| 37 |
|
|
|
| 38 |
|
|
my $Stm=time(); |
| 39 |
|
|
my $Stmstr=scalar(localtime($Stm)); |
| 40 |
|
|
|
| 41 |
|
|
my $PK="$Stm.$$"; |
| 42 |
|
|
my $log2stderr=0; |
| 43 |
|
|
|
| 44 |
|
|
if (open(LOG,">>$LOGF")){ |
| 45 |
|
|
select LOG; |
| 46 |
|
|
$|=1; |
| 47 |
|
|
select STDOUT; |
| 48 |
|
|
} |
| 49 |
|
|
else{ |
| 50 |
|
|
warn "Can\'t open log file $LOGF: $!\nlogging to STDERR"; |
| 51 |
|
|
$log2stderr=1; |
| 52 |
|
|
open (LOG,">&=STDERR"); |
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
#starting |
| 56 |
|
|
print LOG "$PK\t$Stm\t$Stmstr\tStarted\n"; |
| 57 |
|
|
my $tm=time(); |
| 58 |
|
|
my $tmstr=scalar(localtime($tm)); |
| 59 |
|
|
print LOG "$PK\t$tm\t$tmstr\tScanning workdir $WDIR for new files\n"; |
| 60 |
|
|
|
| 61 |
|
|
my %tmpf; |
| 62 |
|
|
my @files; |
| 63 |
|
|
if (opendir(DIR,$WDIR)){ |
| 64 |
|
|
my $totN=0; |
| 65 |
|
|
while (defined(my $f=readdir(DIR))){ |
| 66 |
|
|
next if ($f eq '.'); |
| 67 |
|
|
next if ($f eq '..'); |
| 68 |
|
|
my ($part_tm,$part_pid,$part_rand,$part_prior)=split(/\./,$f); |
| 69 |
|
|
$tmpf{$f}{p}=$part_prior; |
| 70 |
|
|
$tmpf{$f}{tm}=$part_tm; |
| 71 |
|
|
$totN++; |
| 72 |
|
|
} |
| 73 |
|
|
closedir DIR; |
| 74 |
|
|
|
| 75 |
|
|
if ($totN){ |
| 76 |
|
|
@files=sort { |
| 77 |
|
|
if ($tmpf{$a}{p} < $tmpf{$b}{p}){ |
| 78 |
|
|
return 1; |
| 79 |
|
|
} |
| 80 |
|
|
elsif ($tmpf{$a}{p} > $tmpf{$b}{p}){ |
| 81 |
|
|
return -1; |
| 82 |
|
|
} |
| 83 |
|
|
else{ |
| 84 |
|
|
return $tmpf{$a}{tm}<=>$tmpf{$b}{tm}; |
| 85 |
|
|
} |
| 86 |
|
|
} keys (%tmpf); |
| 87 |
|
|
$tm=time(); |
| 88 |
|
|
$tmstr=scalar(localtime()); |
| 89 |
|
|
my $procN=$totN; |
| 90 |
|
|
if ($N2proc<$totN){ |
| 91 |
|
|
@files=splice(@files,0,$N2proc); |
| 92 |
|
|
$procN=$N2proc; |
| 93 |
|
|
} |
| 94 |
|
|
print LOG "$PK\t$tm\t$tmstr\t$totN files in workdir, $procN of them will be processed\n"; |
| 95 |
|
|
# map {print "$_\n"} @files; |
| 96 |
|
|
|
| 97 |
|
|
foreach my $f (@files){ |
| 98 |
|
|
$tm=time(); |
| 99 |
|
|
$tmstr=scalar(localtime()); |
| 100 |
|
|
my $proc_started_tm=$tm; |
| 101 |
|
|
print LOG "$PK\t$tm\t$tmstr\tProcessing $f by $EXTCOM\n"; |
| 102 |
|
|
my $err=system("$EXTCOM < $WDIR$f"); |
| 103 |
|
|
$tm=time(); |
| 104 |
|
|
$tmstr=scalar(localtime()); |
| 105 |
|
|
unless ($err){ |
| 106 |
|
|
my $proc_l=$tm - $proc_started_tm; |
| 107 |
|
|
print LOG "$PK\t$tm\t$tmstr\tFile $f processed [$proc_l s]\n"; |
| 108 |
|
|
unless (rename("$WDIR$f","$PDIR$f")){ |
| 109 |
|
|
print LOG "$PK\t$tm\t$tmstr\tError: Can\'t move $WDIR$f to $PDIR$f: $!\n"; |
| 110 |
|
|
warn "Can\'t move $WDIR$f to $PDIR$f: $!" unless($log2stderr); |
| 111 |
|
|
} |
| 112 |
|
|
} |
| 113 |
|
|
else{ |
| 114 |
|
|
print LOG "$PK\t$tm\t$tmstr\tError: processing $f by $EXTCOM: $!: $?\n"; |
| 115 |
|
|
warn "$PK\t$tm\t$tmstr\tError: processing $f by $EXTCOM: $!: $?" unless ($log2stderr); |
| 116 |
|
|
if (rename("$WDIR$f","$EDIR$f")){ |
| 117 |
|
|
print LOG "$PK\t$tm\t$tmstr\t$f moved to $EDIR\n"; |
| 118 |
|
|
} |
| 119 |
|
|
else{ |
| 120 |
|
|
warn "Can\'t move $WDIR$f to $EDIR$f: $!"; |
| 121 |
|
|
} |
| 122 |
|
|
} |
| 123 |
|
|
} |
| 124 |
|
|
} |
| 125 |
|
|
else{ |
| 126 |
|
|
$tm=time(); |
| 127 |
|
|
$tmstr=scalar(localtime()); |
| 128 |
|
|
print LOG "$PK\t$tm\t$tmstr\tWorkdir $WDIR is empty\n"; |
| 129 |
|
|
} |
| 130 |
|
|
} |
| 131 |
|
|
else{ |
| 132 |
|
|
$tm=time(); |
| 133 |
|
|
$tmstr=scalar(localtime($tm)); |
| 134 |
|
|
print LOG "$PK\t$tm\t$tmstr\tError: Can\'t open dir $WDIR for scanning: $!\n"; |
| 135 |
|
|
warn "Can\'t open dir $WDIR for scanning: $!" unless ($log2stderr); |
| 136 |
|
|
} |
| 137 |
|
|
|
| 138 |
|
|
$tm=time(); |
| 139 |
|
|
$tmstr=scalar(localtime()); |
| 140 |
|
|
print LOG "$PK\t$tm\t$tmstr\tFinished\n"; |
| 141 |
|
|
|
| 142 |
|
|
unless ($log2stderr){ |
| 143 |
|
|
close LOG; |
| 144 |
|
|
} |
| 145 |
|
|
|
| 146 |
|
|
## Unlocking; |
| 147 |
|
|
unlink ($PIDF) || die "Can\'t unlink $PIDF: $!"; |
| 148 |
|
|
|
| 149 |
|
|
sub proc_lock{ |
| 150 |
|
|
my ($pidfn,$maxDTM)=@_; |
| 151 |
|
|
my $stm=time(); |
| 152 |
|
|
if (-e $pidfn){ |
| 153 |
|
|
if (open(F,$pidfn)){ |
| 154 |
|
|
my $str=<F>; |
| 155 |
|
|
close F; |
| 156 |
|
|
return (1,"empty pid file $pidfn") unless(defined($str)); |
| 157 |
|
|
my ($ppstm,$pppid)=split(/\t/,$str) if ($str); |
| 158 |
|
|
return (1,"No proc with pid $pppid") unless (kill(0,$pppid)); |
| 159 |
|
|
my $ctm=time(); |
| 160 |
|
|
my $dtm=$ctm-$ppstm; |
| 161 |
|
|
if (($ctm-$ppstm)>$maxDTM){ |
| 162 |
|
|
return (2,"Previous proc [pid=$pppid;started: ".scalar(localtime($ppstm))."] working more then $maxDTM secs"); |
| 163 |
|
|
} |
| 164 |
|
|
return (3,''); |
| 165 |
|
|
} |
| 166 |
|
|
else{ |
| 167 |
|
|
my $pidfm=(stat($pidfn))[2]; |
| 168 |
|
|
return (1,"Can\'t open $pidfn [mode=$pidfm]: $!"); |
| 169 |
|
|
} |
| 170 |
|
|
} |
| 171 |
|
|
else{ |
| 172 |
|
|
if (open(F,">$pidfn")){ |
| 173 |
|
|
select F; |
| 174 |
|
|
$|=1; |
| 175 |
|
|
select STDOUT; |
| 176 |
|
|
print F "$stm\t$$"; |
| 177 |
|
|
close F; |
| 178 |
|
|
} |
| 179 |
|
|
else{ |
| 180 |
|
|
return (1,"Can\'t open $pidfn: $!"); |
| 181 |
|
|
} |
| 182 |
|
|
} |
| 183 |
|
|
return 0; |
| 184 |
|
|
} |