Skip to content

Commit

Permalink
libexec_crom: Add owlmayerMicroscopy_archiver
Browse files Browse the repository at this point in the history
  • Loading branch information
donald committed Nov 29, 2023
1 parent ddbbd2c commit 91ee9fc
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions libexec_cron/prj_owlmayerMicroscopy_archiver.pl
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/archived.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);
}

0 comments on commit 91ee9fc

Please sign in to comment.