]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Shm.pl
* Fixed a bug that caused seen stats flush to crash related to sqlite not having...
[infobot.git] / src / Shm.pl
index 0c3116bedd9c065e7d13a071a08d586d3e00496e..0484ff2e2c34e2f2a2021443bf4d2a6e8e3f714e 100644 (file)
@@ -9,17 +9,23 @@
 
 use POSIX qw(_exit);
 
+my %shm_keys;
+
 sub openSHM {
     my $IPC_PRIVATE = 0;
     my $size = 2000;
 
-    if (&IsParam("noSHM")) {
+    if (&IsParam('noSHM')) {
        &status("Shared memory: Disabled. WARNING: bot may become unreliable");
        return 0;
     }
 
     if (defined( $_ = shmget($IPC_PRIVATE, $size, 0777) )) {
        &status("Created shared memory (shm) key: [$_]");
+       $shm_keys{$_} = {time     => time,
+                        accessed => 0,
+                        key      => $_,
+                       };
        return $_;
     } else {
        &ERROR("openSHM: failed.");
@@ -45,15 +51,19 @@ sub shmRead {
     my $size = 3*80;
     my $retval = '';
 
-    return '' if (&IsParam("noSHM"));
+    return '' if (&IsParam('noSHM'));
 
     if (shmread($key,$retval,$position,$size)) {
-       &DEBUG("shmRead($key): $retval");
+       #&DEBUG("shmRead($key): $retval");
        return $retval;
     } else {
        &ERROR("shmRead: failed: $!");
+       if (exists $shm_keys{$_}) {
+             closeSHM($key);
+       }
        ### TODO: if this fails, never try again.
-       &openSHM();
+       # What use is opening a SHM segment if we're not going to read it?
+       # &openSHM();
        return '';
     }
 }
@@ -63,7 +73,9 @@ sub shmWrite {
     my $position = 0;
     my $size = 80*3;
 
-    return if (&IsParam("noSHM"));
+    return if (&IsParam('noSHM'));
+
+    $shm_keys{$keys}{accessed} = 1;
 
     if (length($str) > $size) {
        &status("ERROR: length(str) (..)>$size...");
@@ -81,8 +93,8 @@ sub shmWrite {
 
     my $read = &shmRead($key);
     $read =~ s/\0+//g;
-    if ($read eq "") {
-       $str = sprintf("%s:%d:%d: ", $param{ircNick}, $bot_pid, time());
+    if ($read eq '') {
+       $str = sprintf("%s:%d:%d: ", $param{ircUser}, $bot_pid, time());
     } else {
        $str = $read ."||". $str;
     }
@@ -229,13 +241,14 @@ sub shmFlush {
     return if ($$ != $::bot_pid); # fork protection.
 
     if (@_) {
-       &ScheduleThis(5, "shmFlush");
-       return if ($_[0] eq "2");
+       &ScheduleThis(15*60, 'shmFlush'); # 15 minutes
+       return if ($_[0] eq '2');
     }
 
     my $time;
     my $shmmsg = &shmRead($shm);
-    $shmmsg =~ s/\0//g;         # remove padded \0's.
+    # remove padded \0's.
+    $shmmsg =~ s/\0//g;
     return if (length($shmmsg) == 0);
     if ($shmmsg =~ s/^(\S+):(\d+):(\d+): //) {
        my $n   = $1;
@@ -271,7 +284,9 @@ sub shmFlush {
        }
     }
 
-    &shmWrite($shm,"") if ($shmmsg ne "");
+    &shmWrite($shm,'') if ($shmmsg ne '');
 }
 
 1;
+
+# vim:ts=4:sw=4:expandtab:tw=80