Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
authorDan
Fri, 15 Aug 2008 23:22:30 -0400
changeset 32 86140ed9087d
parent 27 20a36fe254c9
child 33 3b4aef1efff6
Experimental: upon change to playlist, send SIGUSR1 to parent process and branch out to all children to force a playlist refresh
functions.php
greyhound.php
webserver.php
--- a/functions.php	Tue Aug 05 13:17:57 2008 -0400
+++ b/functions.php	Fri Aug 15 23:22:30 2008 -0400
@@ -134,6 +134,12 @@
       );
     $playlist[] = $item;
   }
+  // if we're a child process, signal the parent to update
+  if ( defined('HTTPD_WS_CHILD') )
+  {
+    global $httpd;
+    posix_kill($httpd->parent_pid, SIGUSR1);
+  }
 }
 
 /**
--- a/greyhound.php	Tue Aug 05 13:17:57 2008 -0400
+++ b/greyhound.php	Fri Aug 15 23:22:30 2008 -0400
@@ -23,6 +23,7 @@
   // trap SIGTERM
   pcntl_signal(SIGTERM, 'sigterm');
   pcntl_signal(SIGINT,  'sigterm');
+  pcntl_signal(SIGUSR1, 'handle_refresh_signal');
 }
 
 //
@@ -154,3 +155,23 @@
 {
   burnout("Exception caught while running webserver:\n$e");
 }
+
+function handle_refresh_signal()
+{
+  global $httpd;
+  if ( !is_object($httpd) )
+    // we're not serving yet.
+    return false;
+    
+  // we've got an httpd instance; rebuild the playlist
+  rebuild_playlist();
+  
+  // if this is the parent, also ask the children to rebuild.
+  if ( !defined('HTTPD_WS_CHILD') )
+  {
+    foreach ( $httpd->child_list as $pid )
+    {
+      posix_kill($pid, SIGUSR1);
+    }
+  }
+}
--- a/webserver.php	Tue Aug 05 13:17:57 2008 -0400
+++ b/webserver.php	Fri Aug 15 23:22:30 2008 -0400
@@ -160,6 +160,20 @@
   var $socket_initted = false;
   
   /**
+   * The list of child processes spawned by this server.
+   * @var array
+   */
+  
+  var $child_list = array();
+  
+  /**
+   * The parent process's PID
+   * @var int
+   */
+  
+  var $parent_pid = 0;
+  
+  /**
    * Constructor.
    * @param string IPv4 address to bind to
    * @param int Port number
@@ -254,6 +268,7 @@
     
     $this->bind_address = $address;
     $this->server_string = "PhpHttpd/" . HTTPD_VERSION . " PHP/" . PHP_VERSION . "\r\n";
+    $this->parent_pid = getmypid();
     
     // create a UUID
     $uuid_base = md5(microtime() . ( function_exists('mt_rand') ? mt_rand() : rand() ));
@@ -337,6 +352,7 @@
         {
           // we are the parent, continue listening
           socket_close($remote);
+          $this->child_list[] = $pid;
           continue;
         }
         else
@@ -357,11 +373,7 @@
       $client_headers = '';
       if ( defined('HTTPD_WS_CHILD') )
       {
-        if ( !@socket_set_timeout($remote, HTTPD_KEEP_ALIVE_TIMEOUT) )
-        {
-          status('stream_set_timeout() on $remote failed.');
-          var_dump($remote);
-        }
+        @socket_set_timeout($remote, HTTPD_KEEP_ALIVE_TIMEOUT);
       }
       if ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) )
       {