Skip to content

Commit

Permalink
merged branch boombatower/process-restart (PR #5456)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes #5456).

Commits
-------

be62fcc [process] provide a restart method.

Discussion
----------

[process] provide a restart method.

Pull request for issue #5452.

Another possibility would be to allow for either run() or start() scenarios, but I am not sure that is terribly useful since restart() with a new process lends itself to restarting longer running services when they crash and you want the old process so you can inspect the logs and what not.

Otherwise, something like this might work, but doesn't allow for run() to return status code. Someone can get around that by getting manually on returned process.

```php
<?php
public function restart($method = 'start', $callback = null)
{
    if ($this->isRunning()) {
        throw new \RuntimeException('Process is already running');
    }

    if ($method != 'start' && $method != 'run') {
        throw new \RuntimeException('Method must be start or run');
    }

    $process = clone $this;
    $process->$method();
    return $process;
}
```

---------------------------------------------------------------------------

by pborreli at 2012-09-07T07:17:26Z

can you add some tests please ?
  • Loading branch information
fabpot committed Oct 4, 2012
2 parents c1e77d6 + be62fcc commit 0ba4886
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 @@ -150,6 +150,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 @@ -312,6 +326,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/AbstractProcessTest.php
Expand Up @@ -240,6 +240,24 @@ public function testProcessWithTermSignal()
$this->assertEquals(SIGTERM, $process->getTermSignal());
}

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 0ba4886

Please sign in to comment.