Skip to content

Commit b42274b

Browse files
committed
Merge branch 'feature/schedule-overlapping-strategy' of https://github.com/fetzi/framework into fetzi-feature/schedule-overlapping-strategy
2 parents da7b006 + 4b4c484 commit b42274b

File tree

10 files changed

+197
-44
lines changed

10 files changed

+197
-44
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Illuminate\Console\Scheduling;
4+
5+
use Illuminate\Contracts\Cache\Repository as Cache;
6+
7+
class CacheOverlappingStrategy implements OverlappingStrategy
8+
{
9+
/**
10+
* @var \Illuminate\Contracts\Cache\Repository
11+
*/
12+
private $cache;
13+
14+
public function __construct(Cache $cache)
15+
{
16+
$this->cache = $cache;
17+
}
18+
19+
public function prevent(Event $event)
20+
{
21+
return $this->cache->add($event->mutexName(), true, 1440);
22+
}
23+
24+
public function overlaps(Event $event)
25+
{
26+
return $this->cache->has($event->mutexName());
27+
}
28+
29+
public function reset(Event $event)
30+
{
31+
$this->cache->forget($event->mutexName());
32+
}
33+
}

src/Illuminate/Console/Scheduling/CallbackEvent.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use LogicException;
66
use InvalidArgumentException;
77
use Illuminate\Contracts\Container\Container;
8-
use Illuminate\Contracts\Cache\Repository as Cache;
98

