Skip to content

Commit

Permalink
CRM-7955: Consumer fails with "Pid file already exists" error
Browse files Browse the repository at this point in the history
     - re-factoring DbalPidFileManager
  • Loading branch information
tumbochka committed Mar 15, 2017
1 parent f291f29 commit 764ab48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Oro\Component\MessageQueue\Consumption\Dbal;

use Symfony\Component\Filesystem\Filesystem;

class DbalPidFileManager
{
/**
Expand All @@ -13,12 +15,18 @@ class DbalPidFileManager
*/
private $pidFileExtension = '.pid';

/**
* @var Filesystem
*/
private $filesystem;

/**
* @param string $pidDir
*/
public function __construct($pidDir)
{
$this->pidDir = $pidDir;
$this->filesystem = new Filesystem();
}

/**
Expand All @@ -32,7 +40,11 @@ public function createPidFile($consumerId)

$fHandler = @fopen($filename, 'x');
if (false === $fHandler) {
throw new \LogicException(sprintf('Pid file already exists. file:"%s"', $filename));
if ($this->filesystem->exists($filename)) {
throw new \LogicException(sprintf('The pid file already exists. file:"%s"', $filename));
} else {
throw new \LogicException(sprintf('Cannot write the pid file. file:"%s"', $filename));
}
}

$pid = getmypid();
Expand All @@ -53,7 +65,7 @@ public function createPidFile($consumerId)
*/
public function removePidFile($consumerId)
{
@unlink($this->generateFilenameByConsumerId($consumerId));
$this->filesystem->remove($this->generateFilenameByConsumerId($consumerId));
}

/**
Expand Down Expand Up @@ -88,7 +100,8 @@ public function getListOfPidsFileInfo()

private function ensurePidDirExists()
{
@mkdir($this->pidDir, 0777, true);
$this->filesystem->mkdir($this->pidDir, 0777, true);
$this->filesystem->chmod($this->pidDir, 0777);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Oro\Component\MessageQueue\Tests\Unit\Consumption\Dbal;
namespace Oro\Component\MessageQueue\Tests\Unit\Consumption\DBAL;

use Oro\Component\MessageQueue\Consumption\Dbal\DbalPidFileManager;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -36,7 +36,7 @@ public function testShouldThrowIfPidFileAlreadyExists()
{
$processManager = new DbalPidFileManager($this->pidDir);

$this->setExpectedException(\LogicException::class, 'Pid file already exists');
$this->setExpectedException(\LogicException::class, 'The pid file already exists');

$processManager->createPidFile('CONSUMER.ID');
$processManager->createPidFile('CONSUMER.ID');
Expand Down

0 comments on commit 764ab48

Please sign in to comment.