Skip to content

remove $owner, that would be better handled at the logger level #9

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
Oct 18, 2015
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ There are two methods you can use on a `FileLock`:

And one on a `FileFactory`:

* `\TH\Lock\FileFactory::create($resource, $owner = null, $exclusive = FileLock::EXCLUSIVE, $blocking = FileLock::NON_BLOCKING)` used to create a `FileLock` for `$resource`
* `\TH\Lock\FileFactory::create($resource, $exclusive = FileLock::EXCLUSIVE, $blocking = FileLock::NON_BLOCKING)` used to create a `FileLock` for `$resource`

## Notes

Expand Down
6 changes: 3 additions & 3 deletions spec/TH/Lock/FileLockSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function it_should_throw_if_it_cannot_lock()

public function it_remove_its_lock_file_if_not_locked()
{
$this->beConstructedWith($this->lock_file, FileLock::EXCLUSIVE, FileLock::NON_BLOCKING, null, null, true);
$this->beConstructedWith($this->lock_file, FileLock::EXCLUSIVE, FileLock::NON_BLOCKING, null, true);

$this->acquire();
$this->release();
Expand All @@ -93,7 +93,7 @@ public function it_remove_its_lock_file_if_not_locked()

public function it_does_not_remove_its_lock_file_if_still_locked()
{
$this->beConstructedWith($this->lock_file, FileLock::SHARED, FileLock::NON_BLOCKING, null, null, true);
$this->beConstructedWith($this->lock_file, FileLock::SHARED, FileLock::NON_BLOCKING, null, true);

touch($this->lock_file);
flock(fopen($this->lock_file, 'r'), LOCK_SH|LOCK_NB);
Expand All @@ -108,7 +108,7 @@ public function it_does_not_remove_its_lock_file_if_still_locked()

public function it_can_acquire_then_release_and_acquire_again()
{
$this->beConstructedWith($this->lock_file, FileLock::EXCLUSIVE, FileLock::NON_BLOCKING, null, null, true);
$this->beConstructedWith($this->lock_file, FileLock::EXCLUSIVE, FileLock::NON_BLOCKING, null, true);

$this->acquire();
$this->release();
Expand Down
4 changes: 1 addition & 3 deletions src/FileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ public function __construct($lock_dir, $hash_algo = 'sha256', LoggerInterface $l
/**
* Create a FileLock for $resource
* @param string $resource resource identifier
* @param string|null $owner owner name (for logging)
* @param boolean $exclusive true for an exclusive lock, false for shared one
* @param boolean $blocking true to wait for lock to be available, false to throw exception instead of waiting
* @return FileLock
*/
public function create(
$resource,
$owner = null,
$exclusive = FileLock::EXCLUSIVE,
$blocking = FileLock::NON_BLOCKING
) {
Expand All @@ -47,7 +45,7 @@ public function create(

$path = $this->lock_dir.'/'.hash($this->hash_algo, serialize($resource)).'.lock';

$lock = new FileLock($path, $exclusive, $blocking, $resource, $owner, true, $this->logger);
$lock = new FileLock($path, $exclusive, $blocking, $resource, true, $this->logger);

return $lock;
}
Expand Down
14 changes: 3 additions & 11 deletions src/FileLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class FileLock implements Lock
private $exclusive;
private $blocking;
private $identifier;
private $owner;
private $fh;
private $remove_on_release;

Expand All @@ -30,7 +29,6 @@ class FileLock implements Lock
* @param boolean $blocking true to wait for lock to be available,
* false to throw exception instead of waiting
* @param string|null $identifier resource identifier (default to $lock_file) for logging
* @param string|null $owner owner name for logging
* @param boolean $remove_on_release remove file on release if no other lock remains
* @param LoggerInterface $logger
*/
Expand All @@ -39,15 +37,13 @@ public function __construct(
$exclusive = FileLock::EXCLUSIVE,
$blocking = FileLock::NON_BLOCKING,
$identifier = null,
$owner = null,
$remove_on_release = false,
LoggerInterface $logger = null
) {
$this->lock_file = $lock_file;
$this->exclusive = $exclusive;
$this->blocking = $blocking;
$this->identifier = $identifier?:$lock_file;
$this->owner = $owner === null ? '' : $owner.': ';
$this->remove_on_release = $remove_on_release;

$this->logger = $logger ?: new NullLogger;
Expand Down Expand Up @@ -84,20 +80,19 @@ private function tryAcquire($operation, $lock_type)
{
$log_data = [
'identifier' => $this->identifier,
'owner' => $this->owner,
'lock_type' => $lock_type
];

if (!$this->flock($operation)) {
$this->logger->debug('{owner} could not acquire {lock_type} lock on {identifier}', $log_data);
$this->logger->debug('could not acquire {lock_type} lock on {identifier}', $log_data);

throw new Exception(
'Could not acquire '.$lock_type.' lock on '.$this->identifier
);

}

$this->logger->debug('{owner} {lock_type} lock acquired on {identifier}', $log_data);
$this->logger->debug('{lock_type} lock acquired on {identifier}', $log_data);
}

public function release()
Expand All @@ -114,10 +109,7 @@ public function release()
fclose($this->fh);
$this->fh = null;

$this->logger->debug('{owner} lock released on {identifier}', [
'identifier' => $this->identifier,
'owner' => $this->owner
]);
$this->logger->debug('{lock_type} lock released on {identifier}', ['identifier' => $this->identifier]);
}

public function __destruct()
Expand Down