From c6150eb30949480948740b392e55bf6bbec73e43 Mon Sep 17 00:00:00 2001 From: Bryan Sam Meulmeester Date: Wed, 22 May 2019 17:14:13 +0200 Subject: [PATCH 1/6] Added a check to prevent non-CLI calls from failing. --- src/RedisSimpleLock.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/RedisSimpleLock.php b/src/RedisSimpleLock.php index b739dcd..069ffff 100644 --- a/src/RedisSimpleLock.php +++ b/src/RedisSimpleLock.php @@ -33,7 +33,10 @@ public function __construct($identifier, Client $client, $ttl = 10000, LoggerInt $this->logger = $logger ?: new NullLogger; $this->id = mt_rand(); register_shutdown_function($closure = $this->releaseClosure()); - pcntl_signal(SIGINT, $closure); + + if (php_sapi_name() === 'cli') { + pcntl_signal(SIGINT, $closure); + } } public function acquire() From 544576ea50b80ce6c2f37cafe86e92f3acfc4183 Mon Sep 17 00:00:00 2001 From: Bryan Sam Meulmeester Date: Thu, 23 May 2019 10:43:02 +0200 Subject: [PATCH 2/6] Specific SAPIs can now be ignored when creating the RedisSimpleLock --- src/RedisSimpleLock.php | 8 ++++++-- tests/RedisSimpleLockTest.php | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/RedisSimpleLock.php b/src/RedisSimpleLock.php index 069ffff..267a852 100644 --- a/src/RedisSimpleLock.php +++ b/src/RedisSimpleLock.php @@ -24,8 +24,9 @@ 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; @@ -34,7 +35,10 @@ public function __construct($identifier, Client $client, $ttl = 10000, LoggerInt $this->id = mt_rand(); register_shutdown_function($closure = $this->releaseClosure()); - if (php_sapi_name() === 'cli') { + 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); } } diff --git a/tests/RedisSimpleLockTest.php b/tests/RedisSimpleLockTest.php index 3f4d228..371ab02 100644 --- a/tests/RedisSimpleLockTest.php +++ b/tests/RedisSimpleLockTest.php @@ -73,4 +73,15 @@ public function testLockAutoRelease() // first lock sould have been released $lock2->acquire(); } + + public function testSAPIIgnore() + { + $lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null, [php_sapi_name()]); + $handler = pcntl_signal_get_handler(SIGINT); + $this->assertEmpty($handler); + + $lock2 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null); + $handler = pcntl_signal_get_handler(SIGINT); + $this->assertInstanceOf(Closure::class, $handler); + } } From 7516cb827471d2977307b06ccf8cc67c925c6031 Mon Sep 17 00:00:00 2001 From: Bryan Sam Meulmeester Date: Thu, 23 May 2019 11:08:35 +0200 Subject: [PATCH 3/6] Moved the test up to make sure there's no signal set from previous tests. --- tests/RedisSimpleLockTest.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/RedisSimpleLockTest.php b/tests/RedisSimpleLockTest.php index 371ab02..f2f0ff5 100644 --- a/tests/RedisSimpleLockTest.php +++ b/tests/RedisSimpleLockTest.php @@ -12,6 +12,17 @@ protected function setUp() $this->redisClient->flushdb(); } + public function testSAPIIgnore() + { + $lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null, [php_sapi_name()]); + $handler = pcntl_signal_get_handler(SIGINT); + $this->assertEmpty($handler); + + $lock2 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null); + $handler = pcntl_signal_get_handler(SIGINT); + $this->assertInstanceOf(Closure::class, $handler); + } + public function testLock() { $lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50); @@ -73,15 +84,4 @@ public function testLockAutoRelease() // first lock sould have been released $lock2->acquire(); } - - public function testSAPIIgnore() - { - $lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null, [php_sapi_name()]); - $handler = pcntl_signal_get_handler(SIGINT); - $this->assertEmpty($handler); - - $lock2 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null); - $handler = pcntl_signal_get_handler(SIGINT); - $this->assertInstanceOf(Closure::class, $handler); - } } From 5fb935dd2d54702e2805684ecfd62a60cd546039 Mon Sep 17 00:00:00 2001 From: Bryan Sam Meulmeester Date: Thu, 23 May 2019 11:23:47 +0200 Subject: [PATCH 4/6] Moved the test to the factory --- src/RedisSimpleLockFactory.php | 12 +++++++----- tests/RedisSimpleLockFactoryTest.php | 13 +++++++++++++ tests/RedisSimpleLockTest.php | 13 +------------ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/RedisSimpleLockFactory.php b/src/RedisSimpleLockFactory.php index 719caa7..1db28e1 100644 --- a/src/RedisSimpleLockFactory.php +++ b/src/RedisSimpleLockFactory.php @@ -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; } /** @@ -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); } } diff --git a/tests/RedisSimpleLockFactoryTest.php b/tests/RedisSimpleLockFactoryTest.php index 35a821a..184b104 100644 --- a/tests/RedisSimpleLockFactoryTest.php +++ b/tests/RedisSimpleLockFactoryTest.php @@ -13,10 +13,23 @@ 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); + + $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); + + $handler = pcntl_signal_get_handler(SIGINT); + $this->assertInstanceOf(Closure::class, $handler); } } diff --git a/tests/RedisSimpleLockTest.php b/tests/RedisSimpleLockTest.php index f2f0ff5..64c2396 100644 --- a/tests/RedisSimpleLockTest.php +++ b/tests/RedisSimpleLockTest.php @@ -11,18 +11,7 @@ protected function setUp() $this->redisClient = new \Predis\Client(getenv("REDIS_URI")); $this->redisClient->flushdb(); } - - public function testSAPIIgnore() - { - $lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null, [php_sapi_name()]); - $handler = pcntl_signal_get_handler(SIGINT); - $this->assertEmpty($handler); - - $lock2 = new RedisSimpleLock("lock identifier", $this->redisClient, 50, null); - $handler = pcntl_signal_get_handler(SIGINT); - $this->assertInstanceOf(Closure::class, $handler); - } - + public function testLock() { $lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50); From 8c3eef5823d9972b5839f8cb6649a3629ecf9874 Mon Sep 17 00:00:00 2001 From: Bryan Sam Meulmeester Date: Thu, 23 May 2019 11:25:24 +0200 Subject: [PATCH 5/6] typos --- src/RedisSimpleLockFactory.php | 8 ++++---- tests/RedisSimpleLockFactoryTest.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/RedisSimpleLockFactory.php b/src/RedisSimpleLockFactory.php index 1db28e1..1919c1a 100644 --- a/src/RedisSimpleLockFactory.php +++ b/src/RedisSimpleLockFactory.php @@ -12,14 +12,14 @@ class RedisSimpleLockFactory implements TtlFactory private $client; private $defaultTtl; private $logger; - private $ignoredSapis; + private $ignoredSAPIs; - public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface $logger = null, array $ignoredSapis = []) + public function __construct(Client $client, $defaultTtl = 10000, LoggerInterface $logger = null, array $ignoredSAPIs = []) { $this->client = $client; $this->defaultTtl = $defaultTtl; $this->logger = $logger ?: new NullLogger; - $this->ignoredSapis = $ignoredSapis; + $this->ignoredSAPIs = $ignoredSAPIs; } /** @@ -32,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, $this->ignoredSapis); + return new RedisSimpleLock($identifier, $this->client, $ttl ?: $this->defaultTtl, $this->logger, $this->ignoredSAPIs); } } diff --git a/tests/RedisSimpleLockFactoryTest.php b/tests/RedisSimpleLockFactoryTest.php index 184b104..faff7d9 100644 --- a/tests/RedisSimpleLockFactoryTest.php +++ b/tests/RedisSimpleLockFactoryTest.php @@ -13,7 +13,7 @@ protected function setUp() $this->redisClient->flushdb(); } - public function testCreateIgnoredSapisLock() + public function testCreateIgnoredSAPIsLock() { $factory = new RedisSimpleLockFactory($this->redisClient, 50, null, [php_sapi_name()]); $lock = $factory->create('lock identifier'); From bf185edceb8c025003a5505383512337f7195130 Mon Sep 17 00:00:00 2001 From: Bryan Sam Meulmeester Date: Thu, 23 May 2019 11:30:07 +0200 Subject: [PATCH 6/6] pcntl_signal_get_handler doesnt exist pre 7.1 --- tests/RedisSimpleLockFactoryTest.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/RedisSimpleLockFactoryTest.php b/tests/RedisSimpleLockFactoryTest.php index faff7d9..db6beb0 100644 --- a/tests/RedisSimpleLockFactoryTest.php +++ b/tests/RedisSimpleLockFactoryTest.php @@ -19,8 +19,10 @@ public function testCreateIgnoredSAPIsLock() $lock = $factory->create('lock identifier'); $this->assertInstanceOf(RedisSimpleLock::class, $lock); - $handler = pcntl_signal_get_handler(SIGINT); - $this->assertEmpty($handler); + if (function_exists('pcntl_signal_get_handler')) { + $handler = pcntl_signal_get_handler(SIGINT); + $this->assertEmpty($handler); + } } public function testCreateLock() @@ -29,7 +31,9 @@ public function testCreateLock() $lock = $factory->create('lock identifier'); $this->assertInstanceOf(RedisSimpleLock::class, $lock); - $handler = pcntl_signal_get_handler(SIGINT); - $this->assertInstanceOf(Closure::class, $handler); + if (function_exists('pcntl_signal_get_handler')) { + $handler = pcntl_signal_get_handler(SIGINT); + $this->assertInstanceOf(Closure::class, $handler); + } } }