Example #1
0
 /**
  * @param $name
  * @param $command
  */
 public function childProcess($name, $command)
 {
     $process = new \React\ChildProcess\Process($command);
     $process->on('exit', function ($exitCode, $termSignal) {
         // ...
         $this->message($exitCode, 'exit');
         $this->message($termSignal, 'exit');
     });
     if (!$this->color) {
         $tmp = \Peanut\Console\Color::$foregroundColors;
         unset($tmp['black'], $tmp['default'], $tmp['white'], $tmp['light_gray']);
         $this->color = array_keys($tmp);
     }
     $color = array_shift($this->color);
     $this->loop->addTimer(0.001, function ($timer) use($process, $name, $color) {
         $process->start($timer->getLoop());
         $callback = function ($output) use($name, $color) {
             $lines = explode(PHP_EOL, $output);
             $i = 0;
             foreach ($lines as $line) {
                 if ($line) {
                     $tmp = '';
                     if ($name) {
                         $tmp .= \Peanut\Console\Color::text(str_pad($name, 16, ' ', STR_PAD_RIGHT) . ' | ', $color);
                     }
                     $tmp .= \Peanut\Console\Color::text($line, 'light_gray');
                     $this->message($tmp);
                 }
                 $i++;
             }
         };
         $process->stdout->on('data', $callback);
         $process->stderr->on('data', $callback);
     });
 }
Example #2
0
 public function startHead($controller, $headType, $headId, $restarting)
 {
     if (!$restarting) {
         // Console::output(Console::ansiFormat('Starting ' . $headId, [Console::FG_CYAN]));
     } else {
         // Console::output(Console::ansiFormat('Restarting ' . $headId, [Console::FG_CYAN]));
     }
     $_this = $this;
     $process = new \React\ChildProcess\Process($this->getSubCommand($controller, [$headType, $headId]));
     $process->on('exit', function ($exitCode, $termSignal) use(&$_this, &$controller, $headType, $headId) {
         if ($exitCode !== 0) {
             Console::stderr(Console::ansiFormat("Broadcast head {$headType}:{$headId} exited with error code {$exitCode}", [Console::FG_RED]));
             sleep(10);
         }
         if (static::isPaused()) {
             Yii::$app->end(0);
         }
         $_this->_heads[$headId] = $_this->startHead($controller, $headType, $headId, true);
     });
     $this->loop->addTimer(0.0001, function ($timer) use($process, &$_this) {
         $process->start($timer->getLoop());
         $process->stdout->on('data', function ($output) use($_this) {
             $stdout = fopen('php://stdout', 'w+');
             fwrite($stdout, $output);
         });
         $process->stderr->on('data', function ($output) use($_this) {
             $stderr = fopen('php://stderr', 'w+');
             fwrite($stderr, $output);
         });
     });
     sleep(5);
     return $process;
 }
 public function getUsers($loop, $limit, $offset)
 {
     $process = new \React\ChildProcess\Process('php artisan child:react ' . $limit . ' ' . $offset);
     $data = '';
     $process->on('exit', function ($exitCode, $termSignal) use(&$data) {
         $this->users = array_merge($this->users, unserialize($data));
     });
     $loop->addTimer(0.001, function ($timer) use($process, &$data) {
         $process->start($timer->getLoop());
         $process->stdout->on('data', function ($output) use(&$data) {
             $data .= $output;
         });
     });
 }
Example #4
0
 public function handle(array $arguments)
 {
     $this->parseArguments($arguments);
     $start = microtime(true);
     $loop = \React\EventLoop\Factory::create();
     $process = new \React\ChildProcess\Process($this->getCLICommand());
     $message = '';
     $request =& $this;
     $teleport = \Teleport\Teleport::instance();
     $process->on('exit', function ($exitCode, $termSignal) use($teleport, $request, $start, &$message) {
         $request->results = explode(PHP_EOL, rtrim($message, PHP_EOL));
         if ($request->args('debug') || $request->args('verbose')) {
             array_push($request->results, sprintf("request finished with exit code {$exitCode} in %2.4f seconds" . PHP_EOL, microtime(true) - $start));
         }
         if ($teleport->getConfig()->get('verbose', null, false) || $teleport->getConfig()->get('debug', null, false)) {
             echo sprintf("process finished with exit code {$exitCode} in %2.4f seconds" . PHP_EOL, microtime(true) - $start);
         }
     });
     $loop->addTimer(0.001, function ($timer) use($teleport, $request, $process, &$message) {
         if ($teleport->getConfig()->get('verbose', null, false) || $teleport->getConfig()->get('debug', null, false)) {
             echo "process started using cmd: {$process->getCommand()}" . PHP_EOL;
         }
         $process->start($timer->getLoop());
         $process->stdout->on('data', function ($output) use($teleport, $request, &$message) {
             $message .= $output;
             if ($teleport->getConfig()->get('verbose', null, false) || $teleport->getConfig()->get('debug', null, false)) {
                 echo $output;
             }
         });
         $process->stderr->on('data', function ($output) use($teleport, $request, &$message) {
             $message .= $output;
             if ($teleport->getConfig()->get('verbose', null, false) || $teleport->getConfig()->get('debug', null, false)) {
                 echo $output;
             }
         });
     });
     $loop->run();
 }
Example #5
0
<?php

require __DIR__ . '/../../vendor/autoload.php';
$reactor = (new Alert\ReactorFactory())->select();
$loop = (new Loopio\LoopFactory())->createReactLoop($reactor);
$process = new React\ChildProcess\Process('php child-child.php');
$process->on('exit', function ($exitCode, $termSignal) {
    echo "Child exit\n";
});
$loop->addTimer(0.001, function ($timer) use($process) {
    $process->start($timer->getLoop());
    $process->stdout->on('data', function ($output) {
        echo "Child script says: {$output}";
    });
});
$loop->addPeriodicTimer(5, function ($timer) {
    echo "Parent cannot be blocked by child\n";
});
$loop->run();
Example #6
0
<?php

require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$process = new React\ChildProcess\Process('uptime');
$loop->addTimer(0.001, function ($timer) use($process) {
    $process->start($timer->getLoop());
    $process->stdout->on('data', function ($output) {
        echo $output;
    });
});
$loop->run();
Example #7
0
<?php

require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$process = new React\ChildProcess\Process('php echo_child.php');
$loop->addTimer(0.001, function ($timer) use($process) {
    $loop = $timer->getLoop();
    $process->on('exit', function ($output) use($loop) {
        $loop->stop();
    });
    $process->start($loop);
    $process->stdout->on('data', function ($output) {
        echo $output, PHP_EOL;
    });
    $i = 0;
    $loop->addPeriodicTimer(1, function ($timer) use(&$i, $process) {
        $process->stdin->write($i++);
    });
});
$loop->run();