Skip to content
Browse files

[process] provide a restart method.

  • Loading branch information...
1 parent 4dc197c commit be62fcca7f9c658f1abd52cb5371a9768a9e0907 @boombatower boombatower committed with fabpot Sep 7, 2012
Showing with 56 additions and 0 deletions.
  1. +38 −0 src/Symfony/Component/Process/Process.php
  2. +18 −0 src/Symfony/Component/Process/Tests/ProcessTest.php
View
38 src/Symfony/Component/Process/Process.php
@@ -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.
*
@@ -301,6 +315,30 @@ public function start($callback = null)
}
/**
+ * 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.
*
* The callback receives the type of output (out or err) and some bytes
View
18 src/Symfony/Component/Process/Tests/ProcessTest.php
@@ -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');

0 comments on commit be62fcc

Please sign in to comment.
Something went wrong with that request. Please try again.