From 82520856ba3445267d065ddb96366e657a72c510 Mon Sep 17 00:00:00 2001 From: boombatower Date: Fri, 7 Sep 2012 00:03:48 -0700 Subject: [PATCH 1/6] [process] provide a restart method. --- src/Symfony/Component/Process/Process.php | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index cd1e8b42155a..32a50c6ad65f 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -143,6 +143,21 @@ public function __destruct() $this->stop(); } + public function __clone() + { + unset($this->exitcode); + unset($this->fallbackExitcode); + unset($this->processInformation); + unset($this->stdout); + unset($this->stderr); + unset($this->pipes); + unset($this->process); + unset($this->fileHandles); + unset($this->readBytes); + + $this->status = self::STATUS_READY; + } + /** * Runs the process. * @@ -300,6 +315,29 @@ public function start($callback = null) $this->updateStatus(); } + /** + * Restarts the process by cloning and invoking start(). + * + * @param Closure|string|array $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. * From 7dc25a9c1167d37d7b2ee96068232a7d62caadfc Mon Sep 17 00:00:00 2001 From: boombatower Date: Fri, 7 Sep 2012 00:41:41 -0700 Subject: [PATCH 2/6] Add newline before return. --- src/Symfony/Component/Process/Process.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 32a50c6ad65f..b63fbc390eb9 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -335,6 +335,7 @@ public function restart($callback = null) $process = clone $this; $process->start($callback); + return $process; } From 286131b4f79fb5111dcb0ba144813f1827fb53b0 Mon Sep 17 00:00:00 2001 From: boombatower Date: Fri, 7 Sep 2012 00:53:27 -0700 Subject: [PATCH 3/6] Add tests. --- src/Symfony/Component/Process/Tests/ProcessTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Symfony/Component/Process/Tests/ProcessTest.php b/src/Symfony/Component/Process/Tests/ProcessTest.php index 64f929cc20d6..d1b1de6d81f3 100644 --- a/src/Symfony/Component/Process/Tests/ProcessTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessTest.php @@ -155,6 +155,15 @@ public function testStop() } } + public function testRestart() + { + $process1 = new Process('php -r "echo getmypid();"'); + $process1->run(); + $process2 = $process1->restart(); + usleep(300000); // wait for output + $this->assertNotEquals($process1->getOutput(), $process2->getOutput()); + } + public function testPhpDeadlock() { $this->markTestSkipped('Can course php to hang'); From 34d821f8f2b40cd59f319fce6d480f38a5cc08cf Mon Sep 17 00:00:00 2001 From: boombatower Date: Fri, 7 Sep 2012 14:34:45 -0700 Subject: [PATCH 4/6] Update $callback documentation. --- src/Symfony/Component/Process/Process.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index b63fbc390eb9..0d7fca7e8a7f 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -318,8 +318,8 @@ public function start($callback = null) /** * Restarts the process by cloning and invoking start(). * - * @param Closure|string|array $callback A PHP callback to run whenever there is some - * output available on STDOUT or STDERR + * @param callable $callback A PHP callback to run whenever there is some + * output available on STDOUT or STDERR * * @return Process The new process. * From 72c9793eaf244b0301df176c5dcb75aeb3231b5c Mon Sep 17 00:00:00 2001 From: boombatower Date: Fri, 7 Sep 2012 14:42:07 -0700 Subject: [PATCH 5/6] Set properties to null instead of unset()'ing. --- src/Symfony/Component/Process/Process.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 0d7fca7e8a7f..e4b4640074a8 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -145,17 +145,16 @@ public function __destruct() public function __clone() { - unset($this->exitcode); - unset($this->fallbackExitcode); - unset($this->processInformation); - unset($this->stdout); - unset($this->stderr); - unset($this->pipes); - unset($this->process); - unset($this->fileHandles); - unset($this->readBytes); - + $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; } /** From 76aedb6a3729f75a3750efd23fadffe78e2a6d8e Mon Sep 17 00:00:00 2001 From: boombatower Date: Fri, 7 Sep 2012 14:47:31 -0700 Subject: [PATCH 6/6] Update test to be more robust. --- src/Symfony/Component/Process/Tests/ProcessTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Symfony/Component/Process/Tests/ProcessTest.php b/src/Symfony/Component/Process/Tests/ProcessTest.php index d1b1de6d81f3..5548cb7a696d 100644 --- a/src/Symfony/Component/Process/Tests/ProcessTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessTest.php @@ -160,7 +160,16 @@ 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()); }