summaryrefslogtreecommitdiffstats
path: root/lib/Smokeping/Master.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Smokeping/Master.pm')
-rw-r--r--lib/Smokeping/Master.pm39
1 files changed, 17 insertions, 22 deletions
diff --git a/lib/Smokeping/Master.pm b/lib/Smokeping/Master.pm
index e54a264..4a5cd90 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);
@@ -105,30 +105,27 @@ sub save_updates {
" in the local data structure. Make sure you run the ".
"smokeping daemon. ($cfg->{General}{datadir})\n";
}
- elsif ( open (my $hand, '+>>' , $file) ) {
- for (my $i = 10; $i < 0; $i--){
- if ( flock $hand, LOCK_EX ){
+ elsif ( open (my $lock, '>>' , "$file.lock") ) {
+ for (my $i = 10; $i > 0; $i--){
+ if ( flock $lock, LOCK_EX ){
my $existing = [];
- if ( (stat($hand))[7] > 0 ){
- seek $hand, 0,0;
- eval { $existing = fd_retrieve $hand };
+ if ( -r $file ){
+ my $in = eval { retrieve $file };
if ($@) { #error
warn "Loading $file: $@";
- $existing = [];
- }
+ } else {
+ $existing = $in;
+ };
};
push @{$existing}, [ $slave, $time, $updatestring];
- seek $hand, 0,0;
- truncate $hand, 0;
- nstore_fd ($existing, $hand);
- flock $hand, LOCK_UN;
+ nstore($existing, $file);
last;
} else {
warn "Could not lock $file. Trying again for $i rounds.\n";
sleep rand(3);
}
}
- close $hand;
+ close $lock;
} else {
warn "Could not update $file: $!";
}
@@ -145,20 +142,18 @@ sub get_slaveupdates {
my $name = shift;
my $file = $name.".slave_cache";
my $data;
- if ( open (my $hand, '+<', $file) ) {
- if ( flock $hand, LOCK_EX ){
- rename $file,$file.$$;
- eval { $data = fd_retrieve $hand };
- unlink $file.$$;
- flock $hand, LOCK_UN;
+ if ( -r $file and open (my $lock, '>>', "$file.lock") ) {
+ if ( flock $lock, LOCK_EX ){
+ eval { $data = retrieve $file };
+ unlink $file;
if ($@) { #error
warn "Loading $file: $@";
- return;
+ return undef;
}
} else {
warn "Could not lock $file. Will skip and try again in the next round. No harm done!\n";
}
- close $hand;
+ close $lock;
return $data;
}
return;