-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libexec_crom: Add owlmayerMicroscopy_archiver
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#! /usr/bin/perl | ||
use strict; | ||
use warnings; | ||
|
||
sub lock_or_exit { | ||
my ($lockfilename) = @_; | ||
open our $LOCKFD , '>>', $lockfilename or die "$lockfilename: $!\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 warn0 { | ||
warn @_; | ||
return 0; | ||
} | ||
|
||
sub archive { | ||
my ($id, $requestfile) = @_; | ||
my $lpath = "/project/owlmayerMicroscopy/$id"; | ||
my $rpath = "/amd/grele/M/MG006/project/owlmayerMicroscopy/$id"; | ||
my ($dev1, $ino1) = lstat $lpath or return warn0("$lpath: $!\n"); | ||
-d _ or return warn0("$lpath: not a directory\n"); | ||
my ($dev2, $ino2) = lstat $rpath or return warn0("$rpath: $!\n"); | ||
unless ($dev1 == $dev2 && $ino1 == $ino2) { | ||
return warn0("$rpath not the same as $lpath\n"); | ||
} | ||
|
||
my $lf = "/project/owlmayerMicroscopy/$id.is-archived-on-tape.log"; | ||
my $lf1 = "/project/owlmayerMicroscopy/home/archive_logs/$id.is-archived-on-tape.log"; | ||
my $lf2 = "/project/owlmayerMicroscopy/$id/archive.log"; | ||
-e $lf1 and return warn0("$lf1: already exists\n"); | ||
-e $lf2 and return warn0("$lf2: already exists\n"); | ||
|
||
warn "archive $rpath\n"; | ||
system '/project/admin/tools/archiver.pl', $rpath and return 0; | ||
|
||
rename $lf, $lf1 or return warn0("failed to rename $lf to $lf1: $!\n"); | ||
link $lf1, $lf2 or return warn0("failed to link $lf1 to $lf2\n"); | ||
|
||
unlink("$lpath/$requestfile") or warn0("failed to remove $lpath/$requestfile\n"); | ||
return 1; | ||
} | ||
|
||
lock_or_exit("/run/lock/owlmayerMicroscopy-archive"); | ||
chdir '/project/owlmayerMicroscopy' or die "/project/owlmayerMicroscopy: $!\n"; | ||
|
||
umask 022; | ||
|
||
while (1) { | ||
my $progress; | ||
my $errors; | ||
for my $id (scandir '.') { | ||
-d $id or next; | ||
for my $f (scandir $id) { | ||
$f =~ /^archive_me\./ or next; | ||
if(archive($id, $f)) { | ||
$progress++; | ||
} else { | ||
$errors++; | ||
} | ||
} | ||
} | ||
unless ($progress) { | ||
exit($errors ? 1 : 0); | ||
} | ||
sleep(5); | ||
} |