Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.2] [Process] Add getPid and sendSignal methods to Process #5391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Symfony/Component/Process/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process\Exception;

/**
* LogicException for the Process Component.
*
* @author Romain Neutron <[email protected]>
*/
class LogicException extends \LogicException implements ExceptionInterface
{
}
40 changes: 39 additions & 1 deletion src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\Process;

use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Exception\RuntimeException;

/**
* Process is a thin wrapper around proc_* functions to ease
* start independent PHP processes.
Expand Down Expand Up @@ -361,7 +364,7 @@ public function wait($callback = null)
}
$this->updateStatus();
if ($this->processInformation['signaled']) {
throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig']));
throw new RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig']));
}

$time = 0;
Expand Down Expand Up @@ -539,6 +542,41 @@ public function isRunning()
return $this->processInformation['running'];
}

/**
* Return the Pid (process id), if applicable
*
* @return integer|null The process id if running, null otherwise
*/
public function getPid()
{
$this->updateStatus();

return $this->isRunning() ? $this->processInformation['pid'] : null;
}

/**
* Send a posix signal to the process
*
* @param integer $signal A valid posix signal (see http://www.php.net/manual/en/pcntl.constants.php)
* @return Process
*
* @throws LogicException In case the process is not running
* @throws RuntimeException In case the environment does not support posix signals
* @throws RuntimeException In case of failure
*/
public function signal($signal)
{
if (!$this->isRunning()) {
throw new LogicException('Can not send signal on a non running process');
}

if (true !== proc_terminate($this->process, $signal)) {
throw new RuntimeException('Error while sending signal');
}

return $this;
}

/**
* Stops the process.
*
Expand Down
48 changes: 48 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Process\Tests;

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\RuntimeException;

/**
* @author Robert Schönthal <[email protected]>
Expand Down Expand Up @@ -141,6 +142,53 @@ public function testIsRunning()
$this->assertFalse($process->isRunning());
}

public function testGetPid()
{
$process = new Process('php -r "sleep(1);"');
$this->assertNull($process->getPid());
$process->start();
$this->assertGreaterThan(0, $process->getPid());
$process->wait();
$this->assertNull($process->getPid());
}

public function testSendSignal()
{
$process = new Process('php -r "sleep(1);');
$process->start();
$process->sendSignal(SIGCONT);
$process->stop();
}

/**
* @expectedException Symfony\Component\Process\Exception\RuntimeException
*/
public function testSendKillSignal()
{
$process = new Process('php -r "sleep(1);');
$process->start();
$process->sendSignal(SIGKILL);
$process->wait();
}

public function testSendKillSignalAndCatch()
{
$process = new Process('php -r "sleep(1);');
$process->start();
$process->sendSignal(SIGKILL);

try {
$process->wait();
$this->fail('Should throw an exception as the signal stops the process');
} catch (RuntimeException $e) {

}

$this->assertFalse($process->isRunning());
$this->assertTrue($process->hasBeenSignaled());
$this->assertEquals(SIGKILL, $process->getTermSignal());
}

public function testStop()
{
$process = new Process('php -r "while (true) {}"');
Expand Down