Skip to content
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.6] Multi server scheduling cron support #22137

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Console/Scheduling/CacheMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function __construct(Cache $cache)
public function create(Event $event)
{
return $this->cache->add(
$event->mutexName().$event->timestamp->format('Hi'), true, 60
) && $this->cache->add(
$event->mutexName(), true, $event->expiresAt
);
}
Expand All @@ -45,7 +47,7 @@ public function create(Event $event)
*/
public function exists(Event $event)
{
return $this->cache->has($event->mutexName());
return $this->cache->has($event->mutexName()) || $this->cache->has($event->mutexName().$event->timestamp->format('Hi'));
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/Illuminate/Console/Scheduling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Event
*/
public $timezone;

/**
* The timestamp the event was initialized.
*
* @var \Illuminate\Support\Carbon
*/
public $timestamp;

/**
* The user the command should run as.
*
Expand Down Expand Up @@ -146,6 +153,7 @@ public function __construct(Mutex $mutex, $command)
$this->mutex = $mutex;
$this->command = $command;
$this->output = $this->getDefaultOutput();
$this->timestamp = Carbon::now();
}

/**
Expand Down Expand Up @@ -287,13 +295,11 @@ public function runsInMaintenanceMode()
*/
protected function expressionPasses()
{
$date = Carbon::now();

if ($this->timezone) {
$date->setTimezone($this->timezone);
$this->timestamp->setTimezone($this->timezone);
}

return CronExpression::factory($this->expression)->isDue($date->toDateTimeString());
return CronExpression::factory($this->expression)->isDue($this->timestamp->toDateTimeString());
}

/**
Expand Down
65 changes: 36 additions & 29 deletions tests/Console/Scheduling/CacheOverlappingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class CacheMutexTest extends TestCase
*/
protected $cacheMutex;

/**
* @var \Illuminate\Console\Scheduling\Event
*/
protected $event;

/**
* @var \Illuminate\Contracts\Cache\Repository
*/
Expand All @@ -25,60 +30,62 @@ public function setUp()

$this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
$this->cacheMutex = new CacheMutex($this->cacheRepository);
$this->event = new Event($this->cacheMutex, 'command');
}

public function testPreventOverlap()
{
$cacheMutex = $this->cacheMutex;

$this->cacheRepository->shouldReceive('add');
$this->cacheRepository->shouldReceive('add')->with($this->event->mutexName().$this->event->timestamp->format('Hi'), true, 60)->andReturn(true);
$this->cacheRepository->shouldReceive('add')->with($this->event->mutexName(), true, $this->event->expiresAt)->andReturn(true);

$event = new Event($this->cacheMutex, 'command');

$cacheMutex->create($event);
$this->cacheMutex->create($this->event);
}

public function testPreventOverlapFails()
public function testPreventOverlapFailsDueToTaskRunningThisMinute()
{
$cacheMutex = $this->cacheMutex;

$this->cacheRepository->shouldReceive('add')->andReturn(false);

$event = new Event($this->cacheMutex, 'command');
$this->cacheRepository->shouldReceive('add')->with($this->event->mutexName().$this->event->timestamp->format('Hi'), true, 60)->andReturn(false);
$this->cacheRepository->shouldReceive('add')->with($this->event->mutexName(), true, $this->event->expiresAt)->never();

$this->assertFalse($cacheMutex->create($event));
$this->assertFalse($this->cacheMutex->create($this->event));
}

public function testOverlapsForNonRunningTask()
public function testPreventOverlapFailsDueToTaskStillRunning()
{
$cacheMutex = $this->cacheMutex;
$this->cacheRepository->shouldReceive('add')->with($this->event->mutexName().$this->event->timestamp->format('Hi'), true, 60)->andReturn(true);
$this->cacheRepository->shouldReceive('add')->with($this->event->mutexName(), true, $this->event->expiresAt)->andReturn(false);

$this->cacheRepository->shouldReceive('has')->andReturn(false);
$this->assertFalse($this->cacheMutex->create($this->event));
}

$event = new Event($this->cacheMutex, 'command');
public function testOverlapsForNonRunningTaskThatHasNotRunThisMinute()
{
$this->cacheRepository->shouldReceive('has')->with($this->event->mutexName())->andReturn(false);
$this->cacheRepository->shouldReceive('has')->with($this->event->mutexName().$this->event->timestamp->format('Hi'))->andReturn(false);

$this->assertFalse($cacheMutex->exists($event));
$this->assertFalse($this->cacheMutex->exists($this->event));
}

public function testOverlapsForRunningTask()
public function testOverlapsForRunningTaskOncePerMinutue()
{
$cacheMutex = $this->cacheMutex;
$this->cacheRepository->shouldReceive('has')->with($this->event->mutexName())->andReturn(true);
$this->cacheRepository->shouldReceive('has')->with($this->event->mutexName().$this->event->timestamp->format('Hi'))->never();

$this->cacheRepository->shouldReceive('has')->andReturn(true);
$this->assertTrue($this->cacheMutex->exists($this->event));
}

$event = new Event($this->cacheMutex, 'command');
public function testOverlapsForRunningTaskLongerThanMinute()
{
$this->cacheRepository->shouldReceive('has')->with($this->event->mutexName())->andReturn(false);
$this->cacheRepository->shouldReceive('has')->with($this->event->mutexName().$this->event->timestamp->format('Hi'))->andReturn(true);

$this->assertTrue($cacheMutex->exists($event));
$this->assertTrue($this->cacheMutex->exists($this->event));
}

public function testResetOverlap()
{
$cacheMutex = $this->cacheMutex;

$this->cacheRepository->shouldReceive('forget');

$event = new Event($this->cacheMutex, 'command');
$this->cacheRepository->shouldReceive('forget')->with($this->event->mutexName())->once();
$this->cacheRepository->shouldReceive('forget')->with($this->event->mutexName().$this->event->timestamp->format('Hi'))->never();

$cacheMutex->forget($event);
$this->cacheMutex->forget($this->event);
}
}