Permalink
Browse files

add conditional scheduler support

  • Loading branch information...
laurencei committed May 12, 2018
1 parent 65289a2 commit 3e5aec1b5c1268582339c384f5e97dfa4be688c6
@@ -44,9 +44,17 @@ public function update(Request $request, $id)
'alert_on_fail' => 'required|boolean',
'alert_run_time_greater_than' => 'required|numeric|min:0|max:9999',
'alert_run_time_less_than' => 'required|numeric|min:0|max:9999',
'alert_last_run_greater_than' => 'required|numeric|min:0|max:9999999',
]);
$scheduler->fill($request->only(['alert_on_missed', 'alert_on_fail', 'alert_run_time_greater_than', 'alert_run_time_less_than']));
$scheduler->fill($request->only([
'alert_on_missed',
'alert_on_fail',
'alert_run_time_greater_than',
'alert_run_time_less_than',
'alert_last_run_greater_than'
]));
$scheduler->save();
return redirect(route('eyewitness.schedulers.show', $scheduler->id).'#settings')->withSuccess('The scheduler configuration has been updated.');
@@ -29,7 +29,7 @@ public function poll()
$this->setNextSchedule($scheduler);
}
foreach ($this->getOverdueSchedules() as $history) {
foreach ($this->getOverdueNoExitSchedules() as $history) {
$history->output = "No finish ping received by Eyewitness";
$history->exitcode = 1;
$history->save();
@@ -38,6 +38,12 @@ public function poll()
$this->eye->notifier()->alert(new Overdue(['scheduler' => $history->scheduler]));
}
}
foreach ($this->getConditionalSchedules() as $scheduler) {
if ($this->hasNotRunInGivenTimeframe($scheduler)) {
$this->notifyIfChanges($scheduler);
}
}
}
/**
@@ -55,11 +61,21 @@ public function getMissedSchedules()
*
* @return \Eyewitness\Eye\Repo\History\Scheduler
*/
public function getOverdueSchedules()
public function getOverdueNoExitSchedules()
{
return History::whereNull('exitcode')->where('expected_completion', '<=', Carbon::now())->get();
}
/**
* Get a list of any schedulers that are making sure they are run at some point.
*
* @return \Eyewitness\Eye\Repo\History\Scheduler
*/
public function getConditionalSchedules()
{
return SchedulerRepo::where('alert_last_run_greater_than', '>', 0)->get();
}
/**
* Check if the status of the scheduler has changed, and send
* a notification accordingly if needed.
@@ -107,6 +123,17 @@ protected function setNextSchedule($scheduler)
$scheduler->save();
}
/**
* Check if the scheduled command has never run in a given timeframe.
*
* @param \Eyewitness\Eye\Repo\Scheduler $scheduler
* @return bool
*/
protected function hasNotRunInGivenTimeframe($scheduler)
{
return $scheduler->last_run->diffInSeconds() > $scheduler->alert_last_run_greater_than;
}
/**
* Handle the install process for the Scheduler.
*
@@ -36,6 +36,7 @@
'alert_on_fail' => 1,
'alert_run_time_greater_than' => 0,
'alert_run_time_less_than' => 0,
'alert_last_run_greater_than' => 0,
];
});
@@ -15,6 +15,7 @@ public function up()
{
Schema::connection(config('eyewitness.eyewitness_database_connection'))->table('eyewitness_io_schedulers', function (Blueprint $table) {
$table->timestamp('last_run')->nullable()->default(null)->index();
$table->integer('alert_last_run_greater_than')->default(0)->index();
});
}
@@ -27,6 +28,7 @@ public function down()
{
Schema::connection(config('eyewitness.eyewitness_database_connection'))->table('eyewitness_io_schedulers', function (Blueprint $table) {
$table->dropColumn('last_run');
$table->dropColumn('alert_last_run_greater_than');
});
}
}
@@ -88,6 +88,17 @@
</div>
</div>
<div class="flex md:items-center pb-4">
<div class="w-2/3 md:w-1/2 text-right">
<label class="text-grey-dark" for="alert_last_run_greater_than">Alert if job has never given in a given timeframe (seconds)</label>
</div>
<div class="w-1/3 md:w-1/2 text-left">
<div class="flex w-24 text-left">
<eye-input id="alert_last_run_greater_than" type="text" name="alert_last_run_greater_than" label="" value="{{ old('alert_last_run_greater_than', $scheduler->alert_last_run_greater_than) }}"></eye-input>
</div>
</div>
</div>
<div class="text-right mt-8">
<eye-btn color="bg-brand" type="submit" icon='@eyewitness_svg('check-square-11', 'svgcolor-white h-4 w-4')'>Update scheduler</eye-btn>
</div>
@@ -43,7 +43,8 @@ public function test_update_scheduler_validation()
'alert_on_missed' => true,
'alert_on_fail' => true,
'alert_run_time_greater_than' => 0,
'alert_run_time_less_than' => -1,]);
'alert_run_time_less_than' => -1,
'alert_last_run_greater_than' => 0]);
$response->assertStatus(302);
$response->assertSessionHas('errors');
@@ -54,15 +55,17 @@ public function test_update_scheduler_saves_data()
$scheduler = factory(Scheduler::class)->create(['alert_on_missed' => true,
'alert_on_fail' => true,
'alert_run_time_greater_than' => 0,
'alert_run_time_less_than' => 0]);
'alert_run_time_less_than' => 0,
'alert_last_run_greater_than' => 0]);
$response = $this->withSession(['eyewitness:auth' => 1])
->post(route('eyewitness.schedulers.update', $scheduler),
['_method' => 'put',
'alert_on_missed' => false,
'alert_on_fail' => false,
'alert_run_time_greater_than' => 1,
'alert_run_time_less_than' => 2]);
'alert_run_time_less_than' => 2,
'alert_last_run_greater_than' => 3]);
$response->assertRedirect(route('eyewitness.schedulers.show', $scheduler).'#settings');
$response->assertSessionHas('success');
@@ -72,7 +75,8 @@ public function test_update_scheduler_saves_data()
'alert_on_missed' => false,
'alert_on_fail' => false,
'alert_run_time_greater_than' => 1,
'alert_run_time_less_than' => 2]);
'alert_run_time_less_than' => 2,
'alert_last_run_greater_than' => 3]);
}
public function test_destroy_scheduler()
@@ -107,4 +107,46 @@ public function test_does_not_alert_if_disabled_for_overdue_scheduler()
$this->monitor->poll();
}
public function test_alerts_for_conditional_schedules()
{
$schedule = factory(Scheduler::class)->create(['next_run_due' => Carbon::now()->addDay(),
'next_check_due' => Carbon::now()->addDay(),
'last_run' => Carbon::now()->subDay(),
'alert_on_fail' => 1,
'healthy' => 1,
'alert_last_run_greater_than' => 10]);
$this->notifier->shouldReceive('alert')->with(Missed::class)->once();
$this->monitor->poll();
}
public function test_alerts_for_conditional_schedules_already_overdue()
{
$schedule = factory(Scheduler::class)->create(['next_run_due' => Carbon::now()->addDay(),
'next_check_due' => Carbon::now()->addDay(),
'last_run' => Carbon::now()->subDay(),
'alert_on_fail' => 1,
'healthy' => 0,
'alert_last_run_greater_than' => 10]);
$this->notifier->shouldReceive('alert')->with(Missed::class)->never();
$this->monitor->poll();
}
public function test_alerts_for_conditional_schedules_not_overdue()
{
$schedule = factory(Scheduler::class)->create(['next_run_due' => Carbon::now()->addDay(),
'next_check_due' => Carbon::now()->addDay(),
'last_run' => Carbon::now(),
'alert_on_fail' => 1,
'healthy' => 0,
'alert_last_run_greater_than' => 10]);
$this->notifier->shouldReceive('alert')->with(Missed::class)->never();
$this->monitor->poll();
}
}

0 comments on commit 3e5aec1

Please sign in to comment.