Esempio n. 1
0
 /**
  * Creates a log (output).
  *
  * @param  string $event
  * @param  Control $control
  * @param  boolean $output
  * @return void
  */
 protected function log($event, Control $control, $output)
 {
     $record = array('date' => date('Y-m-d H:i:s'), 'pid' => $control->info()->getId(), 'actionId' => $this->actionId, 'event' => $event);
     $this->records[] = $record;
     if (!$output) {
         return;
     }
     $message = sprintf('[%s] PID: %d, Id: %d, Event: %s', $record['date'], $record['pid'], $record['actionId'], $record['event']);
     echo $message . PHP_EOL;
 }
Esempio n. 2
0
 /**
  * Initializes pidfile.
  *
  * Create an empty file, store the PID into the file and lock it.
  */
 public function initialize()
 {
     if ($this->isActive()) {
         throw new RuntimeException('Process is already active');
     }
     $handle = $this->getFileResource();
     if (!@flock($handle, LOCK_EX | LOCK_NB)) {
         throw new RuntimeException('Could not lock pidfile');
     }
     if (-1 === @fseek($handle, 0)) {
         throw new RuntimeException('Could not seek pidfile cursor');
     }
     if (!@ftruncate($handle, 0)) {
         throw new RuntimeException('Could not truncate pidfile');
     }
     if (!@fwrite($handle, $this->control->info()->getId() . PHP_EOL)) {
         throw new RuntimeException('Could not write on pidfile');
     }
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function start()
 {
     if ($this->hasId() && $this->isRunning()) {
         throw new RuntimeException('Process already started');
     }
     $this->context->processId = $this->control->fork();
     $this->context->isRunning = true;
     if ($this->context->processId > 0) {
         $this->action->trigger(Action::EVENT_FORK, $this->control, $this->context);
         return;
     }
     $this->context->processId = $this->control->info()->getId();
     $this->context->startTime = time();
     $this->setHandlerAlarm();
     $this->setHandlerErrorException();
     $this->run();
     restore_error_handler();
     $this->control->quit($this->context->exitCode);
 }
Esempio n. 4
0
 public function testShouldReturnAValidInfoController()
 {
     $control = new Control();
     $this->assertInstanceOf(__NAMESPACE__ . '\\Control\\Info', $control->info());
 }
Esempio n. 5
0
<?php

declare (ticks=1);
require_once __DIR__ . '/../vendor/autoload.php';
use Arara\Process\Action\Callback;
use Arara\Process\Child;
use Arara\Process\Control;
$control = new Control();
$child = new Child(new Callback(function (Control $control) {
    echo 'Child process is ' . $control->info()->getId() . PHP_EOL;
    $control->flush(1);
}), $control);
echo 'Parent process is ' . $control->info()->getId() . PHP_EOL;
$child->start();
if ($child->getStatus()->isSuccessful()) {
    echo 'Child successfully finished' . PHP_EOL;
}
Esempio n. 6
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();
 }