X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;ds=sidebyside;f=src%2FShm.pl;h=6811ff458e26a91a8ef491eb258aaf1819e8385d;hb=c6ea16a8cf7e3bc0e1ef68d6f795520e6db4aebe;hp=27b95198e02ba6f2ef39fd92ca5440db7c4d9abf;hpb=8934852e63f594aeb55619214087e9541fc83375;p=infobot.git diff --git a/src/Shm.pl b/src/Shm.pl index 27b9519..6811ff4 100644 --- a/src/Shm.pl +++ b/src/Shm.pl @@ -44,6 +44,8 @@ sub shmRead { return $retval; } else { &ERROR("shmRead: failed: $!"); + ### TODO: if this fails, never try again. + &openSHM(); return ''; } } @@ -58,7 +60,14 @@ sub shmWrite { if ($str !~ /^$/) { my $read = &shmRead($key); $read =~ s/\0+//g; - $str = $read ."||". $str if ($read ne ""); + + if ($str eq "") { + $str = time().": "; # time stamping, null. + } elsif ($read eq "") { + $str = time().": "; # timestamping. + } else { + $str = $read ."||". $str; + } } if (!shmwrite($key,$str,$position,$size)) { @@ -92,7 +101,7 @@ sub addForked { } my $count = 0; - while (scalar keys %forked > 2) { # 2 or more == fail. + while (scalar keys %forked > 1) { # 2 or more == fail. sleep 1; if ($count > 3) { # 3 seconds. @@ -108,28 +117,38 @@ sub addForked { $count++; } - if (exists $forked{$name}) { - my $time = $forked{$name}{Time}; + if (exists $forked{$name} and !scalar keys %{ $forked{$name} }) { + &WARN("addF: forked{$name} exists but is empty; deleting."); + undef $forked{$name}; + } + + if (exists $forked{$name} and scalar keys %{ $forked{$name} }) { + my $time = $forked{$name}{Time}; + my $continue = 0; + + &DEBUG("PID => $forked{$name}{PID}"); + if (-d "/proc/$forked{$name}{PID}") { &status("fork: still running; good. BAIL OUT."); } else { - &status("fork: lost the fork? REMOVE IT!"); + &WARN("Found dead fork; removing and resetting."); + $continue = 1; } - if (time() - $time > 900) { # stale fork > 15m. + if ($continue) { + # NOTHING. + } elsif (time() - $time > 900) { # stale fork > 15m. &status("forked: forked{$name} presumably exited without notifying us."); - $forked{$name}{Time} = time(); - return 1; } else { # fresh fork. - &msg($who, "$name is already running ". &Time2String(time() - $forked{$name})); + &msg($who, "$name is already running ". &Time2String(time() - $time)); return 0; } - } else { - $forked{$name}{Time} = time(); - $forkedtime = time(); - $count{'Fork'}++; - return 1; } + + $forked{$name}{Time} = time(); + $forkedtime = time(); + $count{'Fork'}++; + return 1; } sub delForked { @@ -142,6 +161,10 @@ sub delForked { POSIX::_exit(0); } + if ($name =~ /\.pl/) { + &WARN("dF: name is name of source file ($name). FIX IT!"); + } + &showProc(); # just for informational purposes. if (exists $forked{$name}) { @@ -152,7 +175,7 @@ sub delForked { &ERROR("delForked: forked{$name} does not exist. should not happen."); } - &status("fork finished for '$name'."); + &status("--- fork finished for '$name' ---"); POSIX::_exit(0); }