Skip to content

Instantly share code, notes, and snippets.

@tomredsky
Created January 25, 2018 11:56
Show Gist options
  • Save tomredsky/d69d69e588ba00060350465e50b44195 to your computer and use it in GitHub Desktop.
Save tomredsky/d69d69e588ba00060350465e50b44195 to your computer and use it in GitHub Desktop.
#!/opt/perl5/bin/perl
use Linux::Inotify2;
use Path::Tiny;
use lib 'lib';
use SPL::AppConfig 'conf';
my $config = conf()->{'Model::DSS'};
my $path = $config->{args}->{spool_dir} . '/mediasets';
my $user = $config->{user};
my $rhost = $config->{rhost};
my $rdir = $config->{rspool} . '/lightbox/';
my $cmd = '/usr/bin/scp';
my $id = '-i /usr/local/webdata/lib/.ssh/splupload_id.rsa';
my $notify = Linux::Inotify2->new() or
die "Unable to create new inotify object: $!";
$notify->watch($path, IN_CLOSE ) or
die "watch creation failed" ;
while () {
my @events = $notify->read;
unless (@events > 0) {
print "read error: $!";
last ;
}
for (@events) {
# filename does not end in '.xml' initially so the regex needs to be loose.
if ( $_->fullname =~ /\.xml/ ) {
say "Going to check ".$_->fullname. ' and ignored=' . $_->IN_IGNORED . ' with mask=' . $_->mask;
handle_file($_->w->name);
}
}
}
sub handle_file {
my ($dir) = @_;
my @files = path($dir)->children( qr/\.xml\z/ );
for (@files) {
my $file = "$dir/inprogress/". $_->basename;
say "Attemping to move ". $_->basename ." to $dir/inprogress/";
$_->move($file) or die "Cannot move $_->basename :$!\n";
say "Want to scp $file to $config->{rhost}";
my @cmd = ($cmd, $id, $file);
push @cmd, $user . '@' . "${rhost}:${rdir}/new";
say join ' ', @cmd;
if ( system(@cmd) == 0 ) {
path($file)->move("$path/complete/" . $_->basename) or die "Cannot move $file to completed folder:$!";
}
else {
warn "There was an error copying $file to $rhost: $?\n";
path($file)->move("$path/failed/" . $_->basename) or die "Cannot move $file to failed folder:$!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment