18 my $optpod = <<'=back';
24 =item B<--transport-uri=s>
26 URI of the Metabase API, passed through to send_tr_reports.pl.
32 ~/var/ctr/ contains the three subdirs C<./process/> and C<./sync/> and
35 The move from the sync/ directory via the process/ directory to the
36 done/ directory is done by the send_tr_reports.pl script which we call
37 in the middle of our loop. If a file is later found in the process
38 directory, then we probably want to move it back to the ctr/ and watch
41 Before that, this script here moves the candidates to be sent from the
42 ctr directory into the sync directory.
44 We use both sleep and inotify. From time to time we read the directory
45 anyway, but when inotify gets a CLOSE_WRITE event, then we do it
46 immediately. So if only one computer writes to the directory, we
47 rarely find two rpt files at once, most of the time only one and we
48 send it off as soon as it is 3 seconds old. If another computer writes
49 to the directory, inotify won't notice, and then we come at the usual
61 use File
::Basename
();
62 use File
::Path
qw(mkpath);
63 use Pod
::Usage
qw(pod2usage);
66 use Time
::HiRes
qw(sleep);
68 my @opt = $optpod =~ /B<--(\S+)>/g;
76 pod2usage
(0) if $Opt{help
};
79 # use lib ("$FindBin::RealBin");
81 sub counting_inotify_backed_sleep
($$) {
82 my($sleep,$inotify) = @_;
83 my $eta = time+$sleep;
85 my $ts = POSIX
::strftime
"%FT%T", localtime;
87 my $left = $eta - time;
89 my @events = $inotify->read;
90 for my $ev (@events) {
92 if ($name =~ /\.rpt/){
93 # printf "!!! inotify event: %s\n", File::Basename::basename($name);
98 printf "\r%s sleeping %d: %d ", $ts, $sleep, $left;
104 my $ctr_dir = "/home/sand/var/ctr";
105 my $sync_dir = "$ctr_dir/sync";
107 if ($inotify = new Linux
::Inotify2
) {
108 $inotify->blocking(0);
115 warn "Unable to create new inotify object: $!";
123 # Could not opendir '/home/sand/var/ctr': Stale file handle at /home/sand/src/andk/andk-cpan-tools/bin/cleanout-ctr-loop.pl line 114.
124 until (opendir $dh, $ctr_dir) {
125 warn "Could not opendir '$ctr_dir': $!; Retrying in 117 seconds\n";
129 for my $dirent (readdir $dh) {
130 next unless $dirent =~ /\.rpt$/;
131 my $abs = "$ctr_dir/$dirent";
132 if ($dirent =~ /^unknown/) {
135 unlink $abs or die "Could not unlink '$abs': $!";
140 WF_PROBABLY_CLOSED
: while () {
141 # wait until this file is older than 2 seconds
143 if (-M
$abs > 3/86400) {
144 last WF_PROBABLY_CLOSED
;
149 my $abs_to = "$sync_dir/$dirent";
150 rename $abs, $abs_to or die "Couldn't mv '$abs' '$abs_to': $!";
155 unless ($found_ctr) {
156 opendir my $dh, $sync_dir or die "Could not opendir '$sync_dir': $!";
157 for my $dirent (readdir $dh) {
158 next unless $dirent =~ /\.rpt$/;
159 my $abs = "$sync_dir/$dirent";
165 if ($found_ctr || $found_sync) {
167 my @opt="$FindBin::RealBin/send_tr_reports.pl";
168 if (my $tu = $Opt{'transport-uri'}) {
169 push @opt, "--transport-uri=$tu";
171 unless (0 == system $^X
, @opt) {
173 warn sprintf "ALERT: Running '%s %s' failed at %s UTC; sleeping $sleep", $^X
, join(" ",@opt), scalar gmtime;
174 $ttgo = time; # when we have a failing submission, we must come back here to work quickier
181 my $donedir = "$ctr_dir/done";
182 opendir my $dh, $donedir or die "Could not opendir $donedir\: $!";
183 for my $dirent (readdir $dh) {
184 my $abs = "$donedir/$dirent";
186 my($todir) = $dirent =~ /(?:pass|fail|unknown|na)\.(.+?)-(?:v?\d)/;
187 die "no todir determined on dirent[$dirent]" unless $todir;
188 my $first_letter = substr($todir,0,1);
189 my $todir_abs = "$donedir/archive/$first_letter/$todir";
190 -d
$todir_abs or mkpath
$todir_abs or die "could not mkdir $todir_abs\: $!";
191 rename $abs, "$todir_abs/$dirent" or die "Could not rename $abs to $todir_abs\: $!";
194 my $sleep = $ttgo - time;
195 if ($sleep >= 1 && $inotify) {
196 counting_inotify_backed_sleep
($sleep, $inotify);