Somewhat broken

master
Micke Nordin 8 years ago
parent 871ad44b1c
commit 4f21aabbbb

@ -34,12 +34,9 @@ my $oldactivesharedcronfile = "$sharedcrondir/$user.old";
my $oldpassivesharedcronfile = "$sharedcrondir/$user.passive.old"; my $oldpassivesharedcronfile = "$sharedcrondir/$user.passive.old";
my $electiondir = "$sharedcrondir/election"; my $electiondir = "$sharedcrondir/election";
my $hostname = hostname; my $hostname = hostname;
my $time = time; my $failovertimeout = 40;
print "Host: $hostname started run at: $time\n";
print "Mode: $mode, user: $user, shareddir: $shareddir and spooldir: $spooldir\n";
# Create directories # Create directories
unless (-d $sharedcrondir) { unless (-d $sharedcrondir) {
mkdir $sharedcrondir or die "Can not create shared directory: $!"; mkdir $sharedcrondir or die "Can not create shared directory: $!";
@ -78,6 +75,7 @@ sub get_nodes {
# Simply write a unix timestamp to a file with our name on it # Simply write a unix timestamp to a file with our name on it
sub write_timestamp { sub write_timestamp {
my $time = time;
open(FILE,"+>$electiondir/$hostname"); open(FILE,"+>$electiondir/$hostname");
print FILE $time; print FILE $time;
close FILE; close FILE;
@ -94,9 +92,9 @@ sub is_active {
# Loop all nodes # Loop all nodes
foreach my $node (@nodes) { foreach my $node (@nodes) {
# If it has been active in the last 120 seconds it is eligable # If it has been active in the last 120 seconds it is eligable
my $diff = $time - $node->{'time'}; my $diff = time - $node->{'time'};
if( $diff < 120 ) { if( $diff < $failovertimeout ) {
print "Node: $node->{'name'} was active last 120 sec\n"; print "Node: $node->{'name'} was active last $failovertimeout sec\n";
# Go to passive mode if the numeric of that host is less than that of this host # Go to passive mode if the numeric of that host is less than that of this host
my $this = get_numeric($hostname); my $this = get_numeric($hostname);
my $that = $node->{'numeric'}; my $that = $node->{'numeric'};
@ -129,11 +127,10 @@ sub get_numeric {
# But a # infront of any actime crontab entry # But a # infront of any actime crontab entry
sub comment_out { sub comment_out {
my ($infile, $outfile) = @_; my ($infile, $outfile) = @_;
print "Commenting out, infile is $infile and outfile is $outfile\n";
open(INFILE,"<$infile"); open(INFILE,"<$infile");
my $content = ''; my $content = '';
while(my $line = <INFILE>) { while(my $line = <INFILE>) {
$line =~ s/(^(?!#))/#$1/g; $line =~ s/(^(?!#))/# $1/g;
$content .= $line; $content .= $line;
} }
@ -147,11 +144,10 @@ sub comment_out {
# Remove a # from any crontab entry # Remove a # from any crontab entry
sub uncomment { sub uncomment {
my ($infile, $outfile) = @_; my ($infile, $outfile) = @_;
print "Uncommenting, infile is $infile and outfile is $outfile\n";
open(INFILE,"<$infile"); open(INFILE,"<$infile");
my $content = ''; my $content = '';
while(my $line = <INFILE>) { while(my $line = <INFILE>) {
$line =~ s/^(#\s?)([\d*])/$2/g; $line =~ s/^(#\s?)([\d*])/ $2/g;
$content .= $line; $content .= $line;
} }
@ -165,8 +161,6 @@ sub uncomment {
sub cron_compare { sub cron_compare {
my ($one, $two, $three) = @_; my ($one, $two, $three) = @_;
print "Comparing files\n";
# Current cronfile and shared cronfile is not same # Current cronfile and shared cronfile is not same
if(compare($one, $two) != 0) { if(compare($one, $two) != 0) {
@ -189,8 +183,13 @@ sub cron_compare {
} }
# If we are running in active/passive mode sub run {
if($mode == 0) { print "Host: $hostname started run at: " . time . "\n";
print "Mode: $mode, user: $user, shareddir: $shareddir and spooldir: $spooldir\n";
# If we are running in active/passive mode
if($mode == 0) {
unless (-d $electiondir) { unless (-d $electiondir) {
mkdir $electiondir; mkdir $electiondir;
} }
@ -216,8 +215,12 @@ if($mode == 0) {
# The other node has changed the file # The other node has changed the file
if($compare == 1) { if($compare == 1) {
uncomment($passivesharedcronfile, $cronfile); uncomment($passivesharedcronfile, $cronfile);
} elsif ($compare == 2) {
copy($passivesharedcronfile, $oldpassivesharedcronfile); copy($passivesharedcronfile, $oldpassivesharedcronfile);
copy($cronfile, $activesharedcronfile);
copy($activesharedcronfile, $oldactivesharedcronfile);
# This node has changed the file
} elsif ($compare == 2) {
copy($cronfile, $activesharedcronfile);
comment_out($cronfile, $passivesharedcronfile); comment_out($cronfile, $passivesharedcronfile);
} }
# We are passive node # We are passive node
@ -226,30 +229,40 @@ if($mode == 0) {
# The other node has changed the file # The other node has changed the file
if($compare == 1) { if($compare == 1) {
copy($passivesharedcronfile, $cronfile); copy($passivesharedcronfile, $cronfile);
copy($passivesharedcronfile, $oldpassivesharedcronfile);
# We have changed stuff # We have changed stuff
} elsif ($compare == 2) { } elsif ($compare == 2) {
copy($passivesharedcronfile, $oldpassivesharedcronfile);
comment_out($cronfile, $passivesharedcronfile); comment_out($cronfile, $passivesharedcronfile);
copy($passivesharedcronfile, $cronfile); copy($passivesharedcronfile, $cronfile);
} }
} }
unlink $tempfile; unlink $tempfile;
} }
# If we are running in active/active mode # If we are running in active/active mode
else { else {
my $compare = cron_compare($cronfile, $activesharedcronfile, $oldactivesharedcronfile ); my $compare = cron_compare($cronfile, $activesharedcronfile, $oldactivesharedcronfile );
# The other node has changed the file # The other node has changed the file
if($compare == 1) { if($compare == 1) {
copy($activesharedcronfile, $oldactivesharedcronfile);
copy($activesharedcronfile, $cronfile); copy($activesharedcronfile, $cronfile);
# We have changed things # We have changed things
} elsif ($compare == 2) { } elsif ($compare == 2) {
copy($activesharedcronfile, $oldactivesharedcronfile); copy($cronfile, $activesharedcronfile);
comment_out($cronfile, $activesharedcronfile); }
copy($activesharedcronfile, $cronfile);
} }
print "Host: $hostname finished run at: " . time . "\n\n\n";
} }
for (my $i = 0; $i < 6; $i++) {
my $start = time;
$| = 1;
run;
if ((my $remaining = 10 - (time - $start)) > 0) {
sleep $remaining;
}
}
exit; exit;

Loading…
Cancel
Save