Implemented system heartbeat, to poll systems regularly that we havent heard from

This commit is contained in:
2023-11-26 13:10:23 +11:00
parent 6e7e09ab50
commit 1ac3583479
8 changed files with 343 additions and 90 deletions

View File

@@ -27,11 +27,13 @@ abstract class Protocol
protected const ERROR = -5;
// Our sessions Types
/** @deprecated use Mailer::class */
public const SESSION_AUTO = 0;
/** @deprecate Use mailers:class */
/** @deprecated Use mailers:class */
public const SESSION_EMSI = 1;
/** @deprecate Use mailers:class */
/** @deprecated Use mailers:class */
public const SESSION_BINKP = 2;
/** @deprecated Use mailers:class */
public const SESSION_ZMODEM = 3;
protected const MAX_PATH = 1024;

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Jobs\SystemHeartbeat as Job;
class SystemHeartbeat extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'system:heartbeat'
.' {--F|force : Force poll systems that are auto hold}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Poll systems that we havent seen for a while';
/**
* Execute the console command.
*/
public function handle()
{
Log::info('CSH:- Triggering heartbeat to systems');
Job::dispatchSync($this->option('force'));
}
}

View File

@@ -2,42 +2,44 @@
namespace App\Console;
use App\Jobs\MailSend;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Jobs\{MailSend,SystemHeartbeat};
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->job(new MailSend(TRUE))->everyMinute()->withoutOverlapping();
$schedule->job(new MailSend(FALSE))->twiceDaily(1,13);
}
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->job(new MailSend(TRUE))->everyMinute()->withoutOverlapping();
$schedule->job(new MailSend(FALSE))->twiceDaily(1,13);
$schedule->job(new SystemHeartbeat())->hourly();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
require base_path('routes/console.php');
}
}

View File

@@ -32,7 +32,7 @@ class SystemController extends Controller
$this->authorize('update',$o);
if ($request->post()) {
foreach (['name','location','sysop','hold','phone','address','port','active','method','notes','zt_id','pkt_type'] as $key)
foreach (['name','location','sysop','hold','phone','address','port','active','method','notes','zt_id','pkt_type','heartbeat'] as $key)
$o->{$key} = $request->post($key);
switch ($request->post('pollmode')) {

View File

@@ -56,7 +56,9 @@ class SystemRegister extends FormRequest
if ((! $request->isMethod('post')) || ($request->action === 'register'))
return [];
if ((! $this->so->exists) && ($request->action === 'create'))
$so = $this->route('o');
if ((! $so->exists) && ($request->action === 'create'))
return [
'name' => 'required|min:3',
];
@@ -65,7 +67,7 @@ class SystemRegister extends FormRequest
[
'name' => 'required|min:3',
],
($this->so->exists || ($request->action !== 'create')) ? [
($so->exists || ($request->action !== 'create')) ? [
'location' => 'required|min:3',
'sysop' => 'required|min:3',
'phone' => 'nullable|regex:/^([0-9-]+)$/',
@@ -74,13 +76,14 @@ class SystemRegister extends FormRequest
'method' => 'nullable|numeric',
'mailer_details.*' => 'nullable|array',
'mailer_details.*.port' => 'nullable|digits_between:2,5',
'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($this->so->exists ? $this->so->id : 0),
'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($so->exists ? $so->id : 0),
'pkt_type' => ['required',Rule::in(array_keys(Packet::PACKET_TYPES))],
] : [],
$this->so->exists ? [
$so->exists ? [
'active' => 'required|boolean',
'hold' => 'required|boolean',
'pollmode' => 'required|integer|min:0|max:2',
'heartbeat' => 'nullable|integer|min:0|max:48',
] : [],
));
}

View File

@@ -0,0 +1,91 @@
<?php
namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Repat\LaravelJobs\Job;
use App\Models\{Address,Setup};
class SystemHeartbeat #implements ShouldQueue
{
use Dispatchable;
private const LOGKEY = 'JSH';
/**
* @param bool $force Include systems that are autohold
*/
public function __construct(private ?bool $force=NULL) {}
/**
* Execute the job.
*/
public function handle(): void
{
// Our zones
$our_zones = Setup::findOrFail(config('app.id'))->system->addresses->pluck('zone_id')->unique();
// Find our uplinks that are hubs, NC, RC or ZCs
// Find any system that also has heartbeat configured
$l = Address::select(['addresses.id','addresses.zone_id','addresses.region_id','addresses.host_id','addresses.node_id','addresses.point_id','role','addresses.system_id'])
->distinct('systems.id')
->join('systems',['systems.id'=>'addresses.system_id'])
->join('system_zone',['system_zone.system_id'=>'systems.id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->where('systems.active',true)
->where('addresses.active',TRUE)
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->whereIN('addresses.zone_id',$our_zones)
->whereNotNull('pollmode')
->where(function($query) {
return $query
->where('role','<',Address::NODE_ACTIVE)
->orWhereNotNull('heartbeat');
})
->when(! $this->force,function($query) {
return $query
->where(function($query) {
return $query->whereNull('autohold')
->orWhere('autohold',FALSE);
});
})
->with(['system','zone.domain'])
->get();
// If we havent polled in heatbeat hours, poll system
foreach ($l as $oo) {
if (Job::where('queue','poll')->get()->pluck('command.address.id')->search($oo->id) === FALSE) {
if ((! $oo->system->last_session)
|| ($oo->system->hearbeat && ($oo->system->last_session->addHours($oo->system->heartbeat) < Carbon::now()))
|| ((! $oo->system->hearbeat) && ($oo->role < Address::NODE_ACTIVE) && ($oo->system->last_session->addHours(6) < Carbon::now())))
{
Log::info(sprintf('%s:- Polling [%s] (%s) - we havent seen them since [%s], heartbeat [%d]',
self::LOGKEY,
$oo->ftn,
$oo->system->name,
$oo->system->last_session ?: 'Never',
$oo->system->heartbeat,
));
AddressPoll::dispatch($oo);
} else {
Log::debug(sprintf('%s:= Not scheduling poll to [%s], we saw them [%s], heartbeat [%d]',
self::LOGKEY,
$oo->ftn,
$oo->system->last_session,
$oo->system->heartbeat
));
}
} else {
Log::notice(sprintf('%s:= Not scheduling poll to [%s], there is already one in the queue',self::LOGKEY,$oo->ftn));
}
}
}
}