CDN: perltidy on new/changed code
This commit is contained in:
parent
d309dbce6a
commit
2fc4f3b54d
6 changed files with 291 additions and 279 deletions
|
|
@ -10,9 +10,9 @@
|
|||
|
||||
our $webguiRoot;
|
||||
|
||||
BEGIN {
|
||||
$webguiRoot = "..";
|
||||
unshift (@INC, $webguiRoot."/lib");
|
||||
BEGIN {
|
||||
$webguiRoot = "..";
|
||||
unshift( @INC, $webguiRoot . "/lib" );
|
||||
}
|
||||
|
||||
use strict;
|
||||
|
|
@ -28,31 +28,30 @@ my $override;
|
|||
my $quiet;
|
||||
|
||||
GetOptions(
|
||||
'configFile=s' => \$configFile,
|
||||
'override' => \$override,
|
||||
'migrate' => \$migrate,
|
||||
'quiet' => \$quiet,
|
||||
'configFile=s' => \$configFile,
|
||||
'override' => \$override,
|
||||
'migrate' => \$migrate,
|
||||
'quiet' => \$quiet,
|
||||
);
|
||||
|
||||
|
||||
if ($configFile eq "") {
|
||||
printHelp();
|
||||
exit 4;
|
||||
};
|
||||
if ( $configFile eq "" ) {
|
||||
printHelp();
|
||||
exit 4;
|
||||
}
|
||||
|
||||
if ($help) {
|
||||
printHelp();
|
||||
exit 2;
|
||||
printHelp();
|
||||
exit 2;
|
||||
}
|
||||
|
||||
# don't want two copies of this to run simultaneously
|
||||
unless (flock(DATA, LOCK_EX|LOCK_NB)) {
|
||||
unless ( flock( DATA, LOCK_EX | LOCK_NB ) ) {
|
||||
print "$0 is already running. Exiting.\n";
|
||||
exit 3;
|
||||
}
|
||||
|
||||
sub printHelp {
|
||||
print <<STOP;
|
||||
print <<STOP;
|
||||
|
||||
Usage: perl $0 --configfile=<webguiConfig>
|
||||
|
||||
|
|
@ -97,29 +96,30 @@ EXIT STATUS
|
|||
Content Delivery Network (CDN) is not enabled.
|
||||
|
||||
STOP
|
||||
}
|
||||
} ## end sub printHelp
|
||||
|
||||
if (!($^O =~ /^Win/i) && $> != 0 && !$override) {
|
||||
print "You must be the super user to use this utility.\n";
|
||||
exit 1;
|
||||
if ( !( $^O =~ /^Win/i ) && $> != 0 && !$override ) {
|
||||
print "You must be the super user to use this utility.\n";
|
||||
exit 1;
|
||||
}
|
||||
|
||||
print "Starting..." unless ($quiet);
|
||||
my $session = WebGUI::Session->open($webguiRoot,$configFile);
|
||||
$session->user({userId=>3});
|
||||
my $session = WebGUI::Session->open( $webguiRoot, $configFile );
|
||||
$session->user( { userId => 3 } );
|
||||
print "OK\n" unless ($quiet);
|
||||
|
||||
my $cdnCfg = $session->config->get('cdn');
|
||||
unless ($cdnCfg and $cdnCfg->{'enabled'} and $cdnCfg->{'queuePath'}) {
|
||||
print "Content delivery network (CDN) is not enabled in $configFile.\n";
|
||||
exit 5;
|
||||
unless ( $cdnCfg and $cdnCfg->{'enabled'} and $cdnCfg->{'queuePath'} ) {
|
||||
print "Content delivery network (CDN) is not enabled in $configFile.\n";
|
||||
exit 5;
|
||||
}
|
||||
|
||||
# Here is the core of the script
|
||||
if ($migrate) {
|
||||
syncUploads($session);
|
||||
} else {
|
||||
syncQueue($session, $cdnCfg);
|
||||
if ($migrate) {
|
||||
syncUploads($session);
|
||||
}
|
||||
else {
|
||||
syncQueue( $session, $cdnCfg );
|
||||
}
|
||||
|
||||
print "Cleaning up..." unless ($quiet);
|
||||
|
|
@ -129,77 +129,81 @@ $session->close();
|
|||
print "OK\n" unless ($quiet);
|
||||
exit 0;
|
||||
|
||||
|
||||
#-----------------------------------------
|
||||
# syncQueue(session, cdnConfig)
|
||||
#-----------------------------------------
|
||||
|
||||
sub syncQueue {
|
||||
sub syncQueue {
|
||||
my $session = shift;
|
||||
my $cdnCfg = shift;
|
||||
my $cdnCfg = shift;
|
||||
my $locIter = WebGUI::Storage->getCdnFileIterator($session);
|
||||
while (my $store = $locIter->()) {
|
||||
my $ctrlFile = $cdnCfg->{'queuePath'} . '/' . $session->id->toHex($store->getId);
|
||||
if (-r $ctrlFile and -s $ctrlFile < 12) {
|
||||
if (! -s $ctrlFile) { # Empty means sync/add/update
|
||||
$store->syncToCdn;
|
||||
} else { # expect "deleted" but be careful.
|
||||
if (open my $ctrlFH, "<$ctrlFile") {
|
||||
my $directive = <$ctrlFH>;
|
||||
chomp $directive;
|
||||
close $ctrlFH;
|
||||
if ($directive =~ m/^deleted$/i) {
|
||||
$store->deleteFromCdn;
|
||||
} # else unknown - ignore
|
||||
} else {
|
||||
warn "Cannot read CDN control file $ctrlFile.";
|
||||
$session->errorHandler->warn("Cannot read CDN control file $ctrlFile.");
|
||||
}
|
||||
}
|
||||
} else { # missing or invalid
|
||||
print "No recognizable CDN control file $ctrlFile.\n";
|
||||
}
|
||||
}
|
||||
} # end syncQueue
|
||||
|
||||
while ( my $store = $locIter->() ) {
|
||||
my $ctrlFile = $cdnCfg->{'queuePath'} . '/' . $session->id->toHex( $store->getId );
|
||||
if ( -r $ctrlFile and -s $ctrlFile < 12 ) {
|
||||
if ( !-s $ctrlFile ) { # Empty means sync/add/update
|
||||
$store->syncToCdn;
|
||||
}
|
||||
else { # expect "deleted" but be careful.
|
||||
if ( open my $ctrlFH, "<$ctrlFile" ) {
|
||||
my $directive = <$ctrlFH>;
|
||||
chomp $directive;
|
||||
close $ctrlFH;
|
||||
if ( $directive =~ m/^deleted$/i ) {
|
||||
$store->deleteFromCdn;
|
||||
} # else unknown - ignore
|
||||
}
|
||||
else {
|
||||
warn "Cannot read CDN control file $ctrlFile.";
|
||||
$session->errorHandler->warn("Cannot read CDN control file $ctrlFile.");
|
||||
}
|
||||
}
|
||||
} ## end if ( -r $ctrlFile and ...
|
||||
else { # missing or invalid
|
||||
warn "No recognizable CDN control file $ctrlFile.";
|
||||
$session->errorHandler->warn("No recognizable CDN control file $ctrlFile.");
|
||||
}
|
||||
} ## end while ( my $store = $locIter...
|
||||
} # end syncQueue
|
||||
|
||||
#-----------------------------------------
|
||||
# syncUploads(session)
|
||||
#-----------------------------------------
|
||||
|
||||
sub syncUploads {
|
||||
sub syncUploads {
|
||||
my $session = shift;
|
||||
|
||||
# Alternate approach would be touch queue files, then run queue.
|
||||
my $uDir = $session->config->get('uploadsPath');
|
||||
if (opendir my $DH, $uDir) {
|
||||
my @part1 = grep { !/^\.+$/ } readdir($DH);
|
||||
foreach my $subdir (@part1) {
|
||||
if (opendir my $SD, "$uDir/$subdir") {
|
||||
my @part2 = grep { !/^\.+$/ } readdir($SD);
|
||||
foreach my $sub2 (@part2) {
|
||||
if (opendir my $S2, "$uDir/$subdir/$sub2") {
|
||||
my @fileId = grep { !/^\.+$/ } readdir($S2);
|
||||
foreach my $fileId (@fileId) {
|
||||
my $store = WebGUI::Storage->get($session,
|
||||
$session->id->fromHex($fileId));
|
||||
$store->syncToCdn; # here is the meat
|
||||
}
|
||||
close $S2;
|
||||
} else {
|
||||
$session->errorHandler->warn("Unable to open $sub2 for directory reading");
|
||||
if ( opendir my $DH, $uDir ) {
|
||||
my @part1 = grep { !/^\.+$/ } readdir($DH);
|
||||
foreach my $subdir (@part1) {
|
||||
if ( opendir my $SD, "$uDir/$subdir" ) {
|
||||
my @part2 = grep { !/^\.+$/ } readdir($SD);
|
||||
foreach my $sub2 (@part2) {
|
||||
if ( opendir my $S2, "$uDir/$subdir/$sub2" ) {
|
||||
my @fileId = grep { !/^\.+$/ } readdir($S2);
|
||||
foreach my $fileId (@fileId) {
|
||||
my $store = WebGUI::Storage->get( $session, $session->id->fromHex($fileId) );
|
||||
$store->syncToCdn; # here is the meat
|
||||
}
|
||||
close $S2;
|
||||
}
|
||||
else {
|
||||
$session->errorHandler->warn("Unable to open $sub2 for directory reading");
|
||||
}
|
||||
}
|
||||
}
|
||||
close $SD;
|
||||
} else {
|
||||
$session->errorHandler->warn("Unable to open $subdir for directory reading");
|
||||
}
|
||||
}
|
||||
close $DH;
|
||||
} else {
|
||||
$session->errorHandler->warn("Unable to open $uDir for directory reading");
|
||||
close $SD;
|
||||
} ## end if ( opendir my $SD, "$uDir/$subdir")
|
||||
else {
|
||||
$session->errorHandler->warn("Unable to open $subdir for directory reading");
|
||||
}
|
||||
} ## end foreach my $subdir (@part1)
|
||||
close $DH;
|
||||
} ## end if ( opendir my $DH, $uDir)
|
||||
else {
|
||||
$session->errorHandler->warn("Unable to open $uDir for directory reading");
|
||||
}
|
||||
} # end syncUploads
|
||||
|
||||
} # end syncUploads
|
||||
|
||||
__DATA__
|
||||
This exists so flock() code above works.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue