Skip to content

Commit

Permalink
rename overlapping strategy to mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 23, 2017
1 parent b42274b commit ae2eb1f
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 133 deletions.
59 changes: 59 additions & 0 deletions src/Illuminate/Console/Scheduling/CacheMutex.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
33 changes: 0 additions & 33 deletions src/Illuminate/Console/Scheduling/CacheOverlappingStrategy.php

This file was deleted.

12 changes: 6 additions & 6 deletions src/Illuminate/Console/Scheduling/CallbackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 {
Expand All @@ -78,7 +78,7 @@ public function run(Container $container)
protected function removeMutex()
{
if ($this->description) {
$this->overlappingStrategy->reset($this);
$this->mutex->forget($this);
}
}

Expand All @@ -98,7 +98,7 @@ public function withoutOverlapping()
}

return $this->skip(function () {
return $this->overlappingStrategy->overlaps($this);
return $this->mutex->exists($this);
});
}

Expand Down
39 changes: 26 additions & 13 deletions src/Illuminate/Console/Scheduling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ class Event
{
use Macroable, ManagesFrequencies;

/**
* The overlapping strategy implementation.
*
* @var OverlappingStrategy
*/
protected $overlappingStrategy;

/**
* The command string.
*
Expand Down Expand Up @@ -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();
}
Expand All @@ -160,7 +160,7 @@ public function getDefaultOutput()
public function run(Container $container)
{
if ($this->withoutOverlapping &&
! $this->overlappingStrategy->prevent($this)) {
! $this->mutex->create($this)) {
return;
}

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

Expand Down Expand Up @@ -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;
}
}
30 changes: 30 additions & 0 deletions src/Illuminate/Console/Scheduling/Mutex.php
Original file line number Diff line number Diff line change
@@ -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);
}
30 changes: 0 additions & 30 deletions src/Illuminate/Console/Scheduling/OverlappingStrategy.php

This file was deleted.

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

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/ConsoleEventSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
);
}

Expand Down
Loading

0 comments on commit ae2eb1f

Please sign in to comment.