-
Notifications
You must be signed in to change notification settings - Fork 186
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
Heartbeats #21
Merged
Merged
Heartbeats #21
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
89db2f5
Starting to add heartbeat model
REBELinBLUE f238091
Adding heartbeat listing
REBELinBLUE ccdb31d
Heartbeat management
REBELinBLUE 996dd19
Added a cronjob to check the heartbeat
REBELinBLUE 91d8fb0
Sending notifications when heartbeats fail
REBELinBLUE ffdc522
Cleanup PHPCI complains
REBELinBLUE 574ef46
Testing heartbeats
2cf22ed
Added unique constraint
REBELinBLUE 521bff5
Adding heartbeat seeder
REBELinBLUE cb8317e
Tweaking text
REBELinBLUE 4ea3c29
Changing alert to alert every time the heartbeat is missed
REBELinBLUE 93f97fa
Adding intervals
REBELinBLUE 9ceedc5
Formatting dates
REBELinBLUE dc853c1
Cleanup
REBELinBLUE 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
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,60 @@ | ||
<?php namespace App\Console\Commands; | ||
|
||
use Queue; | ||
use Carbon\Carbon; | ||
use App\Heartbeat; | ||
use App\Commands\Notify; | ||
use Illuminate\Console\Command; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
/** | ||
* Checks that any expected heartbeats have checked-in | ||
*/ | ||
class CheckHeartbeats extends Command | ||
{ | ||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'heartbeat:check'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Checks that any expected heartbeats have checked-in'; | ||
|
||
/** | ||
* Checks that heartbeats happened as expected | ||
* | ||
* @return mixed | ||
*/ | ||
public function fire() | ||
{ | ||
$heartbeats = Heartbeat::all(); | ||
|
||
foreach ($heartbeats as $heartbeat) { | ||
$last_heard_from = $heartbeat->last_activity; | ||
if ($heartbeat->status === Heartbeat::UNTESTED) { | ||
$last_heard_from = $heartbeat->created_at; | ||
} | ||
|
||
$missed = $heartbeat->missed + 1; | ||
|
||
$next_time = $last_heard_from->addMinutes($heartbeat->interval * $missed); | ||
|
||
if (Carbon::now()->gt($next_time)) { | ||
$heartbeat->status = Heartbeat::MISSING; | ||
$heartbeat->missed = $missed; | ||
$heartbeat->save(); | ||
|
||
foreach ($heartbeat->project->notifications as $notification) { | ||
Queue::pushOn('notify', new Notify($notification, $heartbeat->notificationPayload())); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,159 @@ | ||
<?php namespace App; | ||
|
||
use Lang; | ||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* Heartbeat model | ||
*/ | ||
class Heartbeat extends Model | ||
{ | ||
use SoftDeletes; | ||
|
||
const OK = 0; | ||
const UNTESTED = 1; | ||
const MISSING = 2; | ||
|
||
/** | ||
* The attributes excluded from the model's JSON form. | ||
* | ||
* @var array | ||
*/ | ||
protected $hidden = ['project_id', 'created_at', 'updated_at', 'deleted_at', 'pivot']; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = ['name', 'interval', 'project_id']; | ||
|
||
/** | ||
* The fields which should be tried as Carbon instances | ||
* | ||
* @var array | ||
*/ | ||
protected $dates = ['last_activity']; | ||
|
||
/** | ||
* Additional attributes to include in the JSON representation | ||
* | ||
* @var array | ||
*/ | ||
protected $appends = ['callback_url']; | ||
|
||
/** | ||
* The attributes that should be casted to native types. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'status' => 'integer', | ||
'deploy_code' => 'boolean' | ||
]; | ||
|
||
/** | ||
* Belongs to relationship | ||
* | ||
* @return Project | ||
*/ | ||
public function project() | ||
{ | ||
return $this->belongsTo('App\Project'); | ||
} | ||
|
||
/** | ||
* Override the boot method to bind model event listeners | ||
* | ||
* @return void | ||
*/ | ||
public static function boot() | ||
{ | ||
parent::boot(); | ||
|
||
// When first creating the model generate a webhook hash | ||
static::creating(function ($model) { | ||
if (!array_key_exists('hash', $model->attributes)) { | ||
$model->generateHash(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Generates a hash for use in the webhook URL | ||
* | ||
* @return void | ||
*/ | ||
public function generateHash() | ||
{ | ||
$this->attributes['hash'] = Str::quickRandom(30); | ||
} | ||
|
||
/** | ||
* Define a mutator for the callback URL | ||
* | ||
* @return string | ||
* @todo Shouldn't this be a presenter? | ||
*/ | ||
public function getCallbackUrlAttribute() | ||
{ | ||
return route('heartbeat', $this->hash); | ||
} | ||
|
||
/** | ||
* Updates the last_activity timestamp | ||
* | ||
* @return boolean | ||
*/ | ||
public function pinged() | ||
{ | ||
$this->status = self::OK; | ||
$this->missed = 0; | ||
$this->last_activity = $this->freshTimestamp(); | ||
|
||
return $this->save(); | ||
} | ||
|
||
/** | ||
* Generates a slack payload for the heartbeat failuyre | ||
* | ||
* @return array | ||
*/ | ||
public function notificationPayload() | ||
{ | ||
$message = Lang::get('heartbeats.message', [ 'job' => $this->name ]); | ||
|
||
if (is_null($this->last_activity)) { | ||
$heard_from = Lang::get('app.never'); | ||
} | ||
else { | ||
$heard_from = $this->last_activity->diffForHumans(); | ||
} | ||
|
||
$payload = [ | ||
'attachments' => [ | ||
[ | ||
'fallback' => $message, | ||
'text' => $message, | ||
'color' => 'danger', | ||
'fields' => [ | ||
[ | ||
'title' => Lang::get('notifications.project'), | ||
'value' => sprintf('<%s|%s>', url('project', $this->project_id), $this->project->name), | ||
'short' => true | ||
], [ | ||
'title' => Lang::get('heartbeats.last_check_in'), | ||
'value' => $heard_from, | ||
'short' => true | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
|
||
return $payload; | ||
} | ||
} |
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 App\Http\Controllers; | ||
|
||
use App\Heartbeat; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\StoreHeartbeatRequest; | ||
|
||
/** | ||
* Controller for managing notifications | ||
*/ | ||
class HeartbeatController extends Controller | ||
{ | ||
/** | ||
* Handles the callback URL for the heartbeat | ||
* | ||
* @param string $hash The webhook hash | ||
* @return Response | ||
*/ | ||
public function ping($hash) | ||
{ | ||
$heartbeat = Heartbeat::where('hash', $hash) | ||
->firstOrFail(); | ||
|
||
$heartbeat->pinged(); | ||
|
||
return 'OK'; | ||
} | ||
|
||
/** | ||
* Store a newly created heartbeat in storage. | ||
* | ||
* @param StoreHeartbeatRequest $request | ||
* @return Response | ||
*/ | ||
public function store(StoreHeartbeatRequest $request) | ||
{ | ||
return Heartbeat::create($request->only( | ||
'name', | ||
'interval', | ||
'project_id' | ||
)); | ||
} | ||
|
||
/** | ||
* Update the specified heartbeat in storage. | ||
* | ||
* @param Heartbeat $heartbeat | ||
* @param StoreHeartbeatRequest $request | ||
* @return Response | ||
*/ | ||
public function update(Heartbeat $heartbeat, StoreHeartbeatRequest $request) | ||
{ | ||
$heartbeat->update($request->only( | ||
'name', | ||
'interval' | ||
)); | ||
|
||
return $heartbeat; | ||
} | ||
|
||
/** | ||
* Remove the specified heartbeat from storage. | ||
* | ||
* @param Heartbeat $heartbeat | ||
* @return Response | ||
*/ | ||
public function destroy(Heartbeat $heartbeat) | ||
{ | ||
$heartbeat->delete(); | ||
|
||
return [ | ||
'success' => true | ||
]; | ||
} | ||
} |
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,22 @@ | ||
<?php namespace App\Http\Requests; | ||
|
||
use App\Http\Requests\Request; | ||
|
||
/** | ||
* Request for validating heartbeats | ||
*/ | ||
class StoreHeartbeatRequest extends Request | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => 'required|max:255', | ||
'project_id' => 'required|integer|exists:projects,id' | ||
]; | ||
} | ||
} |
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
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.
PHPCS: Expected 1 space after closing brace; newline found