8000 Added stop method to BackgroundProcess by florianeckerstorfer · Pull Request #3 · cocur/background-process · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added stop method to BackgroundProcess #3

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

Merged
merged 1 commit into from
May 2, 2014
Merged
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
17 changes: 17 additions & 0 deletions 17 src/BackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public function isRunning()
return false;
}

/**
* Stops the process.
*
* @return boolean `true` if the processes was stopped, `false` otherwise.
*/
public function stop()
{
try {
$result = shell_exec(sprintf('kill %d 2>&1', $this->pid));
if (!preg_match('/No such process/', $result)) {
return true;
}
} catch (Exception $e) {}

return false;
}

/**
* Returns the ID of the process.
*
Expand Down
14 changes: 9 additions & 5 deletions tests/BackgroundProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* BackgroundProcessTest
*
* @category Test
* @category test
* @package cocur/background-process
* @author Florian Eckerstorfer <florian@eckerstorfer.co>
* @copyright 2013-2104 Florian Eckerstorfer
Expand All @@ -26,13 +26,17 @@ class BackgroundProcessTest extends \PHPUnit_Framework_TestCase
* @covers Cocur\BackgroundProcess\BackgroundProcess::run()
* @covers Cocur\BackgroundProcess\BackgroundProcess::isRunning()
* @covers Cocur\BackgroundProcess\BackgroundProcess::getPid()
* @covers Cocur\BackgroundProcess\BackgroundProcess::stop()
*/
public function testRun()
{
$process = new BackgroundProcess('sleep 1');
$this->assertFalse($process->isRunning());
$process = new BackgroundProcess('sleep 5');
$this->assertFalse($process->isRunning(), 'process should not run');
$process->run();
$this->assertNotNull($process->getPid());
$this->assertTrue($process->isRunning());
$this->assertNotNull($process->getPid(), 'process should have a pid');
$this->assertTrue($process->isRunning(), 'process should run');
$this->assertTrue($process->stop(), 'stop process');
$this->assertFalse($process->isRunning(), 'processes should not run anymore');
$this->assertFalse($process->stop(), 'cannot stop process that is not running');
}
}
0