-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.4] add OverlappingStrategy for Schedule/Event #18295
Merged
taylorotwell
merged 8 commits into
laravel:5.4
from
fetzi:feature/schedule-overlapping-strategy
Mar 23, 2017
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b366d5b
add overlapping strategy to be able to override the cache implementat…
fetzi 42d5c85
fix broken schedule tests
fetzi 781d4c0
add tests for CacheOverlappingStrategy
fetzi fdac88e
fix docbloc spacings
fetzi 41df68e
apply StyleCI fixes
fetzi d3befb9
remove unused import
fetzi ac3e486
fix conflict with commit 8ebb5b859ae8f2382a1836a83a47542de234d63a
fetzi 4b4c484
Merge branch '5.4' into feature/schedule-overlapping-strategy
fetzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/Illuminate/Console/Scheduling/CacheOverlappingStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console\Scheduling; | ||
|
||
use Illuminate\Contracts\Cache\Repository as Cache; | ||
|
||
class CacheOverlappingStrategy implements OverlappingStrategy | ||
{ | ||
/** | ||
* @var \Illuminate\Contracts\Cache\Repository | ||
*/ | ||
private $cache; | ||
|
||
public function __construct(Cache $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
public function prevent(Event $event) | ||
{ | ||
$this->cache->put($event->mutexName(), true, 1440); | ||
} | ||
|
||
public function overlaps(Event $event) | ||
{ | ||
return $this->cache->has($event->mutexName()); | ||
} | ||
|
||
public function reset(Event $event) | ||
{ | ||
$this->cache->forget($event->mutexName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console\Scheduling; | ||
|
||
interface OverlappingStrategy | ||
{ | ||
/** | ||
* prevents overlapping for the given event. | ||
* | ||
* @param Event $event | ||
* @return void | ||
*/ | ||
public function prevent(Event $event); | ||
|
||
/** | ||
* checks if the given event's command is already running. | ||
* | ||
* @param Event $event | ||
* @return bool | ||
*/ | ||
public function overlaps(Event $event); | ||
|
||
/** | ||
* resets the overlapping strategy for the given event. | ||
* | ||
* @param Event $event | ||
* @return void | ||
*/ | ||
public function reset(Event $event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Console\Scheduling; | ||
|
||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
use Illuminate\Console\Scheduling\Event; | ||
use Illuminate\Console\Scheduling\CacheOverlappingStrategy; | ||
|
||
class CacheOverlappingStrategyTest extends TestCase | ||
{ | ||
/** | ||
* @var CacheOverlappingStrategy | ||
*/ | ||
protected $cacheOverlappingStrategy; | ||
|
||
/** | ||
* @var \Illuminate\Contracts\Cache\Repository | ||
*/ | ||
protected $cacheRepository; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); | ||
$this->cacheOverlappingStrategy = new CacheOverlappingStrategy($this->cacheRepository); | ||
} | ||
|
||
public function testPreventOverlap() | ||
{ | ||
$cacheOverlappingStrategy = $this->cacheOverlappingStrategy; | ||
|
||
$this->cacheRepository->shouldReceive('put'); | ||
|
||
$event = new Event($this->cacheOverlappingStrategy, 'command'); | ||
|
||
$cacheOverlappingStrategy->prevent($event); | ||
} | ||
|
||
public function testOverlapsForNonRunningTask() | ||
{ | ||
$cacheOverlappingStrategy = $this->cacheOverlappingStrategy; | ||
|
||
$this->cacheRepository->shouldReceive('has')->andReturn(false); | ||
|
||
$event = new Event($this->cacheOverlappingStrategy, 'command'); | ||
|
||
$this->assertFalse($cacheOverlappingStrategy->overlaps($event)); | ||
} | ||
|
||
public function testOverlapsForRunningTask() | ||
{ | ||
$cacheOverlappingStrategy = $this->cacheOverlappingStrategy; | ||
|
||
$this->cacheRepository->shouldReceive('has')->andReturn(true); | ||
|
||
$event = new Event($this->cacheOverlappingStrategy, 'command'); | ||
|
||
$this->assertTrue($cacheOverlappingStrategy->overlaps($event)); | ||
} | ||
|
||
public function testResetOverlap() | ||
{ | ||
$cacheOverlappingStrategy = $this->cacheOverlappingStrategy; | ||
|
||
$this->cacheRepository->shouldReceive('forget'); | ||
|
||
$event = new Event($this->cacheOverlappingStrategy, 'command'); | ||
|
||
$cacheOverlappingStrategy->reset($event); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1440 = 24 hours.
Depending on your workload, if one of your servers goes down, you might not want to wait a full day for the key to expire and resume schedule.