Skip to content

Commit

Permalink
Adding ability to check the laravel schedule is running
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinj committed Dec 9, 2017
1 parent 9a53bc1 commit 657d873
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 14 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
Scrutiny helps your laravel project ensure that its current server
environment is configured and running as planned.

## Installation

To install through composer, add the following to your `composer.json` file:

```json
{
"require": {
"brightmachine/scrutiny": "~1.0"
}
}
```

And then run `composer install` from the terminal.

### Quick Installation

The installation intructions can be simplified using the following:

composer require "brightmachine/scrutiny=~1.0"

## How it works

1. In `AppServiceProvider::boot()`, configure the probes to check for all the things your environment needs in order to run
Expand All @@ -21,11 +41,9 @@ All probes fall under the namespace `Scrutiny\Probes`:
- `ExecutableIsInstalled`
- `PhpExtensionLoaded`
- `QueueIsRunning`

- `ScheduleIsRunning`
- `HasNoFailedJobs`

Each check has its own parameters and can be used multiple times.
Each check has its own parameters and can be used multiple times where it makes sense.

Some system checks may not be supported on Windows.

Expand Down Expand Up @@ -112,4 +130,4 @@ This is the spirit in which contributions will be considered.
2. Tell us why/how your PR will benefit the project
3. We may ask you for clarification, but we'll quickly let you know whether or not it's likely your change will be merged

Xx
😘 Xx
21 changes: 21 additions & 0 deletions src/ProbeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Scrutiny\Probes\ExecutableIsInstalled;
use Scrutiny\Probes\PhpExtensionLoaded;
use Scrutiny\Probes\QueueIsRunning;
use Scrutiny\Probes\ScheduleIsRunning;

class ProbeManager
{
Expand Down Expand Up @@ -111,6 +112,26 @@ public function queueIsRunning($maxHandleTime = 300, $queue = null, $connection
return $this;
}

/**
* @return $this
*/
public function scheduleIsRunning()
{
static $added;

if ($added) {
return $this;
}

$this->probes->push(
new ScheduleIsRunning()
);

$added = true;

return $this;
}

/**
* @param string $probeName
* @param callable $callback
Expand Down
21 changes: 11 additions & 10 deletions src/Probes/QueueIsRunning.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class QueueIsRunning implements Probe
*/
protected $queue;

/**
* @var null
*/
protected $connection;

/**
* @var string|null
*/
Expand All @@ -39,10 +44,6 @@ class QueueIsRunning implements Probe
* @var Repository
*/
protected $cacheStore;
/**
* @var null
*/
private $connection;

public function __construct($maxHandleTime = 300, $queue = null, $connection = null)
{
Expand Down Expand Up @@ -70,12 +71,12 @@ public function name($identifier = null)
if (!$this->queue && !$this->connection) {
$defaultIdentifier = 'default';
} else {
$queue = $this->queue ?: 'default';
$defaultIdentifier = "$queue queue";

if ($this->connection) {
$defaultIdentifier .= " on {$this->connection}";
}
// we have a configured queue or connection
$defaultIdentifier = collect()
->push($this->queue ? "{$this->queue} queue": null)
->push($this->connection ? "{$this->connection} connection": null)
->filter()
->implode(' on ');
}

return sprintf("Queue is Running: %s", $this->nameIdentifier ?: $defaultIdentifier);
Expand Down
110 changes: 110 additions & 0 deletions src/Probes/ScheduleIsRunning.php
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');
}
}
74 changes: 74 additions & 0 deletions tests/Probes/ScheduleIsRunningTest.php
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;
}
}

0 comments on commit 657d873

Please sign in to comment.