Skip to content

Commit

Permalink
[process] provide a restart method.
Browse files Browse the repository at this point in the history
  • Loading branch information
boombatower authored and fabpot committed Oct 4, 2012
1 parent 4dc197c commit be62fcc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Symfony/Component/Process/Process.php
Expand Up @@ -143,6 +143,20 @@ public function __destruct()
$this->stop();
}

public function __clone()
{
$this->exitcode = null;
$this->fallbackExitcode = null;
$this->processInformation = null;
$this->stdout = null;
$this->stderr = null;
$this->pipes = null;
$this->process = null;
$this->status = self::STATUS_READY;
$this->fileHandles = null;
$this->readBytes = null;
}

/**
* Runs the process.
*
Expand Down Expand Up @@ -300,6 +314,30 @@ public function start($callback = null)
$this->updateStatus();
}

/**
* Restarts the process by cloning and invoking start().
*
* @param callable $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return Process The new process.
*
* @throws \RuntimeException When process can't be launch or is stopped
* @throws \RuntimeException When process is already running
* @see start()
*/
public function restart($callback = null)
{
if ($this->isRunning()) {
throw new \RuntimeException('Process is already running');
}

$process = clone $this;
$process->start($callback);

return $process;
}

/**
* Waits for the process to terminate.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Expand Up @@ -155,6 +155,24 @@ public function testStop()
}
}

public function testRestart()
{
$process1 = new Process('php -r "echo getmypid();"');
$process1->run();
$process2 = $process1->restart();

usleep(300000); // wait for output

// Ensure that both processed finished and the output is numeric
$this->assertFalse($process1->isRunning());
$this->assertFalse($process2->isRunning());
$this->assertTrue(is_numeric($process1->getOutput()));
$this->assertTrue(is_numeric($process2->getOutput()));

// Ensure that restart returned a new process by check that the output is different
$this->assertNotEquals($process1->getOutput(), $process2->getOutput());
}

public function testPhpDeadlock()
{
$this->markTestSkipped('Can course php to hang');
Expand Down

0 comments on commit be62fcc

Please sign in to comment.