Skip to content

Commit ae2eb1f

Browse files
committed
rename overlapping strategy to mutex
1 parent b42274b commit ae2eb1f

12 files changed

+172
-133
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Illuminate\Console\Scheduling;
4+
5+
use Illuminate\Contracts\Cache\Repository as Cache;
6+
7+
class CacheMutex implements Mutex
8+
{
9+
/**
10+
* The cache repository implementation.
11+
*
12+
* @var \Illuminate\Contracts\Cache\Repository
13+
*/
14+
public $cache;
15+
16+
/**
17+
* Create a new overlapping strategy.
18+
*
19+
* @param \Illuminate\Contracts\Cache\Repository $cache
20+
* @return void
21+
*/
22+
public function __construct(Cache $cache)
23+
{
24+
$this->cache = $cache;
25+
}
26+
27+
/**
28+
* Attempt to obtain a mutex for the given event.
29+
*
30+
* @param \Illuminate\Console\Scheduling\Event $event
31+
* @return bool
32+
*/
33+
public function create(Event $event)
34+
{
35+
return $this->cache->add($event->mutexName(), true, 1440);
36+
}
37+
38+
/**
39+
* Determine if a mutex exists for the given event.
40+
*
41+
* @param \Illuminate\Console\Scheduling\Event $event
42+
* @return bool
43+
*/
44+
public function exists(Event $event)
45+
{
46+
return $this->cache->has($event->mutexName());
47+
}
48+
49+
/**
50+
* Clear the mutex for the given event.
51+
*
52+
* @param \Illuminate\Console\Scheduling\Event $event
53+
* @return void
54+
*/
55+
public function forget(Event $event)
56+
{
57+
$this->cache->forget($event->mutexName());
58+
}
59+
}

src/Illuminate/Console/Scheduling/CacheOverlappingStrategy.php

-33
This file was deleted.

