Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cocur/background-process
Browse files Browse the repository at this point in the history
* 'master' of github.com:cocur/background-process:
  Add createFromPID static method
  • Loading branch information
Florian Eckerstorfer committed Feb 11, 2017
2 parents a0f6251 + 7a2ee16 commit 9ae2902
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ echo "\nDone.\n";

If the process runs you can stop it:

```
```php
// ...
if ($process->isRunning()) {
$process->stop();
Expand All @@ -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
----------
Expand Down
23 changes: 19 additions & 4 deletions src/BackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions tests/BackgroundProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 9ae2902

Please sign in to comment.