Skip to content

Commit 11250f4

Browse files
authored
Merge pull request #9 from im-bryan/feature/non-cli
Added a check to prevent non-CLI calls from failing.
2 parents 5c7aa0c + bf185ed commit 11250f4

4 files changed

+34
-8
lines changed

src/RedisSimpleLock.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ class RedisSimpleLock implements Lock
2424
* @param Client $client the Predis client
2525
* @param integer $ttl lock time-to-live in milliseconds
2626
* @param LoggerInterface|null $logger
27+
* @param array $ignoredSAPIs the server apis to ignore the pcntl_signal callback for
2728
*/
28-
public function __construct($identifier, Client $client, $ttl = 10000, LoggerInterface $logger = null)
29+
public function __construct($identifier, Client $client, $ttl = 10000, LoggerInterface $logger = null, array $ignoredSAPIs = [])
2930
{
3031
$this->identifier = $identifier;
3132
$this->client = $client;
3233
$this->ttl = $ttl;
3334
$this->logger = $logger ?: new NullLogger;
3435
$this->id = mt_rand();
3536
register_shutdown_function($closure = $this->releaseClosure());
36-
pcntl_signal(SIGINT, $closure);
37+
38+
if (!in_array(php_sapi_name(), $ignoredSAPIs)) {
39+
if (!function_exists('pcntl_signal')) {
40+
throw new \RuntimeException("pcntl_signal() from the pcntl extension is not availlable, configure `$ignoredSAPIs = ['".php_sapi_name()."']` to skip catching SIGINT signal.");
41+
}
42+
pcntl_signal(SIGINT, $closure);
43+
}
3744
}
3845

3946
public function acquire()

src/RedisSimpleLockFactory.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ class RedisSimpleLockFactory implements TtlFactory
1212
private $client;
1313
private $defaultTtl;
1414
private $logger;
15+
private $ignoredSAPIs;
1516

16-
public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface $logger = null)
17+
public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface $logger = null, array $ignoredSAPIs = [])
1718
{
18-
$this->client = $client;
19-
$this->defaultTtl = $defaultTtl;
20-
$this->logger = $logger ?: new NullLogger;
19+
$this->client = $client;
20+
$this->defaultTtl = $defaultTtl;
21+
$this->logger = $logger ?: new NullLogger;
22+
$this->ignoredSAPIs = $ignoredSAPIs;
2123
}
2224

2325
/**
@@ -30,6 +32,6 @@ public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface
3032
*/
3133
public function create($identifier, $ttl = null)
3234
{
33-
return new RedisSimpleLock($identifier, $this->client, $ttl ?: $this->defaultTtl, $this->logger);
35+
return new RedisSimpleLock($identifier, $this->client, $ttl ?: $this->defaultTtl, $this->logger, $this->ignoredSAPIs);
3436
}
3537
}

tests/RedisSimpleLockFactoryTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,27 @@ protected function setUp()
1313
$this->redisClient->flushdb();
1414
}
1515

16+
public function testCreateIgnoredSAPIsLock()
17+
{
18+
$factory = new RedisSimpleLockFactory($this->redisClient, 50, null, [php_sapi_name()]);
19+
$lock = $factory->create('lock identifier');
20+
$this->assertInstanceOf(RedisSimpleLock::class, $lock);
21+
22+
if (function_exists('pcntl_signal_get_handler')) {
23+
$handler = pcntl_signal_get_handler(SIGINT);
24+
$this->assertEmpty($handler);
25+
}
26+
}
27+
1628
public function testCreateLock()
1729
{
1830
$factory = new RedisSimpleLockFactory($this->redisClient, 50);
1931
$lock = $factory->create('lock identifier');
2032
$this->assertInstanceOf(RedisSimpleLock::class, $lock);
33+
34+
if (function_exists('pcntl_signal_get_handler')) {
35+
$handler = pcntl_signal_get_handler(SIGINT);
36+
$this->assertInstanceOf(Closure::class, $handler);
37+
}
2138
}
2239
}

tests/RedisSimpleLockTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected function setUp()
1111
$this->redisClient = new \Predis\Client(getenv("REDIS_URI"));
1212
$this->redisClient->flushdb();
1313
}
14-
14+
1515
public function testLock()
1616
{
1717
$lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50);

0 commit comments

Comments
 (0)