protected function execute(InputInterface $input, OutputInterface $output) { $host = $this->getConfig('connecthost'); $port = $this->getConfig('connectport'); $timeout = $this->getConfig('connecttimeout'); $conn = $host . ':' . $port; $prompt = 'php-resque ' . $conn . '> '; $output->writeln('<comment>Connecting to ' . $conn . '...</comment>'); if (!($fh = @fsockopen('tcp://' . $host, $port, $errno, $errstr, $timeout))) { $output->writeln('<error>[' . $errno . '] ' . $errstr . ' host ' . $conn . '</error>'); return; } // Set socket timeout to 200ms stream_set_timeout($fh, 0, 200 * 1000); $stdin = fopen('php://stdin', 'r'); $prompting = false; Resque\Socket\Server::fwrite($fh, 'shell'); while (true) { if (feof($fh)) { $output->writeln('<comment>Connection to ' . $conn . ' closed.</comment>'); break; } $read = array($fh, $stdin); $write = null; $except = null; $selected = @stream_select($read, $write, $except, 0); if ($selected > 0) { foreach ($read as $r) { if ($r == $stdin) { $input = trim(fgets($stdin)); if (empty($input)) { $output->write($prompt); $prompting = true; } else { Resque\Socket\Server::fwrite($fh, $input); $prompting = false; } } elseif ($r == $fh) { $input = ''; while (($buffer = fgets($fh, 1024)) !== false) { $input .= $buffer; } if ($prompting) { $output->writeln(''); } $output->writeln('<pop>' . trim($input) . '</pop>'); if (!feof($fh)) { $output->write($prompt); $prompting = true; } } } } // Sleep for 10ms to stop CPU spiking usleep(10 * 1000); } fclose($fh); }
protected function execute(InputInterface $input, OutputInterface $output) { $cmd = $input->getArgument('cmd'); $host = $this->getConfig('connecthost'); $port = $this->getConfig('connectport'); $timeout = $this->getConfig('connecttimeout'); if (!($fh = @fsockopen('tcp://' . $host, $port, $errno, $errstr, $timeout))) { $this->log('[' . $errno . '] ' . $errstr . ' host ' . $host . ':' . $port, Resque\Logger::ERROR); return; } stream_set_timeout($fh, 0, 500 * 1000); $payload = array('cmd' => $cmd, 'id' => $input->getArgument('id'), 'force' => $input->getOption('force'), 'json' => $this->getConfig('json')); Resque\Socket\Server::fwrite($fh, json_encode($payload)); $response = ''; while (($buffer = fgets($fh, 1024)) !== false) { $response .= $buffer; } $this->log('<pop>' . trim($response) . '</pop>'); fclose($fh); }