Skip to content

Commit

Permalink
allow customization of schedule mutex cache store
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 8, 2018
1 parent 74c54c0 commit 20e2919
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 16 deletions.
32 changes: 26 additions & 6 deletions src/Illuminate/Console/Scheduling/CacheEventMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
);
}
Expand All @@ -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());
}

/**
Expand All @@ -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;
}
}
36 changes: 30 additions & 6 deletions src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
);
}

/**
Expand All @@ -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;
}
}
19 changes: 19 additions & 0 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 12 additions & 1 deletion tests/Console/ConsoleEventSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

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
{
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'));

Expand All @@ -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 ? '"' : '\'';
Expand Down
18 changes: 17 additions & 1 deletion tests/Console/Scheduling/CacheEventMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class CacheEventMutexTest extends TestCase
*/
protected $event;

/**
* @var \Illuminate\Contracts\Cache\Factory
*/
protected $cacheFactory;

/**
* @var \Illuminate\Contracts\Cache\Repository
*/
Expand All @@ -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');
}

Expand All @@ -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);
Expand Down
20 changes: 18 additions & 2 deletions tests/Console/Scheduling/CacheSchedulingMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class CacheSchedulingMutexTest extends TestCase
*/
protected $time;

/**
* @var \Illuminate\Contracts\Cache\Factory
*/
protected $cacheFactory;

/**
* @var \Illuminate\Contracts\Cache\Repository
*/
Expand All @@ -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();
}

Expand All @@ -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);
Expand Down

0 comments on commit 20e2919

Please sign in to comment.