Skip to content

Commit

Permalink
Merge pull request #7 from henriquemoody/pidfile
Browse files Browse the repository at this point in the history
Create method `getPid()` on Pidfile class
  • Loading branch information
henriquemoody committed Nov 5, 2014
2 parents 308e723 + 46713a9 commit e37a7a5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 111 deletions.
91 changes: 0 additions & 91 deletions API.md

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "arara/process",
"type": "library",
"description": "Provides a better API to work process on Unix-like systems using PHP",
"description": "Provides a better API to work with processes on Unix-like systems",
"keywords": ["fork", "multitasking", "pcntl", "posix", "process", "spawn"],
"homepage": "https://github.com/Arara/Process",
"license" : "MIT",
Expand Down
17 changes: 12 additions & 5 deletions src/Arara/Process/Pidfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,26 @@ protected function getFileResource()

public function isActive()
{
$content = fgets($this->getFileResource());
$pieces = explode(PHP_EOL, trim($content));
if (! isset($pieces[0]) || (isset($pieces[0]) && empty($pieces[0]))) {
$pid = $this->getPid();
if (null === $pid) {
return false;
}

return $this->control->signal()->send(0, $pieces[0]);
return $this->control->signal()->send(0, $pid);
}

public function getPid()
{
$content = fgets($this->getFileResource());
$pieces = explode(PHP_EOL, trim($content));

return reset($pieces) ?: null;
}

public function initialize()
{
if ($this->isActive()) {
throw new RuntimeException('Pidfile is already active');
throw new RuntimeException('Process is already active');
}

if (! @flock($this->getFileResource(), (LOCK_EX | LOCK_NB))) {
Expand Down
40 changes: 26 additions & 14 deletions tests/Arara/Process/PidfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,32 +247,44 @@ public function testShouldCheckIfPidIsActiveWhenPidfileContainsProcessId()
$this->assertTrue($pidfile->isActive());
}

public function testShouldCheckOnlyTheFirstLineOfThePidWhenPidfileIsNotEmpty()
public function testShouldReturnPidWhenHasPidOnPidfile()
{
$processId = 123456;
$GLOBALS['arara']['fgets']['return'] = $processId . PHP_EOL . 987981723 . 12387687;
$GLOBALS['arara']['fgets']['return'] = $processId;

$signal = $this->getMock('Arara\Process\Control\Signal');
$signal
->expects($this->once())
->method('send')
->with(0, $processId)
->will($this->returnValue(true));
$control = $this->getMock('Arara\Process\Control');

$pidfile = new Pidfile($control);

$this->assertEquals($processId, $pidfile->getPid());
}

public function testShouldReturnNullWhenThereIsNoPidOnPidfile()
{
$GLOBALS['arara']['fgets']['return'] = '';

$control = $this->getMock('Arara\Process\Control');
$control
->expects($this->any())
->method('signal')
->will($this->returnValue($signal));

$pidfile = new Pidfile($control);

$this->assertTrue($pidfile->isActive());
$this->assertNull($pidfile->getPid());
}

public function testShouldReturnOnlyTheFirstLineOfThePidWhenPidfileIsNotEmpty()
{
$processId = 123456;
$GLOBALS['arara']['fgets']['return'] = $processId . PHP_EOL . 987981723 . 12387687;

$control = $this->getMock('Arara\Process\Control');

$pidfile = new Pidfile($control);

$this->assertEquals($processId, $pidfile->getPid());
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Pidfile is already active
* @expectedExceptionMessage Process is already active
*/
public function testShouldThrowsAnExceptionIfIsIsAlreadyActiveWhenInitializing()
{
Expand Down

0 comments on commit e37a7a5

Please sign in to comment.