Skip to content

Commit

Permalink
WIP - Implemented timeout checking and wait system - not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
sushilkg committed Sep 4, 2018
1 parent 0791e32 commit 926c4a6
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/Helper/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Shell
const STATE_STARTED = 'started';
const STATE_TERMINATED = 'terminated';

/** @var bool Whether to wait for the process to finish or return instantly */
protected $async = false;

/** @var string Command to be executed */
protected $command;

Expand All @@ -46,11 +49,17 @@ class Shell
/** @var resource The actual process resource returned from proc_open */
protected $process = null;

/** @var string Status of the process as returned from proc_get_status */
protected $processStatus = null;

/** @var int Process starting time in unix timestamp */
protected $processStartTime;

/** @var string Current state of the shell execution */
protected $state = self::STATE_READY;

/** @var string Status of the process as returned from proc_get_status */
protected $processStatus = null;
/** @var float Default timeout for the process in seconds with microseconds */
protected $processTimeoutPeriod = 10;

public function __construct(string $command, string $input = null)
{
Expand Down Expand Up @@ -98,15 +107,39 @@ protected function closePipes()
\fclose($this->pipes[self::STDERR_DESCRIPTOR_KEY]);
}

public function execute()
public function wait()
{
while ($this->isRunning()) {
$this->checkTimeout();
}

return $this->exitCode;
}

public function checkTimeout()
{
if ($this->state !== self::STATE_STARTED) {
return;
}

$execution_duration = microtime(true) - $this->processStartTime;

if ($execution_duration > $this->processTimeoutPeriod) {
$this->stop();

throw new RuntimeException("Process timeout occurred");
}
}

public function execute($async = false)
{
if ($this->isRunning()) {
throw new RuntimeException('Process is already running');
}

$this->descriptors = $this->getDescriptors();

$this->process = proc_open($this->command, $this->descriptors, $this->pipes);
$this->process = \proc_open($this->command, $this->descriptors, $this->pipes);

if (!\is_resource($this->process)) {
throw new RuntimeException('Bad program could not be started.');
Expand All @@ -116,6 +149,18 @@ public function execute()

$this->setInput();
$this->updateProcessStatus();
$this->processStartTime = microtime(true);

if ($this->async = $async) {
$this->setOutputStreamNonBlocking();
} else {
$this->wait();
}
}

private function setOutputStreamNonBlocking()
{
return \stream_set_blocking($this->pipes[self::STDOUT_DESCRIPTOR_KEY], 0);
}

public function getState()
Expand All @@ -125,6 +170,7 @@ public function getState()

public function getOutput()
{
\stream_set_blocking($this->pipes[self::STDOUT_DESCRIPTOR_KEY], 0);
return \stream_get_contents($this->pipes[self::STDOUT_DESCRIPTOR_KEY]);
}

Expand Down

0 comments on commit 926c4a6

Please sign in to comment.