Exemple #1
0
 /**
  * Tail -f for stdout or stderr for a process.
  *
  * @param Response   $response
  * @param Supervisor $instance
  * @param string     $process
  * @param string     $type
  *
  * @throws Exception
  */
 private function getLog($response, $instance, $process, $type = 'Stdout')
 {
     $template = $this->app['twig']->loadTemplate(sprintf('layout/tail.twig'));
     $response->setContent($template->render(['doNotClose' => true, 'baseUrl' => $this->app->getConfig('dir', '/')]))->sendContent();
     //flush template to browser.  Tmeplate is setup to use bigpipe so the connection doesn't close.
     ob_flush();
     flush();
     if ($type != 'Stdout' && $type != 'Stderr') {
         throw new Exception('Log type not supported');
     }
     $offset = -4096;
     while (true) {
         // Check if client is still connected.
         if (connection_aborted()) {
             return;
         }
         usleep(300000);
         //0.3 s
         //Get log until no more from last offset.
         $bufferOverFlow = true;
         while ($bufferOverFlow) {
             list($string, $newoffset, $bufferOverFlow) = $instance->{'tailProcess' . $type . 'Log'}($process, $offset, 4096);
             if (strlen($string) > 0) {
                 if ($offset > 0) {
                     $template = $this->app['twig']->loadTemplate(sprintf('layout/tailContent.twig'));
                     // Set content with <br/> for new lines and only send in offset difference.
                     // This is becuase tailProcessStdoutLog returns 4096 no matter what the offset is.
                     $twigVariables = ['string' => nl2br(substr($string, $offset - $newoffset))];
                     $response->setContent($template->render($twigVariables))->sendContent();
                 } else {
                     $template = $this->app['twig']->loadTemplate(sprintf('layout/tailContent.twig'));
                     $twigVariables = ['string' => nl2br($string)];
                     $response->setContent($template->render($twigVariables))->sendContent();
                 }
                 // Flush to browser.
                 ob_flush();
                 flush();
             }
             $offset = $newoffset;
         }
     }
 }