src/Illuminate/Console/Scheduling/CallbackEvent.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ class CallbackEvent extends Event
2525
/**
2626
* Create a new event instance.
2727
*
28-
* @param OverlappingStrategy $overlappingStrategy
28+
* @param \Illuminate\Console\Scheduling\Mutex $mutex
2929
* @param string $callback
3030
* @param array $parameters
3131
* @return void
3232
*
3333
* @throws \InvalidArgumentException
3434
*/
35-
public function __construct(OverlappingStrategy $overlappingStrategy, $callback, array $parameters = [])
35+
public function __construct(Mutex $mutex, $callback, array $parameters = [])
3636
{
3737
if (! is_string($callback) && ! is_callable($callback)) {
3838
throw new InvalidArgumentException(
3939
'Invalid scheduled callback event. Must be a string or callable.'
4040
);
4141
}
4242

43-
$this->overlappingStrategy = $overlappingStrategy;
43+
$this->mutex = $mutex;
4444
$this->callback = $callback;
4545
$this->parameters = $parameters;
4646
}
@@ -56,7 +56,7 @@ public function __construct(OverlappingStrategy $overlappingStrategy, $callback,
5656
public function run(Container $container)
5757
{
5858
if ($this->description) {
59-
$this->overlappingStrategy->prevent($this);
59+
$this->mutex->create($this);
6060
}
6161

6262
try {
@@ -78,7 +78,7 @@ public function run(Container $container)
7878
protected function removeMutex()
7979
{
8080
if ($this->description) {
81-
$this->overlappingStrategy->reset($this);
81+
$this->mutex->forget($this);
8282
}
8383
}
8484

@@ -98,7 +98,7 @@ public function withoutOverlapping()
9898
}
9999

100100
return $this->skip(function () {
101-
return $this->overlappingStrategy->overlaps($this);
101+
return $this->mutex->exists($this);
102102
});
103103
}
104104

src/Illuminate/Console/Scheduling/Event.php

+26-13
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ class Event
1515
{
1616
use Macroable, ManagesFrequencies;
1717

18-
/**
19-
* The overlapping strategy implementation.
20-
*
21-
* @var OverlappingStrategy
22-
*/
23-
protected $overlappingStrategy;
24-
2518
/**
2619
* The command string.
2720
*
@@ -127,16 +120,23 @@ class Event
127120
*/
128121
public $description;
129122

123+
/**
124+
* The mutex implementation.
125+
*
126+
* @var \Illuminate\Console\Scheduling\Mutex
127+
*/
128+
public $mutex;
129+
130130
/**
131131
* Create a new event instance.
132132
*
133-
* @param OverlappingStrategy $overlappingStrategy
133+
* @param \Illuminate\Console\Scheduling\Mutex $mutex
134134
* @param string $command
135135
* @return void
136136
*/
137-
public function __construct(OverlappingStrategy $overlappingStrategy, $command)
137+
public function __construct(Mutex $mutex, $command)
138138
{
139-
$this->overlappingStrategy = $overlappingStrategy;
139+
$this->mutex = $mutex;
140140
$this->command = $command;
141141
$this->output = $this->getDefaultOutput();
142142
}
@@ -160,7 +160,7 @@ public function getDefaultOutput()
160160
public function run(Container $container)
161161
{
162162
if ($this->withoutOverlapping &&
163-
! $this->overlappingStrategy->prevent($this)) {
163+
! $this->mutex->create($this)) {
164164
return;
165165
}
166166

@@ -516,9 +516,9 @@ public function withoutOverlapping()
516516
$this->withoutOverlapping = true;
517517

518518
return $this->then(function () {
519-
$this->overlappingStrategy->reset($this);
519+
$this->mutex->forget($this);
520520
})->skip(function () {
521-
return $this->overlappingStrategy->overlaps($this);
521+
return $this->mutex->exists($this);
522522
});
523523
}
524524

@@ -632,4 +632,17 @@ public function getExpression()
632632
{
633633
return $this->expression;
634634
}
635+
636+
/**
637+
* Set the mutex implementation to be used.
638+
*
639+
* @param \Illuminate\Console\Scheduling\Mutex $mutex
640+
* @return $this
641+
*/
642+
public function preventOverlapsUsing(Mutex $mutex)
643+
{
644+
$this->mutex = $mutex;
645+
646+
return $this;
647+
}
635648
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Illuminate\Console\Scheduling;
4+
5+
interface Mutex
6+
{
7+
/**
8+
* Attempt to obtain a mutex for the given event.
9+
*
10+
* @param \Illuminate\Console\Scheduling\Event $event
11+
* @return bool
12+
*/
13+
public function create(Event $event);
14+
15+
/**
16+
* Determine if a mutex exists for the given event.
17+
*
18+
* @param \Illuminate\Console\Scheduling\Event $event
19+
* @return bool
20+
*/
21+
public function exists(Event $event);
22+
23+
/**
24+
* Clear the mutex for the given event.
25+
*
26+
* @param \Illuminate\Console\Scheduling\Event $event
27+
* @return void
28+
*/
29+
public function forget(Event $event);
30+
}

src/Illuminate/Console/Scheduling/OverlappingStrategy.php

-30
This file was deleted.

src/Illuminate/Console/Scheduling/Schedule.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
class Schedule
1010
{
1111
/**
12-
* The overlapping strategy implementation.
12+
* All of the events on the schedule.
1313
*
14-
* @var OverlappingStrategy
14+
* @var array
1515
*/
16-
protected $overlappingStrategy;
16+
protected $events = [];
1717

1818
/**
19-
* All of the events on the schedule.
19+
* The mutex implementation.
2020
*
21-
* @var array
21+
* @var \Illuminate\Console\Scheduling\Mutex
2222
*/
23-
protected $events = [];
23+
protected $mutex;
2424

2525
/**
2626
* Create a new event instance.
@@ -31,11 +31,9 @@ public function __construct()
3131
{
3232
$container = Container::getInstance();
3333

34-
if (! $container->bound(OverlappingStrategy::class)) {
35-
$this->overlappingStrategy = $container->make(CacheOverlappingStrategy::class);
36-
} else {
37-
$this->overlappingStrategy = $container->make(OverlappingStrategy::class);
38-
}
34+
$this->mutex = $container->bound(Mutex::class)
35+
? $container->make(Mutex::class)
36+
: $container->make(CacheMutex::class);
3937
}
4038

4139
/**
@@ -47,7 +45,9 @@ public function __construct()
4745
*/
4846
public function call($callback, array $parameters = [])
4947
{
50-
$this->events[] = $event = new CallbackEvent($this->overlappingStrategy, $callback, $parameters);
48+
$this->events[] = $event = new CallbackEvent(
49+
$this->mutex, $callback, $parameters
50+
);
5151

5252
return $event;
5353
}
@@ -96,7 +96,7 @@ public function exec($command, array $parameters = [])
9696
$command .= ' '.$this->compileParameters($parameters);
9797
}
9898

99-
$this->events[] = $event = new Event($this->overlappingStrategy, $command);
99+
$this->events[] = $event = new Event($this->mutex, $command);
100100

101101
return $event;
102102
}

tests/Console/ConsoleEventSchedulerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public function setUp()
1414

1515
$container = \Illuminate\Container\Container::getInstance();
1616

17-
$container->instance('Illuminate\Console\Scheduling\OverlappingStrategy', m::mock('Illuminate\Console\Scheduling\CacheOverlappingStrategy'));
17+
$container->instance('Illuminate\Console\Scheduling\Mutex', m::mock('Illuminate\Console\Scheduling\CacheMutex'));
1818

1919
$container->instance(
20-
'Illuminate\Console\Scheduling\Schedule', $this->schedule = new Schedule(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'))
20+
'Illuminate\Console\Scheduling\Schedule', $this->schedule = new Schedule(m::mock('Illuminate\Console\Scheduling\Mutex'))
2121
);
2222
}
2323

0 commit comments

Comments
 (0)