Skip to content
Permalink
0708053cdf
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 77 lines (69 sloc) 2.12 KB
#! /usr/bin/perl
use strict;
use warnings;
sub lock_or_exit {
open our $LOCKFD,'>>','/run/lock/minion-archive' or die "/run/lock/minion-archive: $!\n";
unless (flock $LOCKFD,6) { # LOCK_EX+LOCK_NB
$!!=11 and die "$!\n";
warn "already running\n";
exit;
}
}
sub scandir {
my ($dirname)=@_;
opendir my $dir,$dirname or die "$dirname: $!\n";
return sort grep !/^\./,readdir $dir;
}
sub find_dh_pattern {
my ($dh,$pattern)=@_;
while(my $fn=readdir $dh) {
return $1 if $fn=~$pattern;
}
return undef;
}
sub warn0 {
warn @_;
return 0;
}
sub archive {
my ($id)=@_;
my $minionpath="/project/minion/raw/$id";
lstat $minionpath or return warn0("$minionpath: $!\n");
-l _ or return warn0("$minionpath: not a symbolic link\n");
my $target=readlink($minionpath) or die "$minionpath: $!\n";
$target=~s#/+$##;
my $miniondatapath="/project/miniondata/raw/$id";
$target eq $miniondatapath or return warn0("$minionpath: invalid target $target\n");
my ($dev1,$ino1)=lstat $target or return warn0("$target: $!\n");
-d _ or return warn0("$target: not a directory\n");
my $fullpath="/amd/mercwithamouth/M/M8026/project/miniondata/raw/$id";
my ($dev2,$ino2)=lstat $fullpath or die "$fullpath: $!\n";
unless ($dev1==$dev2 && $ino1==$ino2) {
die "$fullpath not the same as $target\n";
}
-e "$fullpath.is-archived-on-tape.log" and die "$fullpath.is-archived-on-tape.log already exists\n";
warn "archive $fullpath\n";
system '/project/admin/tools/archiver.pl',$fullpath and exit 1;
system 'mv',"$fullpath.is-archived-on-tape.log","/project/minion/archive/$id.ARCHIVE_COMPLETED" and exit 1;
unlink("/project/minion/archive/$id.ARCHIVE_REQUESTED") or die "/project/minion/archive/$id.ARCHIVE_REQUESTED: $!\n";
return 1;
}
lock_or_exit();
chdir '/project/minion/archive' or die "/project/minion/archive: $!\n";
while (1) {
my $progress;
my $errors;
opendir my $dh,'.';
while (my $id=find_dh_pattern($dh,qr/^(.+)\.ARCHIVE_REQUESTED$/)) {
redo unless -e "$id.ARCHIVE_REQUESTED"; # Operator changed his mind?
if (archive($id)) {
$progress++;
} else {
$errors++;
}
}
unless ($progress) {
exit($errors ? 1 : 0);
}
sleep 5;
}