-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ability to check the laravel schedule is running
- Loading branch information
Showing
5 changed files
with
238 additions
and
14 deletions.
There are no files selected for viewing
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,110 @@ | ||
<?php | ||
|
||
namespace Scrutiny\Probes; | ||
|
||
use Illuminate\Cache\Repository; | ||
use Illuminate\Foundation\Bus\DispatchesJobs; | ||
use Illuminate\Support\Facades\Cache; | ||
use Scrutiny\Probe; | ||
use Scrutiny\ProbeSkippedException; | ||
|
||
class ScheduleIsRunning implements Probe | ||
{ | ||
use DispatchesJobs; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
protected $nameIdentifier; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $cacheKey; | ||
|
||
public function __construct() | ||
{ | ||
$this->cacheKey = class_basename($this); | ||
$this->registerScheduleCallback(); | ||
} | ||
|
||
public function id() | ||
{ | ||
if ($this->nameIdentifier) { | ||
return $this->name(); | ||
} | ||
|
||
return sprintf("probe:%s", class_basename($this)); | ||
} | ||
|
||
public function name($identifier = null) | ||
{ | ||
if ($identifier) { | ||
$this->nameIdentifier = $identifier; | ||
} | ||
|
||
return 'Schedule is Running'; | ||
} | ||
|
||
public function check() | ||
{ | ||
$lastRunTime = $this->lastRunTime(); | ||
|
||
if ($lastRunTime === null) { | ||
$this->recordLastRunTime(0); | ||
throw new ProbeSkippedException('Initiated schedule probe'); | ||
} | ||
|
||
if ((time() - $lastRunTime) >= 90) { | ||
$message = $lastRunTime == 0 ? 'has never run' : 'last ran at '.date('Y-m-d H:i:s', $lastRunTime); | ||
throw new \Exception($message); | ||
} | ||
} | ||
|
||
protected function lastRunTime() | ||
{ | ||
$cacheStore = $this->getCacheStore(); | ||
|
||
if (!$cacheStore->has($this->cacheKey)) { | ||
return null; | ||
} | ||
|
||
return $cacheStore->get($this->cacheKey); | ||
} | ||
|
||
protected function registerScheduleCallback() | ||
{ | ||
$app = app(); | ||
|
||
if (!$app->runningInConsole()) { | ||
return; | ||
} | ||
|
||
$app->booted(function () { | ||
/** @var \Illuminate\Console\Scheduling\Schedule $schedule */ | ||
$schedule = app('Illuminate\Console\Scheduling\Schedule'); | ||
|
||
$schedule->call(function () { | ||
$this->recordLastRunTime(time()); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @param int $time | ||
*/ | ||
protected function recordLastRunTime($time) | ||
{ | ||
$cacheStore = $this->getCacheStore(); | ||
$cacheStore->forget($this->cacheKey); | ||
$cacheStore->forever($this->cacheKey, $time); | ||
} | ||
|
||
/** | ||
* @return Repository | ||
*/ | ||
protected function getCacheStore() | ||
{ | ||
return Cache::store('scrutiny-file'); | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
namespace ScrutinyTest\Probes; | ||
|
||
use Scrutiny\Measurements\Duration; | ||
use Scrutiny\Probes\QueueIsRunning; | ||
use Scrutiny\Probes\QueueIsRunning\QueueIsRunningJob; | ||
use Scrutiny\Probes\ScheduleIsRunning; | ||
use ScrutinyTest\TestCase; | ||
|
||
class ScheduleIsRunningTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @expectedException \Scrutiny\ProbeSkippedException | ||
* @expectedExceptionMessage Initiated schedule probe | ||
*/ | ||
public function skipsIfFirstRun() | ||
{ | ||
$check = new ConfigurableScheduleIsRunning(); | ||
$check->check(); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage has never run | ||
*/ | ||
public function failsIfNothingRecordedSinceFirstRun() | ||
{ | ||
$check = new ConfigurableScheduleIsRunning(); | ||
$check->lastRunTime = 0; | ||
$check->check(); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage last ran at | ||
*/ | ||
public function failsIfScheduleLastRunMoreThan90SecondsAgo() | ||
{ | ||
$check = new ConfigurableScheduleIsRunning(); | ||
$check->lastRunTime = time() - 91; | ||
$check->check(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function passesIfCompletedJobUnderTheThreshold() | ||
{ | ||
$check = new ConfigurableScheduleIsRunning(); | ||
$check->lastRunTime = time() - 60; | ||
$check->check(); | ||
|
||
$this->assertTrue(true); | ||
} | ||
} | ||
|
||
class ConfigurableScheduleIsRunning extends ScheduleIsRunning | ||
{ | ||
public $lastRunTime; | ||
|
||
protected function lastRunTime() | ||
{ | ||
return $this->lastRunTime; | ||
} | ||
|
||
protected function recordLastRunTime($time) | ||
{ | ||
return $this->lastRunTime = $time; | ||
} | ||
} |