summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNiko Tyni <ntyni@iki.fi>2008-03-18 07:46:15 +0100
committerNiko Tyni <ntyni@iki.fi>2008-03-18 07:46:15 +0100
commitefe9d8dafd6a8bc5845eae3b4dbc172ad68aa187 (patch)
tree42b27a2462448ac8cce55ac76389735ff0cb5728 /lib
parent6b3f4223b367af243c978815ac46cb344a2a0777 (diff)
downloadsmokeping-efe9d8dafd6a8bc5845eae3b4dbc172ad68aa187.tar.gz
smokeping-efe9d8dafd6a8bc5845eae3b4dbc172ad68aa187.tar.xz
revert the locking change in r951 for portability reasons
Diffstat (limited to 'lib')
-rw-r--r--lib/Smokeping/Master.pm32
1 files changed, 13 insertions, 19 deletions
diff --git a/lib/Smokeping/Master.pm b/lib/Smokeping/Master.pm
index 81ce01f..6fbdcbf 100644
--- a/lib/Smokeping/Master.pm
+++ b/lib/Smokeping/Master.pm
@@ -1,7 +1,7 @@
# -*- perl -*-
package Smokeping::Master;
use Data::Dumper;
-use Storable qw(nstore_fd dclone fd_retrieve);
+use Storable qw(nstore dclone retrieve);
use strict;
use warnings;
use Fcntl qw(:flock);
@@ -115,16 +115,13 @@ sub save_updates {
warn "Skipping update for ${name}.${slave}.slave_cache since ".
"you seem to try todo some directory magic here. Don't!";
}
- else {
+ elsif ( open (my $lock, '>>' , "$file.lock") ) {
for (my $i = 3; $i > 0; $i--){
- my $fh;
- if ( open ($fh, '+>>' , $file) and flock($fh, LOCK_EX) ){
+ if ( flock($lock, LOCK_EX) ){
my $existing = [];
- next if ! -e $file; # the reader unlinked it from under us
- seek $fh, 0, 0;
- if ( -s _ ){
- my $in = eval { fd_retrieve $fh };
+ if ( -r $file ){
+ my $in = eval { retrieve $file };
if ($@) { #error
warn "Loading $file: $@";
} else {
@@ -132,17 +129,16 @@ sub save_updates {
};
};
push @{$existing}, [ $slave, $time, $updatestring ];
- nstore_fd($existing, $fh);
- flock($fh, LOCK_UN);
- close $fh;
+ nstore($existing, $file);
last;
} else {
warn "Could not lock $file ($!). Trying again for $i rounds.\n";
sleep rand(2);
}
- warn "Could not update $file, giving up for now.";
- close $fh;
}
+ close $lock;
+ } else {
+ warn "Could not update $file: $!";
}
}
};
@@ -165,12 +161,10 @@ sub get_slaveupdates {
my $dir = slavedatadir($cfg);
$file =~ s/^\Q$datadir\E/$dir/;
- my $fh;
- if ( open ($fh, '<', $file) ) {
- if ( flock $fh, LOCK_SH ){
- eval { $data = fd_retrieve $fh };
+ if ( -r $file and open (my $lock, '>>', "$file.lock") ) {
+ if ( flock $lock, LOCK_EX ){
+ eval { $data = retrieve $file };
unlink $file;
- flock $fh, LOCK_UN;
if ($@) { #error
warn "Loading $file: $@";
return $empty;
@@ -178,7 +172,7 @@ sub get_slaveupdates {
} else {
warn "Could not lock $file. Will skip and try again in the next round. No harm done!\n";
}
- close $fh;
+ close $lock;
return $data;
}
return $empty;