Skip to content

Commit

Permalink
Merge pull request #9 from im-bryan/feature/non-cli
Browse files Browse the repository at this point in the history
Added a check to prevent non-CLI calls from failing.
  • Loading branch information
mathroc authored May 23, 2019
2 parents 5c7aa0c + bf185ed commit 11250f4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/RedisSimpleLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ class RedisSimpleLock implements Lock
* @param Client $client the Predis client
* @param integer $ttl lock time-to-live in milliseconds
* @param LoggerInterface|null $logger
* @param array $ignoredSAPIs the server apis to ignore the pcntl_signal callback for
*/
public function __construct($identifier, Client $client, $ttl = 10000, LoggerInterface $logger = null)
public function __construct($identifier, Client $client, $ttl = 10000, LoggerInterface $logger = null, array $ignoredSAPIs = [])
{
$this->identifier = $identifier;
$this->client = $client;
$this->ttl = $ttl;
$this->logger = $logger ?: new NullLogger;
$this->id = mt_rand();
register_shutdown_function($closure = $this->releaseClosure());
pcntl_signal(SIGINT, $closure);

if (!in_array(php_sapi_name(), $ignoredSAPIs)) {
if (!function_exists('pcntl_signal')) {
throw new \RuntimeException("pcntl_signal() from the pcntl extension is not availlable, configure `$ignoredSAPIs = ['".php_sapi_name()."']` to skip catching SIGINT signal.");
}
pcntl_signal(SIGINT, $closure);
}
}

public function acquire()
Expand Down
12 changes: 7 additions & 5 deletions src/RedisSimpleLockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class RedisSimpleLockFactory implements TtlFactory
private $client;
private $defaultTtl;
private $logger;
private $ignoredSAPIs;

public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface $logger = null)
public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface $logger = null, array $ignoredSAPIs = [])
{
$this->client = $client;
$this->defaultTtl = $defaultTtl;
$this->logger = $logger ?: new NullLogger;
$this->client = $client;
$this->defaultTtl = $defaultTtl;
$this->logger = $logger ?: new NullLogger;
$this->ignoredSAPIs = $ignoredSAPIs;
}

/**
Expand All @@ -30,6 +32,6 @@ public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface
*/
public function create($identifier, $ttl = null)
{
return new RedisSimpleLock($identifier, $this->client, $ttl ?: $this->defaultTtl, $this->logger);
return new RedisSimpleLock($identifier, $this->client, $ttl ?: $this->defaultTtl, $this->logger, $this->ignoredSAPIs);
}
}
17 changes: 17 additions & 0 deletions tests/RedisSimpleLockFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,27 @@ protected function setUp()
$this->redisClient->flushdb();
}

public function testCreateIgnoredSAPIsLock()
{
$factory = new RedisSimpleLockFactory($this->redisClient, 50, null, [php_sapi_name()]);
$lock = $factory->create('lock identifier');
$this->assertInstanceOf(RedisSimpleLock::class, $lock);

if (function_exists('pcntl_signal_get_handler')) {
$handler = pcntl_signal_get_handler(SIGINT);
$this->assertEmpty($handler);
}
}

public function testCreateLock()
{
$factory = new RedisSimpleLockFactory($this->redisClient, 50);
$lock = $factory->create('lock identifier');
$this->assertInstanceOf(RedisSimpleLock::class, $lock);

if (function_exists('pcntl_signal_get_handler')) {
$handler = pcntl_signal_get_handler(SIGINT);
$this->assertInstanceOf(Closure::class, $handler);
}
}
}
2 changes: 1 addition & 1 deletion tests/RedisSimpleLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected function setUp()
$this->redisClient = new \Predis\Client(getenv("REDIS_URI"));
$this->redisClient->flushdb();
}

public function testLock()
{
$lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50);
Expand Down

0 comments on commit 11250f4

Please sign in to comment.