clrghouz/app/Jobs/SystemHeartbeat.php
Deon George 3d8fef62c9
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 48s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 2m0s
Create Docker Image / Final Docker Image Manifest (push) Successful in 10s
Improvements to finding idle nodes, last_session actually only shows the last time the remote polled us
2024-09-15 22:00:40 +10:00

91 lines
2.7 KiB
PHP

<?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 = our_address()->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_NN)
->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_seen)
|| ($oo->system->hearbeat && ($oo->system->last_seen->addHours($oo->system->heartbeat) < Carbon::now()))
|| ((! $oo->system->hearbeat) && ($oo->role_id < Address::NODE_NN) && ($oo->system->last_seen->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_seen ?: '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_seen,
$oo->system->heartbeat
));
}
} else {
Log::notice(sprintf('%s:= Not scheduling poll to [%s], there is already one in the queue',self::LOGKEY,$oo->ftn));
}
}
}
}