From 03ac6162f1261e54d01d2b40bdcd8fe7a048de5e Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Sun, 8 Jun 2008 18:09:07 +0000 Subject: * refactored locking file to make use of ScopedResource git-svn-id: http://svn.openslx.org/svn/openslx/openslx/trunk@1867 95ad53e4-c205-0410-b2fa-d234c58c8868 --- config-db/slxconfig-demuxer | 90 +++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 49 deletions(-) (limited to 'config-db') diff --git a/config-db/slxconfig-demuxer b/config-db/slxconfig-demuxer index a14ffc8d..c435d7d4 100755 --- a/config-db/slxconfig-demuxer +++ b/config-db/slxconfig-demuxer @@ -51,6 +51,7 @@ use OpenSLX::Basics; use OpenSLX::ConfigDB qw(:support); use OpenSLX::ConfigFolder; use OpenSLX::MakeInitRamFS::Engine; +use OpenSLX::ScopedResource; use OpenSLX::Utils; my ( @@ -112,13 +113,46 @@ if (createConfigFolderForDefaultSystem()) { chomp(my $slxVersion = qx{slxversion}); -my $lockFile = "$openslxConfig{'private-path'}/config-demuxer.lock"; -my $haveLock = lockScript($lockFile); - -END -{ - unlockScript($lockFile) if $haveLock; -} +# protect against parallel executions of this script +my $criticalSection = OpenSLX::ScopedResource->new({ + name => 'osplugin::session', + acquire => sub { + return if $option{dryRun}; + + # use a lock-file to singularize execution of this script: + my $lockFile = "$openslxConfig{'private-path'}/config-demuxer.lock"; + if (-e $lockFile) { + my $ctime = (stat($lockFile))[10]; + my $now = time(); + if ($now - $ctime > 15 * 60) { + # existing lock file is older than 15 minutes, wipe it: + unlink $lockFile; + } + } + if (!sysopen(LOCKFILE, $lockFile, O_RDWR | O_CREAT | O_EXCL)) { + if ($! == 13) { + die _tr(qq[Unable to create lock-file <%s>, exiting!\n], $lockFile); + } else { + die _tr(unshiftHereDoc(<<" End-of-Here"), + Lock-file '%s' exists, script is already running. + Please remove the logfile and try again if you are sure that no one else + is executing this script. + End-of-Here + $lockFile + ); + } + } + 1 + }, + release => sub { + return if $option{dryRun}; + + my $lockFile = "$openslxConfig{'private-path'}/config-demuxer.lock"; + close(LOCKFILE); + unlink $lockFile; + 1 + }, +}); my $tempPath = "$openslxConfig{'temp-path'}/slxconfig-demuxer"; if (!$option{dryRun}) { @@ -197,47 +231,6 @@ exit; ################################################################################ ### ################################################################################ -sub lockScript -{ - my $lockFile = shift; - - return if $option{dryRun}; - - # use a lock-file to singularize execution of this script: - if (-e $lockFile) { - my $ctime = (stat($lockFile))[10]; - my $now = time(); - if ($now - $ctime > 15 * 60) { - # existing lock file is older than 15 minutes, wipe it: - unlink $lockFile; - } - } - if (!sysopen(LOCKFILE, $lockFile, O_RDWR | O_CREAT | O_EXCL)) { - if ($! == 13) { - die _tr(qq[Unable to create lock-file <%s>, exiting!\n], $lockFile); - } else { - die _tr( - qq[Lock-file <%s> exists, script is already running. -Please remove the logfile and try again if you are sure that no one else -is executing this script.\n], $lockFile - ); - } - } - return 1; -} - -sub unlockScript -{ - my $lockFile = shift; - - return if $option{dryRun}; - - close(LOCKFILE); - unlink $lockFile; - - return; -} - sub folderContainsFiles { my $folder = shift; @@ -898,4 +891,3 @@ Please refer to the C-manpage for a more detailed description of these options. =cut - -- cgit v1.2.3-55-g7522