Permalink
Browse files

rename overlapping strategy to mutex

  • Loading branch information...
taylorotwell committed Mar 23, 2017
1 parent b42274b commit ae2eb1f498aa6c2e6c45040d26fb9502eabab535
@@ -0,0 +1,59 @@
<?php
namespace Illuminate\Console\Scheduling;
use Illuminate\Contracts\Cache\Repository as Cache;
class CacheMutex implements Mutex
{
/**
* The cache repository implementation.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
public $cache;
/**
* Create a new overlapping strategy.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
* @return void
*/
public function __construct(Cache $cache)
{
$this->cache = $cache;
}
/**
* Attempt to obtain a mutex for the given event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return bool
*/
public function create(Event $event)
{
return $this->cache->add($event->mutexName(), true, 1440);
}
/**
* Determine if a mutex exists for the given event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return bool
*/
public function exists(Event $event)
{
return $this->cache->has($event->mutexName());
}
/**
* Clear the mutex for the given event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return void
*/
public function forget(Event $event)
{
$this->cache->forget($event->mutexName());
}
}

This file was deleted.

Oops, something went wrong.
@@ -25,22 +25,22 @@ class CallbackEvent extends Event
/**
* Create a new event instance.
*
* @param OverlappingStrategy $overlappingStrategy
* @param \Illuminate\Console\Scheduling\Mutex $mutex
* @param string $callback
* @param array $parameters
* @return void
*
* @throws \InvalidArgumentException
*/
public function __construct(OverlappingStrategy $overlappingStrategy, $callback, array $parameters = [])
public function __construct(Mutex $mutex, $callback, array $parameters = [])
{
if (! is_string($callback) && ! is_callable($callback)) {
throw new InvalidArgumentException(
'Invalid scheduled callback event. Must be a string or callable.'
);
}
$this->overlappingStrategy = $overlappingStrategy;
$this->mutex = $mutex;
$this->callback = $callback;
$this->parameters = $parameters;
}
@@ -56,7 +56,7 @@ public function __construct(OverlappingStrategy $overlappingStrategy, $callback,
public function run(Container $container)
{
if ($this->description) {
$this->overlappingStrategy->prevent($this);
$this->mutex->create($this);
}
try {
@@ -78,7 +78,7 @@ public function run(Container $container)
protected function removeMutex()
{
if ($this->description) {
$this->overlappingStrategy->reset($this);
$this->mutex->forget($this);
}
}
@@ -98,7 +98,7 @@ public function withoutOverlapping()
}
return $this->skip(function () {
return $this->overlappingStrategy->overlaps($this);
return $this->mutex->exists($this);
});
}
@@ -15,13 +15,6 @@ class Event
{
use Macroable, ManagesFrequencies;
/**
* The overlapping strategy implementation.
*
* @var OverlappingStrategy
*/
protected $overlappingStrategy;
/**
* The command string.
*
@@ -127,16 +120,23 @@ class Event
*/
public $description;
/**
* The mutex implementation.
*
* @var \Illuminate\Console\Scheduling\Mutex
*/
public $mutex;
/**
* Create a new event instance.
*
* @param OverlappingStrategy $overlappingStrategy
* @param \Illuminate\Console\Scheduling\Mutex $mutex
* @param string $command
* @return void
*/
public function __construct(OverlappingStrategy $overlappingStrategy, $command)
public function __construct(Mutex $mutex, $command)
{
$this->overlappingStrategy = $overlappingStrategy;
$this->mutex = $mutex;
$this->command = $command;
$this->output = $this->getDefaultOutput();
}
@@ -160,7 +160,7 @@ public function getDefaultOutput()
public function run(Container $container)
{
if ($this->withoutOverlapping &&
! $this->overlappingStrategy->prevent($this)) {
! $this->mutex->create($this)) {
return;
}
@@ -516,9 +516,9 @@ public function withoutOverlapping()
$this->withoutOverlapping = true;
return $this->then(function () {
$this->overlappingStrategy->reset($this);
$this->mutex->forget($this);
})->skip(function () {
return $this->overlappingStrategy->overlaps($this);
return $this->mutex->exists($this);
});
}
@@ -632,4 +632,17 @@ public function getExpression()
{
return $this->expression;
}
/**
* Set the mutex implementation to be used.
*
* @param \Illuminate\Console\Scheduling\Mutex $mutex
* @return $this
*/
public function preventOverlapsUsing(Mutex $mutex)
{
$this->mutex = $mutex;
return $this;
}
}
@@ -0,0 +1,30 @@
<?php
namespace Illuminate\Console\Scheduling;
interface Mutex
{
/**
* Attempt to obtain a mutex for the given event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return bool
*/
public function create(Event $event);
/**
* Determine if a mutex exists for the given event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return bool
*/
public function exists(Event $event);
/**
* Clear the mutex for the given event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return void
*/
public function forget(Event $event);
}

This file was deleted.

Oops, something went wrong.
@@ -9,18 +9,18 @@
class Schedule
{
/**
* The overlapping strategy implementation.
* All of the events on the schedule.
*
* @var OverlappingStrategy
* @var array
*/
protected $overlappingStrategy;
protected $events = [];
/**
* All of the events on the schedule.
* The mutex implementation.
*
* @var array
* @var \Illuminate\Console\Scheduling\Mutex
*/
protected $events = [];
protected $mutex;
/**
* Create a new event instance.
@@ -31,11 +31,9 @@ public function __construct()
{
$container = Container::getInstance();
if (! $container->bound(OverlappingStrategy::class)) {
$this->overlappingStrategy = $container->make(CacheOverlappingStrategy::class);
} else {
$this->overlappingStrategy = $container->make(OverlappingStrategy::class);
}
$this->mutex = $container->bound(Mutex::class)
? $container->make(Mutex::class)
: $container->make(CacheMutex::class);
}
/**
@@ -47,7 +45,9 @@ public function __construct()
*/
public function call($callback, array $parameters = [])
{
$this->events[] = $event = new CallbackEvent($this->overlappingStrategy, $callback, $parameters);
$this->events[] = $event = new CallbackEvent(
$this->mutex, $callback, $parameters
);
return $event;
}
@@ -96,7 +96,7 @@ public function exec($command, array $parameters = [])
$command .= ' '.$this->compileParameters($parameters);
}
$this->events[] = $event = new Event($this->overlappingStrategy, $command);
$this->events[] = $event = new Event($this->mutex, $command);
return $event;
}
@@ -14,10 +14,10 @@ public function setUp()
$container = \Illuminate\Container\Container::getInstance();
$container->instance('Illuminate\Console\Scheduling\OverlappingStrategy', m::mock('Illuminate\Console\Scheduling\CacheOverlappingStrategy'));
$container->instance('Illuminate\Console\Scheduling\Mutex', m::mock('Illuminate\Console\Scheduling\CacheMutex'));
$container->instance(
'Illuminate\Console\Scheduling\Schedule', $this->schedule = new Schedule(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'))
'Illuminate\Console\Scheduling\Schedule', $this->schedule = new Schedule(m::mock('Illuminate\Console\Scheduling\Mutex'))
);
}
Oops, something went wrong.

0 comments on commit ae2eb1f

Please sign in to comment.