From 20e29199365a11b31e35179bbfe3e83485e05a03 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 8 Feb 2018 14:43:29 -0600 Subject: [PATCH] allow customization of schedule mutex cache store --- .../Console/Scheduling/CacheEventMutex.php | 32 +++++++++++++---- .../Scheduling/CacheSchedulingMutex.php | 36 +++++++++++++++---- .../Console/Scheduling/Schedule.php | 19 ++++++++++ tests/Console/ConsoleEventSchedulerTest.php | 13 ++++++- .../Scheduling/CacheEventMutexTest.php | 18 +++++++++- .../Scheduling/CacheSchedulingMutexTest.php | 20 +++++++++-- 6 files changed, 122 insertions(+), 16 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/CacheEventMutex.php b/src/Illuminate/Console/Scheduling/CacheEventMutex.php index 849d5adb19ff..4eaaf05291b5 100644 --- a/src/Illuminate/Console/Scheduling/CacheEventMutex.php +++ b/src/Illuminate/Console/Scheduling/CacheEventMutex.php @@ -2,21 +2,28 @@ namespace Illuminate\Console\Scheduling; -use Illuminate\Contracts\Cache\Repository as Cache; +use Illuminate\Contracts\Cache\Factory as Cache; class CacheEventMutex implements EventMutex { /** * The cache repository implementation. * - * @var \Illuminate\Contracts\Cache\Repository + * @var \Illuminate\Contracts\Cache\Factory */ public $cache; + /** + * The cache store that should be used. + * + * @var string|null + */ + public $store; + /** * Create a new overlapping strategy. * - * @param \Illuminate\Contracts\Cache\Repository $cache + * @param \Illuminate\Contracts\Cache\Factory $cache * @return void */ public function __construct(Cache $cache) @@ -32,7 +39,7 @@ public function __construct(Cache $cache) */ public function create(Event $event) { - return $this->cache->add( + return $this->cache->store($this->store)->add( $event->mutexName(), true, $event->expiresAt ); } @@ -45,7 +52,7 @@ public function create(Event $event) */ public function exists(Event $event) { - return $this->cache->has($event->mutexName()); + return $this->cache->store($this->store)->has($event->mutexName()); } /** @@ -56,6 +63,19 @@ public function exists(Event $event) */ public function forget(Event $event) { - $this->cache->forget($event->mutexName()); + $this->cache->store($this->store)->forget($event->mutexName()); + } + + /** + * Specify the cache store that should be used. + * + * @param string $store + * @return $this + */ + public function useStore($store) + { + $this->store = $store; + + return $this; } } diff --git a/src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php b/src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php index c865f4c63187..79e3dea770d9 100644 --- a/src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php +++ b/src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php @@ -3,21 +3,28 @@ namespace Illuminate\Console\Scheduling; use DateTimeInterface; -use Illuminate\Contracts\Cache\Repository as Cache; +use Illuminate\Contracts\Cache\Factory as Cache; class CacheSchedulingMutex implements SchedulingMutex { /** - * The cache repository implementation. + * The cache factory implementation. * - * @var \Illuminate\Contracts\Cache\Repository + * @var \Illuminate\Contracts\Cache\Factory */ public $cache; + /** + * The cache store that should be used. + * + * @var string|null + */ + public $store; + /** * Create a new overlapping strategy. * - * @param \Illuminate\Contracts\Cache\Repository $cache + * @param \Illuminate\Contracts\Cache\Factory $cache * @return void */ public function __construct(Cache $cache) @@ -34,7 +41,9 @@ public function __construct(Cache $cache) */ public function create(Event $event, DateTimeInterface $time) { - return $this->cache->add($event->mutexName().$time->format('Hi'), true, 60); + return $this->cache->store($this->store)->add( + $event->mutexName().$time->format('Hi'), true, 60 + ); } /** @@ -46,6 +55,21 @@ public function create(Event $event, DateTimeInterface $time) */ public function exists(Event $event, DateTimeInterface $time) { - return $this->cache->has($event->mutexName().$time->format('Hi')); + return $this->cache->store($this->store)->has( + $event->mutexName().$time->format('Hi') + ); + } + + /** + * Specify the cache store that should be used. + * + * @param string $store + * @return $this + */ + public function useStore($store) + { + $this->store = $store; + + return $this; } } diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index 9c418b1f839a..be99741fcf0f 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -174,4 +174,23 @@ public function events() { return $this->events; } + + /** + * Specify the cache store that should be used to store mutexes. + * + * @param string $store + * @return $this + */ + public function useCache($store) + { + if ($this->eventMutex instanceof CacheEventMutex) { + $this->eventMutex->useStore($store); + } + + if ($this->schedulingMutex instanceof CacheSchedulingMutex) { + $this->schedulingMutex->useStore($store); + } + + return $this; + } } diff --git a/tests/Console/ConsoleEventSchedulerTest.php b/tests/Console/ConsoleEventSchedulerTest.php index d29f4a210d4d..644e2599ed51 100644 --- a/tests/Console/ConsoleEventSchedulerTest.php +++ b/tests/Console/ConsoleEventSchedulerTest.php @@ -4,7 +4,10 @@ use Mockery as m; use PHPUnit\Framework\TestCase; +use Illuminate\Container\Container; use Illuminate\Console\Scheduling\Schedule; +use Illuminate\Console\Scheduling\EventMutex; +use Illuminate\Console\Scheduling\SchedulingMutex; class ConsoleEventSchedulerTest extends TestCase { @@ -12,7 +15,7 @@ public function setUp() { parent::setUp(); - $container = \Illuminate\Container\Container::getInstance(); + $container = Container::getInstance(); $container->instance('Illuminate\Console\Scheduling\EventMutex', m::mock('Illuminate\Console\Scheduling\CacheEventMutex')); @@ -28,6 +31,14 @@ public function tearDown() m::close(); } + public function testMutexCanReceiveCustomStore() + { + Container::getInstance()->make(EventMutex::class)->shouldReceive('useStore')->once()->with('test'); + Container::getInstance()->make(SchedulingMutex::class)->shouldReceive('useStore')->once()->with('test'); + + $this->schedule->useCache('test'); + } + public function testExecCreatesNewCommand() { $escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\''; diff --git a/tests/Console/Scheduling/CacheEventMutexTest.php b/tests/Console/Scheduling/CacheEventMutexTest.php index 8bc639b2edf2..1efc67281676 100644 --- a/tests/Console/Scheduling/CacheEventMutexTest.php +++ b/tests/Console/Scheduling/CacheEventMutexTest.php @@ -19,6 +19,11 @@ class CacheEventMutexTest extends TestCase */ protected $event; + /** + * @var \Illuminate\Contracts\Cache\Factory + */ + protected $cacheFactory; + /** * @var \Illuminate\Contracts\Cache\Repository */ @@ -28,8 +33,10 @@ public function setUp() { parent::setUp(); + $this->cacheFactory = m::mock('Illuminate\Contracts\Cache\Factory'); $this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); - $this->cacheMutex = new CacheEventMutex($this->cacheRepository); + $this->cacheFactory->shouldReceive('store')->andReturn($this->cacheRepository); + $this->cacheMutex = new CacheEventMutex($this->cacheFactory); $this->event = new Event($this->cacheMutex, 'command'); } @@ -40,6 +47,15 @@ public function testPreventOverlap() $this->cacheMutex->create($this->event); } + public function testCustomConnection() + { + $this->cacheFactory->shouldReceive('store')->with('test')->andReturn($this->cacheRepository); + $this->cacheRepository->shouldReceive('add')->once(); + $this->cacheMutex->useStore('test'); + + $this->cacheMutex->create($this->event); + } + public function testPreventOverlapFails() { $this->cacheRepository->shouldReceive('add')->once()->andReturn(false); diff --git a/tests/Console/Scheduling/CacheSchedulingMutexTest.php b/tests/Console/Scheduling/CacheSchedulingMutexTest.php index cd05af16046b..c118fe67f540 100644 --- a/tests/Console/Scheduling/CacheSchedulingMutexTest.php +++ b/tests/Console/Scheduling/CacheSchedulingMutexTest.php @@ -26,6 +26,11 @@ class CacheSchedulingMutexTest extends TestCase */ protected $time; + /** + * @var \Illuminate\Contracts\Cache\Factory + */ + protected $cacheFactory; + /** * @var \Illuminate\Contracts\Cache\Repository */ @@ -35,9 +40,11 @@ public function setUp() { parent::setUp(); + $this->cacheFactory = m::mock('Illuminate\Contracts\Cache\Factory'); $this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); - $this->cacheMutex = new CacheSchedulingMutex($this->cacheRepository); - $this->event = new Event(new CacheEventMutex($this->cacheRepository), 'command'); + $this->cacheFactory->shouldReceive('store')->andReturn($this->cacheRepository); + $this->cacheMutex = new CacheSchedulingMutex($this->cacheFactory); + $this->event = new Event(new CacheEventMutex($this->cacheFactory), 'command'); $this->time = Carbon::now(); } @@ -48,6 +55,15 @@ public function testMutexReceviesCorrectCreate() $this->assertTrue($this->cacheMutex->create($this->event, $this->time)); } + public function testCanUseCustomConnection() + { + $this->cacheFactory->shouldReceive('store')->with('test')->andReturn($this->cacheRepository); + $this->cacheRepository->shouldReceive('add')->once()->with($this->event->mutexName().$this->time->format('Hi'), true, 60)->andReturn(true); + $this->cacheMutex->useStore('test'); + + $this->assertTrue($this->cacheMutex->create($this->event, $this->time)); + } + public function testPreventsMultipleRuns() { $this->cacheRepository->shouldReceive('add')->once()->with($this->event->mutexName().$this->time->format('Hi'), true, 60)->andReturn(false);