Exemple #1
0
 /**
  * Если результат выполнения операции уже известен, возвращает его, иначе блокируется до получения ответа
  *
  * @return mixed Результат выполнения Future
  */
 public function resolve()
 {
     if (!$this->resolved) {
         $result = null;
         try {
             $response = new \Phasty\Server\Http\Response();
             $response->setReadStream($this->stream);
             $response->on("read-complete", function ($event, $response) use(&$result) {
                 $result = Result::processResponse($response);
             });
             $response->on("error", function ($event) {
                 throw new \Exception($event->getBody());
             });
             $timer = new Timer(Result::OPERATION_TIMEOUT_SECONDS, Result::OPERATION_TIMEOUT_MICROSECONDS, function () {
                 $this->stream->close();
                 throw new \Exception("Operation timed out");
             });
             $streamSet = new StreamSet();
             $streamSet->addReadStream($this->stream);
             $streamSet->addTimer($timer);
             $streamSet->listen();
         } catch (\Exception $e) {
             $result = $e;
         }
         $this->resolveWith($result);
     }
     return $this->value;
 }
Exemple #2
0
 public function __construct(Stream\Socket\Server $serverSocket, Stream\StreamSet $streamSet = null)
 {
     if (is_null($streamSet)) {
         $streamSet = Stream\StreamSet::instance();
     }
     $serverSocket->on("connected", [$this, "onNewConnection"]);
 }
Exemple #3
0
 public function __construct($job, array $requiredFiles = [], \Phasty\Stream\StreamSet $streamSet = null)
 {
     if (!is_subclass_of($job, "\\Phasty\\Process\\Child\\CallableClass")) {
         throw new \Exception("Job class must implement \\Phasty\\Process\\Child\\CallableClass");
     }
     if (!$streamSet) {
         $streamSet = \Phasty\Stream\StreamSet::instance();
     }
     $this->job = $job;
     $this->inStream = new \Phasty\Stream\Stream();
     $this->outStream = new \Phasty\Stream\Stream();
     $this->inStream->on("close", [$this, "onInStreamClose"]);
     $this->streamReader = new \Phasty\Events\StreamReader($this->inStream);
     $this->streamReader->addListener($this);
     if ($job instanceof \Phasty\Events\Eventable) {
         $this->on(null, $job);
     }
     $streamSet->addReadStream($this->inStream);
     $this->on("start", function () {
         $this->running = true;
     });
     $this->on("error", function () {
         $this->error = true;
     });
     $this->on("stop", [$this, "onStop"]);
     $this->requiredFiles = $requiredFiles;
 }
Exemple #4
0
 public function testWhenUnexcpectedChildDeath()
 {
     $streamSet = \Phasty\Stream\StreamSet::instance();
     $streamSet->addTimer(new \Phasty\Stream\Timer(10, 0, function () use($streamSet) {
         $streamSet->stop();
     }));
     $times = 300;
     $stopped = $errored = 0;
     $procs = [];
     for ($i = 0; $i < $times; $i++) {
         $procs[] = new Controller(new ChildProc());
         $procs[$i]->on("stop", function () use(&$stopped) {
             $stopped++;
         });
         $procs[$i]->on("error", function ($event) use(&$errored) {
             $errored++;
         });
         $procs[$i]->sleep();
         $streamSet->addTimer($timers[$i] = new \Phasty\Stream\Timer(0, 100, function ($event, $timer) use($procs, $i) {
             $procs[$i]->kill();
             $timer->cancel();
         }));
     }
     $streamSet->listen();
     $this->assertEquals($times, $stopped, "Process should be stopped, but it wasn't");
     $this->assertEquals($times, $errored, "Process should catch error, but it hasn't");
     for ($i = 1; $i <= $times; $i++) {
         $signaled = $procs[$i - 1]->isSignaled();
         $this->assertTrue($signaled, "Process [{$i}] was signaled, but says he wasn't");
     }
 }
Exemple #5
0
 /**
  * Send message to all pipes in directory, optionally using StreamSet
  *
  * @param string $fifoPath    Path to directory with pipes
  * @param mixed  $message     Message should be castable to string
  * @param mixed $useStreamSet If \Phasty\Stream\StreamSet use it for streams \
  *                            If false no need using streams \
  *                            If null use default StreamSet instance
  */
 public static function send($fifoPath, $message, $useStreamSet = false)
 {
     $fifos = glob("{$fifoPath}/*");
     $message = (string) $message;
     log::debug("Mx1 send to " . count($fifos) . " fifos");
     foreach ($fifos as $fifo) {
         if (!isset(self::$streams[$fifo])) {
             self::$streams[$fifo] = new \Phasty\Stream\Reader\NamedPipe($fifo);
             if ($useStreamSet !== false) {
                 if (is_null($useStreamSet)) {
                     self::$streams[$fifo]->setStreamSet(\Phasty\Stream\StreamSet::instance());
                 } else {
                     self::$streams[$fifo]->setStreamSet($useStreamSet);
                 }
             }
             self::$streams[$fifo]->on("close", function () use($fifo) {
                 unset(self::$streams[$fifo]);
             });
         }
         self::$streams[$fifo]->write($message);
     }
 }
Exemple #6
0
 public static function isWaitingForServices()
 {
     return \Phasty\Stream\StreamSet::instance()->getReadStreamsCount() > 0;
 }