Skip to content

Commit

Permalink
Changed a lot of code and bumped the version to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jan 27, 2017
1 parent 67332f2 commit 7f986fd
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 118 deletions.
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# Project
/vendor/
/dev/
phpunit.xml
composer.lock

# System
.DS_Store
/vendor
/phpunit.xml
composer.lock
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
Process Locker for scripts
==========================
[![Test passing](https://travis-ci.org/HopeUA/locker.svg?branch=master)](https://travis-ci.org/HopeUA/locker)
[![Latest Stable Version](https://poser.pugx.org/hope/locker/v/stable.svg)](https://packagist.org/packages/hope/locker)
Usage
-----
# Process Locker for PHP scripts

Largely inspired by [HopeUA/Locker](https://github.com/HopeUA/locker)

## Usage
$locker = new FileLocker('large-process', ['lockDir' => '/tmp/lock']);
if ($locker->isLocked()) {
die('Already locked');
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hope/locker",
"description": "Process locker",
"keywords": ["locker","hopechannel","hopeua"],
"name": "loevgaard/locker",
"description": "PHP Process locker",
"keywords": ["locker"],
"license": "MIT",
"require": {
"php": ">=5.4.0"
Expand All @@ -12,7 +12,7 @@
},
"autoload": {
"psr-4": {
"Hope\\Locker\\": "src/"
"Loevgaard\\Locker\\": "src/"
}
}
}
82 changes: 35 additions & 47 deletions src/FileLocker.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Hope\Locker;
namespace Loevgaard\Locker;

/**
* Lock using file and id of process
Expand All @@ -22,81 +22,70 @@ class FileLocker implements LockerInterface
private $regPid = '~^\d+$~';

/**
* @var array Options
* The lock directory
*
* @var string
*/
private $options;
private $lockDir;

public function __construct($id, array $options = [])
public function __construct($id, $lockDir = null)
{
// Test ID
if (!preg_match($this->regId, $id)) {
throw new LockerException('Invalid ID', LockerException::INVALID_ID);
}

if(!isset($options['lockDir']) || !is_dir($options['lockDir'])) {
throw new LockerException('Invalid lock dir', LockerException::INVALID_LOCK_DIR);
if($lockDir) {
if(!is_dir($lockDir) || !is_writable($lockDir)) {
throw new LockerException('Invalid lock dir', LockerException::INVALID_LOCK_DIR);
}
} else {
$lockDir = sys_get_temp_dir();
}

$this->id = $id;
$this->options = $options;
$this->lockDir = $lockDir;
}

/**
* {@inheritdoc}
*/
public function lock()
{
// Check if already locked
if ($this->isLocked()) {
throw new LockerException('Already locked', LockerException::LOCKED);
// Check if lock file exists
if (file_exists($this->getFilePath())) {
// Get pid of last process
$pid = @file_get_contents($this->getFilePath());
if (false === $pid) {
throw new LockerException(sprintf('Failed to read the lock file %s', $this->getFilePath()), LockerException::FS_READ);
}

// Check if pid is valid
if (!preg_match($this->regPid, $pid)) {
throw new LockerException(sprintf('Unexpected content in lock file %s', $this->getFilePath()), LockerException::LOCK_CONTENT);
}

// Check if pid exist
// does not work on windows so we exclude this check on windows
if (stripos(PHP_OS, 'win') === false && !file_exists('/proc/' . $pid)) {
return false;
}
}

// Try to lock
if (false === @file_put_contents($this->getFilePath(), getmypid())) {
throw new LockerException(sprintf('Failed to write lock file "%s"', $this->getFilePath()), LockerException::FS_WRITE);
}
}

/**
* {@inheritdoc}
*/
public function unlock()
{
if ($this->isLocked()) {
if (false === @unlink($this->getFilePath())) {
throw new LockerException(sprintf('Failed to delete the lock file %s', $this->getFilePath()), LockerException::FS_DEL);
}
}
return true;
}

/**
* {@inheritdoc}
*/
public function isLocked()
public function release()
{
// Check the lock file
if (!file_exists($this->getFilePath())) {
return false;
}

// Get pid of last process
$pid = @file_get_contents($this->getFilePath());
if (false === $pid) {
throw new LockerException(sprintf('Failed to read the lock file %s', $this->getFilePath()), LockerException::FS_READ);
}

// Check if pid is valid
if (!preg_match($this->regPid, $pid)) {
throw new LockerException(sprintf('Unexpected content in lock file %s', $this->getFilePath()), LockerException::LOCK_CONTENT);
}

// Check if pid exist
// does not work on windows so we exclude this check on windows
if (stripos(PHP_OS, 'win') === false && !file_exists('/proc/' . $pid)) {
return false;
}

return true;
@unlink($this->getFilePath());
}

/**
Expand All @@ -106,8 +95,7 @@ public function isLocked()
*/
private function getFilePath()
{
$lockDir = $this->options['lockDir'];
$lockFile = $lockDir . DIRECTORY_SEPARATOR . $this->id . '.lock';
$lockFile = $this->lockDir . DIRECTORY_SEPARATOR . $this->id . '.lock';
return $lockFile;
}
}
2 changes: 1 addition & 1 deletion src/LockerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Hope\Locker;
namespace Loevgaard\Locker;

/**
* Locker aware interface
Expand Down
2 changes: 1 addition & 1 deletion src/LockerException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Hope\Locker;
namespace Loevgaard\Locker;

use RuntimeException;

Expand Down
14 changes: 3 additions & 11 deletions src/LockerInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Hope\Locker;
namespace Loevgaard\Locker;

/**
* Interface for different lockers
Expand All @@ -15,18 +15,10 @@ interface LockerInterface
public function lock();

/**
* Unlocks the locker
* Releases the lock
*
* @return void
* @throws LockerException
*/
public function unlock();

/**
* Check if lock is locked
*
* @return bool
* @throws LockerException
*/
public function isLocked();
public function release();
}
Loading

0 comments on commit 7f986fd

Please sign in to comment.