Ejemplo n.º 1
0
 /**
  * Called on Construct or Init
  * @return void
  */
 public function setup()
 {
     $this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
     if (!socket_bind($this->socket, $this->ip, $this->port)) {
         $errno = socket_last_error();
         $this->error(sprintf('Could not bind to address %s:%s [%s] %s', $this->ip, $this->port, $errno, socket_strerror($errno)));
         throw new Exception('Could not start server.');
     }
     socket_listen($this->socket);
     $this->daemon->on(Core_Daemon::ON_POSTEXECUTE, array($this, 'run'));
 }
Ejemplo n.º 2
0
 public function setup()
 {
     // This class implements both the Task and the Plugin interfaces. Like plugins, this setup() method will be
     // called in the parent process during application init. And like tasks, this setup() method will be called right
     // after the process is forked.
     $that = $this;
     if (Core_Daemon::is('parent')) {
         // Use the ftok() method to create a deterministic memory address.
         // This is a bit ugly but ftok needs a filesystem path so we give it one using the daemon filename and
         // current worker alias.
         $tmp = sys_get_temp_dir();
         $ftok = sprintf($tmp . '/%s_%s', str_replace('/', '_', $this->daemon->get('filename')), $this->alias);
         if (!touch($ftok)) {
             $this->fatal_error("Unable to create Worker ID. ftok() failed. Could not write to {$tmp} directory at {$ftok}");
         }
         $this->guid = ftok($ftok, $this->alias[0]);
         @unlink($ftok);
         if ($this->guid == -1) {
             $this->fatal_error("Unable to create Worker ID. ftok() failed. Unexpected return value: {$this->guid}");
         }
         $this->via->setup();
         $this->via->purge();
         if ($this->daemon->get('debug_workers')) {
             $this->debug();
         }
         $this->daemon->on(Core_Daemon::ON_PREEXECUTE, array($this, 'run'));
         $this->daemon->on(Core_Daemon::ON_IDLE, array($this, 'garbage_collector'), ceil(120 / ($this->workers * 0.5)));
         // Throttle the garbage collector
         $this->daemon->on(Core_Daemon::ON_SIGNAL, array($this, 'dump'), null, function ($args) {
             return $args[0] == SIGUSR1;
         });
         $this->fork();
     } else {
         unset($this->calls, $this->running_calls, $this->on_return, $this->on_timeout, $this->call_count);
         $this->calls = $this->call_count = $this->running_calls = array();
         $this->via->setup();
         $event_restart = function () use($that) {
             $that->log('Restarting Worker Process...');
         };
         $this->daemon->on(Core_Daemon::ON_SIGNAL, $event_restart, null, function ($args) {
             return $args[0] == SIGUSR1;
         });
         call_user_func($this->get_callback('setup'));
         $this->log('Worker Process Started');
     }
 }
Ejemplo n.º 3
0
 /**
  * Called on Construct or Init
  * @return void
  */
 public function setup()
 {
     $this->daemon->on(Core_Daemon::ON_IDLE, array($this, 'reap'), 30);
 }