109
class CallbackEvent extends Event
1110
{
@@ -26,22 +25,22 @@ class CallbackEvent extends Event
2625
/**
2726
* Create a new event instance.
2827
*
29-
* @param \Illuminate\Contracts\Cache\Repository $cache
28+
* @param OverlappingStrategy $overlappingStrategy
3029
* @param string $callback
3130
* @param array $parameters
3231
* @return void
3332
*
3433
* @throws \InvalidArgumentException
3534
*/
36-
public function __construct(Cache $cache, $callback, array $parameters = [])
35+
public function __construct(OverlappingStrategy $overlappingStrategy, $callback, array $parameters = [])
3736
{
3837
if (! is_string($callback) && ! is_callable($callback)) {
3938
throw new InvalidArgumentException(
4039
'Invalid scheduled callback event. Must be a string or callable.'
4140
);
4241
}
4342

44-
$this->cache = $cache;
43+
$this->overlappingStrategy = $overlappingStrategy;
4544
$this->callback = $callback;
4645
$this->parameters = $parameters;
4746
}
@@ -57,7 +56,7 @@ public function __construct(Cache $cache, $callback, array $parameters = [])
5756
public function run(Container $container)
5857
{
5958
if ($this->description) {
60-
$this->cache->put($this->mutexName(), true, 1440);
59+
$this->overlappingStrategy->prevent($this);
6160
}
6261

6362
try {
@@ -79,7 +78,7 @@ public function run(Container $container)
7978
protected function removeMutex()
8079
{
8180
if ($this->description) {
82-
$this->cache->forget($this->mutexName());
81+
$this->overlappingStrategy->reset($this);
8382
}
8483
}
8584

@@ -99,7 +98,7 @@ public function withoutOverlapping()
9998
}
10099

101100
return $this->skip(function () {
102-
return $this->cache->has($this->mutexName());
101+
return $this->overlappingStrategy->overlaps($this);
103102
});
104103
}
105104

src/Illuminate/Console/Scheduling/Event.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@
1010
use Symfony\Component\Process\Process;
1111
use Illuminate\Support\Traits\Macroable;
1212
use Illuminate\Contracts\Container\Container;
13-
use Illuminate\Contracts\Cache\Repository as Cache;
1413

1514
class Event
1615
{
1716
use Macroable, ManagesFrequencies;
1817

1918
/**
20-
* The cache store implementation.
19+
* The overlapping strategy implementation.
2120
*
22-
* @var \Illuminate\Contracts\Cache\Repository
21+
* @var OverlappingStrategy
2322
*/
24-
protected $cache;
23+
protected $overlappingStrategy;
2524

2625
/**
2726
* The command string.
@@ -131,13 +130,13 @@ class Event
131130
/**
132131
* Create a new event instance.
133132
*
134-
* @param \Illuminate\Contracts\Cache\Repository $cache
133+
* @param OverlappingStrategy $overlappingStrategy
135134
* @param string $command
136135
* @return void
137136
*/
138-
public function __construct(Cache $cache, $command)
137+
public function __construct(OverlappingStrategy $overlappingStrategy, $command)
139138
{
140-
$this->cache = $cache;
139+
$this->overlappingStrategy = $overlappingStrategy;
141140
$this->command = $command;
142141
$this->output = $this->getDefaultOutput();
143142
}
@@ -161,7 +160,7 @@ public function getDefaultOutput()
161160
public function run(Container $container)
162161
{
163162
if ($this->withoutOverlapping &&
164-
! $this->cache->add($this->mutexName(), true, 1440)) {
163+
! $this->overlappingStrategy->prevent($this)) {
165164
return;
166165
}
167166

@@ -517,9 +516,9 @@ public function withoutOverlapping()
517516
$this->withoutOverlapping = true;
518517

519518
return $this->then(function () {
520-
$this->cache->forget($this->mutexName());
519+
$this->overlappingStrategy->reset($this);
521520
})->skip(function () {
522-
return $this->cache->has($this->mutexName());
521+
return $this->overlappingStrategy->overlaps($this);
523522
});
524523
}
525524

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Illuminate\Console\Scheduling;
4+
5+
interface OverlappingStrategy
6+
{
7+
/**
8+
* prevents overlapping for the given event.
9+
*
10+
* @param Event $event
11+
* @return bool true if the prevent operation was successful
12+
*/
13+
public function prevent(Event $event);
14+
15+
/**
16+
* checks if the given event's command is already running.
17+
*
18+
* @param Event $event
19+
* @return bool
20+
*/
21+
public function overlaps(Event $event);
22+
23+
/**
24+
* resets the overlapping strategy for the given event.
25+
*
26+
* @param Event $event
27+
* @return void
28+
*/
29+
public function reset(Event $event);
30+
}

src/Illuminate/Console/Scheduling/Schedule.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
use Illuminate\Console\Application;
66
use Illuminate\Container\Container;
77
use Symfony\Component\Process\ProcessUtils;
8-
use Illuminate\Contracts\Cache\Repository as Cache;
98

109
class Schedule
1110
{
1211
/**
13-
* The cache store implementation.
12+
* The overlapping strategy implementation.
1413
*
15-
* @var \Illuminate\Contracts\Cache\Repository
14+
* @var OverlappingStrategy
1615
*/
17-
protected $cache;
16+
protected $overlappingStrategy;
1817

1918
/**
2019
* All of the events on the schedule.
@@ -26,12 +25,17 @@ class Schedule
2625
/**
2726
* Create a new event instance.
2827
*
29-
* @param \Illuminate\Contracts\Cache\Repository $cache
3028
* @return void
3129
*/
32-
public function __construct(Cache $cache)
30+
public function __construct()
3331
{
34-
$this->cache = $cache;
32+
$container = Container::getInstance();
33+
34+
if (! $container->bound(OverlappingStrategy::class)) {
35+
$this->overlappingStrategy = $container->make(CacheOverlappingStrategy::class);
36+
} else {
37+
$this->overlappingStrategy = $container->make(OverlappingStrategy::class);
38+
}
3539
}
3640

3741
/**
@@ -43,7 +47,7 @@ public function __construct(Cache $cache)
4347
*/
4448
public function call($callback, array $parameters = [])
4549
{
46-
$this->events[] = $event = new CallbackEvent($this->cache, $callback, $parameters);
50+
$this->events[] = $event = new CallbackEvent($this->overlappingStrategy, $callback, $parameters);
4751

4852
return $event;
4953
}
@@ -92,7 +96,7 @@ public function exec($command, array $parameters = [])
9296
$command .= ' '.$this->compileParameters($parameters);
9397
}
9498

95-
$this->events[] = $event = new Event($this->cache, $command);
99+
$this->events[] = $event = new Event($this->overlappingStrategy, $command);
96100

97101
return $event;
98102
}

tests/Console/ConsoleEventSchedulerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ public function setUp()
1212
{
1313
parent::setUp();
1414

15-
\Illuminate\Container\Container::getInstance()->instance(
16-
'Illuminate\Console\Scheduling\Schedule', $this->schedule = new Schedule(m::mock('Illuminate\Contracts\Cache\Repository'))
15+
$container = \Illuminate\Container\Container::getInstance();
16+
17+
$container->instance('Illuminate\Console\Scheduling\OverlappingStrategy', m::mock('Illuminate\Console\Scheduling\CacheOverlappingStrategy'));
18+
19+
$container->instance(
20+
'Illuminate\Console\Scheduling\Schedule', $this->schedule = new Schedule(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'))
1721
);
1822
}
1923

tests/Console/ConsoleScheduledEventTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testBasicCronCompilation()
3535
$app->shouldReceive('isDownForMaintenance')->andReturn(false);
3636
$app->shouldReceive('environment')->andReturn('production');
3737

38-
$event = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
38+
$event = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
3939
$this->assertEquals('* * * * * *', $event->getExpression());
4040
$this->assertTrue($event->isDue($app));
4141
$this->assertTrue($event->skip(function () {
@@ -45,25 +45,25 @@ public function testBasicCronCompilation()
4545
return true;
4646
})->filtersPass($app));
4747

48-
$event = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
48+
$event = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
4949
$this->assertEquals('* * * * * *', $event->getExpression());
5050
$this->assertFalse($event->environments('local')->isDue($app));
5151

52-
$event = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
52+
$event = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
5353
$this->assertEquals('* * * * * *', $event->getExpression());
5454
$this->assertFalse($event->when(function () {
5555
return false;
5656
})->filtersPass($app));
5757

5858
// chained rules should be commutative
59-
$eventA = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
60-
$eventB = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
59+
$eventA = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
60+
$eventB = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
6161
$this->assertEquals(
6262
$eventA->daily()->hourly()->getExpression(),
6363
$eventB->hourly()->daily()->getExpression());
6464

65-
$eventA = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
66-
$eventB = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
65+
$eventA = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
66+
$eventB = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
6767
$this->assertEquals(
6868
$eventA->weekdays()->hourly()->getExpression(),
6969
$eventB->hourly()->weekdays()->getExpression());
@@ -76,11 +76,11 @@ public function testEventIsDueCheck()
7676
$app->shouldReceive('environment')->andReturn('production');
7777
Carbon::setTestNow(Carbon::create(2015, 1, 1, 0, 0, 0));
7878

79-
$event = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
79+
$event = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
8080
$this->assertEquals('* * * * 4 *', $event->thursdays()->getExpression());
8181
$this->assertTrue($event->isDue($app));
8282

83-
$event = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
83+
$event = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
8484
$this->assertEquals('0 19 * * 3 *', $event->wednesdays()->at('19:00')->timezone('EST')->getExpression());
8585
$this->assertTrue($event->isDue($app));
8686
}
@@ -92,7 +92,7 @@ public function testTimeBetweenChecks()
9292
$app->shouldReceive('environment')->andReturn('production');
9393
Carbon::setTestNow(Carbon::now()->startOfDay()->addHours(9));
9494

95-
$event = new Event(m::mock('Illuminate\Contracts\Cache\Repository'), 'php foo');
95+
$event = new Event(m::mock('Illuminate\Console\Scheduling\OverlappingStrategy'), 'php foo');
9696
$this->assertTrue($event->between('8:00', '10:00')->filtersPass($app));
9797
$this->assertTrue($event->between('9:00', '9:00')->filtersPass($app));
9898
$this->assertFalse($event->between('10:00', '11:00')->filtersPass($app));

0 commit comments

Comments
 (0)