Ejemplo n.º 1
0
 /**
  * Throws an error in the observable.
  *
  * @param \Throwable $reason
  */
 public function fail(\Throwable $reason)
 {
     if (null !== $this->started) {
         $this->started->reject(new CompletedError());
     }
     $this->delayed->reject($reason);
 }
Ejemplo n.º 2
0
 /**
  * Marks the observable as complete with the given error.
  *
  * @param \Throwable $exception
  */
 public function fail(\Throwable $exception)
 {
     if (null === $this->observable) {
         return;
     }
     $this->observable = null;
     $this->failed = true;
     $this->delayed->reject($exception);
 }
Ejemplo n.º 3
0
 /**
  * @throws \Icicle\Concurrent\Exception\ProcessException If starting the process fails.
  * @throws \Icicle\Concurrent\Exception\StatusError If the process is already running.
  */
 public function start()
 {
     if (null !== $this->delayed) {
         throw new StatusError('The process has already been started.');
     }
     $this->delayed = new Delayed();
     $fd = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'a'], ['pipe', 'w']];
     $nd = 0 === strncasecmp(PHP_OS, 'WIN', 3) ? 'NUL' : '/dev/null';
     $command = sprintf('(%s) 3>%s; code=$?; echo $code >&3; exit $code', $this->command, $nd);
     $this->process = proc_open($command, $fd, $pipes, $this->cwd ?: null, $this->env ?: null, $this->options);
     if (!is_resource($this->process)) {
         throw new ProcessException('Could not start process.');
     }
     $this->oid = getmypid();
     $status = proc_get_status($this->process);
     if (!$status) {
         proc_close($this->process);
         $this->process = null;
         throw new ProcessException('Could not get process status.');
     }
     $this->pid = $status['pid'];
     $this->stdin = new WritablePipe($pipes[0]);
     $this->stdout = new ReadablePipe($pipes[1]);
     $this->stderr = new ReadablePipe($pipes[2]);
     $stream = $pipes[3];
     stream_set_blocking($stream, 0);
     $this->poll = Loop\poll($stream, function ($resource) {
         if (!is_resource($resource) || feof($resource)) {
             $this->close($resource);
             $this->delayed->reject(new ProcessException('Process ended unexpectedly.'));
         } else {
             $code = fread($resource, 1);
             $this->close($resource);
             if (!strlen($code) || !is_numeric($code)) {
                 $this->delayed->reject(new ProcessException('Process ended without providing a status code.'));
             } else {
                 $this->delayed->resolve((int) $code);
             }
         }
         $this->poll->free();
     });
 }
Ejemplo n.º 4
0
 /**
  * Throws an error in the observable.
  *
  * @param \Throwable $reason
  */
 public function fail(\Throwable $reason)
 {
     $this->delayed->reject($reason);
 }