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);
67 my @opt = $optpod =~ /B<--(\S+)>/g;
75 pod2usage
(0) if $Opt{help
};
78 # use lib ("$FindBin::RealBin");
80 sub counting_inotify_backed_sleep
($$) {
81 my($sleep,$inotify) = @_;
82 my $eta = time+$sleep;
84 my $ts = POSIX
::strftime
"%FT%T", localtime;
86 my $left = $eta - time;
88 my @events = $inotify->read;
89 for my $ev (@events) {
91 if ($name =~ /\.rpt/){
92 # printf "!!! inotify event: %s\n", File::Basename::basename($name);
97 printf "\r%s sleeping %d: %d ", $ts, $sleep, $left;
103 my $ctr_dir = "/home/sand/var/ctr";
104 my $sync_dir = "$ctr_dir/sync";
106 if ($inotify = new Linux
::Inotify2
) {
107 $inotify->blocking(0);
114 warn "Unable to create new inotify object: $!";
122 # 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.
123 until (opendir $dh, $ctr_dir) {
124 warn "Could not opendir '$ctr_dir': $!; Retrying in 117 seconds\n";
128 for my $dirent (readdir $dh) {
129 next unless $dirent =~ /\.rpt$/;
130 my $abs = "$ctr_dir/$dirent";
131 if ($dirent =~ /^unknown/) {
134 unlink $abs or die "Could not unlink '$abs': $!";
139 WF_PROBABLY_CLOSED
: while () {
140 # wait until this file is older than 2 seconds
142 if (-M
$abs > 3/86400) {
143 last WF_PROBABLY_CLOSED
;
148 my $abs_to = "$sync_dir/$dirent";
149 rename $abs, $abs_to or die "Couldn't mv '$abs' '$abs_to': $!";
154 unless ($found_ctr) {
155 opendir my $dh, $sync_dir or die "Could not opendir '$sync_dir': $!";
156 for my $dirent (readdir $dh) {
157 next unless $dirent =~ /\.rpt$/;
158 my $abs = "$sync_dir/$dirent";
164 if ($found_ctr || $found_sync) {
166 my @opt="$FindBin::RealBin/send_tr_reports.pl";
167 if (my $tu = $Opt{'transport-uri'}) {
168 push @opt, "--transport-uri=$tu";
170 unless (0 == system $^X
, @opt) {
171 warn sprintf "ALERT: Running '%s %s' failed at %s UTC; sleeping 30", $^X
, join(" ",@opt), scalar gmtime;
178 my $donedir = "$ctr_dir/done";
179 opendir my $dh, $donedir or die "Could not opendir $donedir\: $!";
180 for my $dirent (readdir $dh) {
181 my $abs = "$donedir/$dirent";
183 my($todir) = $dirent =~ /(?:pass|fail|unknown|na)\.(.+?)-(?:v?\d)/;
184 die "no todir determined on dirent[$dirent]" unless $todir;
185 my $first_letter = substr($todir,0,1);
186 my $todir_abs = "$donedir/archive/$first_letter/$todir";
187 -d
$todir_abs or mkpath
$todir_abs or die "could not mkdir $todir_abs\: $!";
188 rename $abs, "$todir_abs/$dirent" or die "Could not rename $abs to $todir_abs\: $!";
191 my $sleep = $ttgo - time;
192 if ($sleep >= 1 && $inotify) {
193 counting_inotify_backed_sleep
($sleep, $inotify);