Skip to content

Added a check to prevent non-CLI calls from failing. #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 6 commits into from
May 23, 2019
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
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