From 9605ec0dbd511f36ff96c39596e295ccc48f8870 Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Sat, 11 Feb 2017 13:34:03 +0100 Subject: [PATCH] Add createFromPID static method --- README.md | 13 ++++++++++++- src/BackgroundProcess.php | 23 +++++++++++++++++++---- tests/BackgroundProcessTest.php | 21 +++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fd0fb43..c84d375 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ echo "\nDone.\n"; If the process runs you can stop it: -``` +```php // ... if ($process->isRunning()) { $process->stop(); @@ -72,6 +72,17 @@ In practice, the following methods will throw an exception if called on a Window - `Cocur\BackgroundProcess\BackgroundProcess::isRunning()` - `Cocur\BackgroundProcess\BackgroundProcess::stop()` +### Create with existing PID + +If you have a long running process and store its PID in the database you might want to check at a later point (when you don't have the BackgroundProcess object anymore) whether the process is still running and stop the process. + +```php +use Cocur\BackgroundProcess\BackgroundProcess; + +$process = BackgroundProcess::createFromPID($pid); +$process->isRunning(); // -> true +$process->stop(); // -> true +``` Change Log ---------- diff --git a/src/BackgroundProcess.php b/src/BackgroundProcess.php index 413d975..599265c 100755 --- a/src/BackgroundProcess.php +++ b/src/BackgroundProcess.php @@ -62,7 +62,10 @@ public function __construct($command = null) */ public function run($outputFile = '/dev/null', $append = false) { - if(is_null($this->command)) return; + if($this->command === null) { + return; + } + switch ($this->getOS()) { case self::OS_WINDOWS: shell_exec(sprintf('%s &', $this->command, $outputFile)); @@ -134,13 +137,13 @@ public function getPid() return $this->pid; } - + /** * Set the process id. - * + * * @param $pid */ - public function setPid($pid) + protected function setPid($pid) { $this->pid = $pid; } @@ -174,4 +177,16 @@ protected function checkSupportingOS($message) throw new RuntimeException(sprintf($message, PHP_OS)); } } + + /** + * @param int $pid PID of process to resume + * + * @return Cocur\BackgroundProcess\BackgroundProcess + */ + static public function createFromPID($pid) { + $process = new self(); + $process->setPid($pid); + + return $process; + } } diff --git a/tests/BackgroundProcessTest.php b/tests/BackgroundProcessTest.php index 5793ff4..3f5707c 100644 --- a/tests/BackgroundProcessTest.php +++ b/tests/BackgroundProcessTest.php @@ -182,4 +182,25 @@ public function stopShouldThrowExceptionIfWindows() $process = new BackgroundProcess('sleep 1'); $process->stop(); } + + /** + * @test + * @covers Cocur\BackgroundProcess\BackgroundProcess::createFromPID() + */ + public function createFromPIDShouldCreateObjectFromPID() + { + if (preg_match('/^WIN/', PHP_OS)) { + $this->markTestSkipped('Cocur\BackgroundProcess\BackgroundProcess::createFromPID() is not supported on Windows.'); + + return; + } + $process = new BackgroundProcess('sleep 1'); + $process->run(); + $pid = $process->getPid(); + + $newProcess = BackgroundProcess::createFromPID($pid); + + $this->assertEquals($pid, $newProcess->getPid()); + $this->assertTrue($newProcess->stop()); + } }