Esempio n. 1
0
 /**
  * Returns TRUE when pidfile is active or FALSE when is not.
  *
  * @return bool
  */
 public function isActive()
 {
     $pid = $this->getProcessId();
     if (null === $pid) {
         return false;
     }
     return $this->control->signal()->send(0, $pid);
 }
Esempio n. 2
0
 public function testShouldDefineSignalHandlersByDefault()
 {
     $actualCount = 0;
     $expectedCount = 4;
     $this->overwrite('pcntl_signal', function () use(&$actualCount) {
         $actualCount++;
         return true;
     });
     $control = new Control();
     $control->signal();
     $this->assertEquals($expectedCount, $actualCount);
 }
Esempio n. 3
0
 /**
  * Define the timeout handler.
  */
 protected function setHandlerAlarm()
 {
     $handler = new SignalAlarm($this->control, $this->action, $this->context);
     $this->control->signal()->setHandler('alarm', $handler);
     $this->control->signal()->alarm($this->context->timeout);
 }
Esempio n. 4
0
 /**
  * Default trigger for EVENT_START.
  *
  * - Activates the circular reference collector
  * - Detach session
  * - Reset umask
  * - Update work directory
  * - Close file descriptors
  * - Define process owner, if any
  * - Define process group, if any
  * - Create new file descriptors
  * - Create pidfile
  * - Define pidfile cleanup
  *
  * @param Control $control
  * @param Context $context
  */
 public function handleStart(Control $control, Context $context)
 {
     if (!$context->pidfile instanceof Pidfile) {
         throw new LogicException('Pidfile is not defined');
     }
     // Activates the circular reference collector
     gc_enable();
     // Callback for handle when process is terminated
     $control->signal()->prependHandler(SIGTERM, function () use($context) {
         $this->setAsDying();
         $context->pidfile->finalize();
     });
     $control->signal()->setHandler(SIGTSTP, SIG_IGN);
     $control->signal()->setHandler(SIGTTOU, SIG_IGN);
     $control->signal()->setHandler(SIGTTIN, SIG_IGN);
     $control->signal()->setHandler(SIGHUP, SIG_IGN);
     // Detach session
     $control->info()->detachSession();
     // Reset umask
     @umask($this->getOption('umask'));
     // Update work directory
     @chdir($this->getOption('work_dir'));
     // Close file descriptors
     fclose(STDIN);
     fclose(STDOUT);
     fclose(STDERR);
     // Define process owner
     if (null !== ($userId = $this->getOption('user_id'))) {
         $control->info()->setUserId($userId);
     }
     // Define process group
     if (null !== ($groupId = $this->getOption('group_id'))) {
         $control->info()->setGroupId($groupId);
     }
     // Create new file descriptors
     $context->stdin = fopen($this->getOption('stdin'), 'r');
     $context->stdout = fopen($this->getOption('stdout'), 'wb');
     $context->stderr = fopen($this->getOption('stderr'), 'wb');
     // Create pidfile
     $context->pidfile->initialize();
 }