2024-05-25 12:25:57 +00:00
< ? php
namespace App\Jobs ;
use Carbon\Carbon ;
use Illuminate\Bus\Queueable ;
use Illuminate\Contracts\Queue\ShouldQueue ;
use Illuminate\Database\Eloquent\Collection ;
use Illuminate\Foundation\Bus\Dispatchable ;
use Illuminate\Queue\InteractsWithQueue ;
use Illuminate\Queue\SerializesModels ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Log ;
use Illuminate\Support\Facades\Notification ;
2024-05-26 11:41:02 +00:00
use App\Classes\FTN\Message ;
2024-06-12 13:12:51 +00:00
use App\Models\ { Address , Domain , System };
2024-05-25 12:25:57 +00:00
use App\Notifications\Echomails\AbsentNodes ;
use App\Notifications\Emails\NodeMarkedDown as NodeMarkedDownEmail ;
use App\Notifications\Netmails\NodeMarkedDown as NodeMarkedDownNetmail ;
use App\Notifications\Emails\NodeMarkedHold as NodeMarkedHoldEmail ;
use App\Notifications\Netmails\NodeMarkedHold as NodeMarkedHoldNetmail ;
use App\Notifications\Emails\NodeDelisted as NodeDelistedEmail ;
use App\Notifications\Netmails\NodeDelisted as NodeDelistedNetmail ;
class AddressIdle implements ShouldQueue
{
use Dispatchable , InteractsWithQueue , Queueable , SerializesModels ;
private const LOGKEY = 'JAI' ;
private ? Address $ao ; // System address
private Domain $do ; // Domain we are processing
/**
* Create a new job instance .
*/
public function __construct ( Domain $do , Address $ao = NULL )
{
2024-11-02 13:16:48 +00:00
$this -> do = $do -> withoutRelations ();
$this -> ao = $ao ? -> withoutRelations ();
2024-05-25 12:25:57 +00:00
}
/**
* Execute the job .
*/
public function handle () : void
{
$result = collect ();
// Delist DOWN nodes
2024-09-22 07:33:08 +00:00
if ( config ( 'fido.idle.delist' )) {
$age = Carbon :: now () -> subDays ( config ( 'fido.idle.delist' ));
foreach ( $this -> old ( $this -> do , config ( 'fido.idle.delist' ), Address :: NODE_DOWN , $this -> ao ) as $ao ) {
2024-10-19 22:39:14 +00:00
Log :: debug ( sprintf ( '%s:- Evaluating DOWN node [%s], not seen for at least [%d] days, last update [%d] days' , self :: LOGKEY , $ao -> ftn , $ao -> system -> last_seen ? -> diffInDays (), $ao -> updated_at -> diffInDays ()));
2024-09-22 07:33:08 +00:00
// Only delist system that has been marked down
// Only delist them if its been 14 days since they were marked DOWN
if (( ! $ao -> is_down ) || ( $ao -> updated_at -> greaterThan ( Carbon :: now () -> subWeeks ( 2 ))))
continue ;
// Validate that the last seen was infact that long ago
2024-09-29 03:16:23 +00:00
if ( $ao -> system -> last_seen && $ao -> system -> last_seen -> greaterThan ( $age ))
2024-09-22 07:33:08 +00:00
continue ;
2024-10-19 22:39:14 +00:00
Log :: info ( sprintf ( '%s:- Delisting [%s], not seen for at least [%d] days' , self :: LOGKEY , $ao -> ftn , $ao -> system -> last_seen ? -> diffInDays ()));
2024-09-22 07:33:08 +00:00
$contact = FALSE ;
// Remove subscribed echoareas
$ao -> echoareas () -> detach ();
// Remove subscribed fileareas
$ao -> fileareas () -> detach ();
$ao -> active = FALSE ;
$ao -> validated = FALSE ;
$ao -> save ();
2024-11-04 04:58:20 +00:00
// Clear the queue
AddressClearQueue :: dispatchSync ( $ao );
2024-09-22 07:33:08 +00:00
// Email Alert
if ( $ao -> system -> users -> count ()) {
Notification :: send ( $ao -> system -> users , new NodeDelistedEmail ( $ao -> withoutRelations ()));
$contact = TRUE ;
}
// Netmail Alert (to othernet network address)
2024-11-03 22:05:27 +00:00
if ( $ao -> system -> aka_unknown () -> count ()) {
Notification :: route ( 'netmail' , $ao -> system -> aka_unknown () -> first () -> withoutRelations ()) -> notify ( new NodeDelistedNetmail ( $ao -> withoutRelations ()));
2024-09-22 07:33:08 +00:00
$contact = TRUE ;
}
$ao -> contacted = $contact ;
$result -> push ( $ao );
2024-05-25 12:25:57 +00:00
}
}
// Mark nodes DOWN
2024-09-22 07:33:08 +00:00
if ( config ( 'fido.idle.down' )) {
$age = Carbon :: now () -> subDays ( config ( 'fido.idle.down' ));
foreach ( $this -> old ( $this -> do , config ( 'fido.idle.down' ), Address :: NODE_HOLD , $this -> ao ) as $ao ) {
2024-10-19 22:39:14 +00:00
Log :: debug ( sprintf ( '%s:- Evaluating HOLD node [%s], not seen for at least [%d] days, last update [%d] days' , self :: LOGKEY , $ao -> ftn , $ao -> system -> last_seen ? -> diffInDays (), $ao -> updated_at -> diffInDays ()));
2024-09-22 07:33:08 +00:00
// Only mark down system that has been marked down
// Only mark down them if its been 14 days since they were marked HOLD
if (( ! $ao -> is_hold ) || ( $ao -> updated_at -> greaterThan ( Carbon :: now () -> subWeeks ( 2 ))))
continue ;
// Validate that the last seen was infact that long ago
2024-09-29 03:16:23 +00:00
if ( $ao -> system -> last_seen && $ao -> system -> last_seen -> greaterThan ( $age ))
2024-09-22 07:33:08 +00:00
continue ;
2024-10-19 22:39:14 +00:00
Log :: info ( sprintf ( '%s:- Marking [%s] as DOWN, not seen for at least [%d] days' , self :: LOGKEY , $ao -> ftn , $ao -> system -> last_seen ? -> diffInDays ()));
2024-09-22 07:33:08 +00:00
$contact = FALSE ;
// Email Alert
if ( $ao -> system -> users -> count ()) {
Notification :: send ( $ao -> system -> users , new NodeMarkedDownEmail ( $ao -> withoutRelations ()));
$contact = TRUE ;
}
// Netmail Alert (to othernet network address)
2024-11-03 22:05:27 +00:00
if ( $ao -> system -> aka_unknown () -> count ()) {
Notification :: route ( 'netmail' , $ao -> system -> aka_unknown () -> first () -> withoutRelations ()) -> notify ( new NodeMarkedDownNetmail ( $ao -> withoutRelations ()));
2024-09-22 07:33:08 +00:00
$contact = TRUE ;
}
// Mark as DOWN
$ao -> role &= ~ Address :: NODE_HOLD ;
$ao -> role |= Address :: NODE_DOWN ;
$ao -> save ();
$ao -> contacted = $contact ;
$result -> push ( $ao );
2024-05-25 12:25:57 +00:00
}
}
2024-09-08 10:39:10 +00:00
// @todo Make sure we only process addresses that we are responsible for, eg: 1/999 shouldnt have been processed even though 3/999 as eligible (and they are were connected to the same system)
2024-05-25 12:25:57 +00:00
// Mark nodes as HOLD
2024-09-22 07:33:08 +00:00
if ( config ( 'fido.idle.hold' )) {
$age = Carbon :: now () -> subDays ( config ( 'fido.idle.hold' ));
foreach ( $this -> old ( $this -> do , config ( 'fido.idle.hold' ), Address :: NODE_ALL , $this -> ao ) as $ao ) {
2024-10-19 22:39:14 +00:00
Log :: debug ( sprintf ( '%s:- Evaluating IDLE node [%s], not seen for at least [%d] days' , self :: LOGKEY , $ao -> ftn , $ao -> system -> last_seen ? -> diffInDays ()));
2024-09-22 07:33:08 +00:00
// Ignore any systems that are a Discoverd System
if ( $ao -> system -> name === System :: default ) {
Log :: alert ( sprintf ( '%s:! Ignoring HOLD for discovered System [%s]' , self :: LOGKEY , $ao -> ftn ));
continue ;
}
// Ignore any systems already marked hold or down
if ( $ao -> role & ( Address :: NODE_DOWN | Address :: NODE_HOLD ))
continue ;
// Validate that the last seen was infact that long ago
2024-09-29 03:16:23 +00:00
if ( $ao -> system -> last_seen && $ao -> system -> last_seen -> greaterThan ( $age ))
2024-09-22 07:33:08 +00:00
continue ;
$contact = FALSE ;
2024-10-19 22:39:14 +00:00
Log :: info ( sprintf ( '%s:- Marking [%s] as HOLD, not seen for at least [%d] days' , self :: LOGKEY , $ao -> ftn , $ao -> system -> last_seen ? -> diffInDays ()));
2024-09-22 07:33:08 +00:00
// Email Alert
if ( $ao -> system -> users -> count ()) {
Notification :: send ( $ao -> system -> users , new NodeMarkedHoldEmail ( $ao -> withoutRelations ()));
$contact = TRUE ;
}
// Netmail Alert (to othernet network address)
2024-11-03 22:05:27 +00:00
if ( $ao -> system -> aka_unknown () -> count ()) {
Notification :: route ( 'netmail' , $ao -> system -> aka_unknown () -> first () -> withoutRelations ()) -> notify ( new NodeMarkedHoldNetmail ( $ao -> withoutRelations ()));
2024-09-22 07:33:08 +00:00
$contact = TRUE ;
}
// Mark as DOWN
$ao -> role |= Address :: NODE_HOLD ;
$ao -> save ();
$ao -> contacted = $contact ;
$result -> push ( $ao );
2024-06-12 13:12:51 +00:00
}
2024-05-25 12:25:57 +00:00
}
if ( $result -> count ())
2024-11-04 13:28:34 +00:00
Notification :: route ( 'echomail' , $this -> do -> nodestatus_echoarea ) -> notify ( new AbsentNodes ( $result ));
2024-05-25 12:25:57 +00:00
}
private function old ( Domain $do , int $days , int $flags = 0 , Address $ao = NULL ) : Collection
{
2024-09-22 07:33:08 +00:00
// Ignore dates that are zero
if ( ! $days )
return collect ();
2024-05-25 12:25:57 +00:00
$age = Carbon :: now () -> subDays ( $days ) -> endOfDay ();
2024-11-06 11:44:21 +00:00
$ours = our_address ( $do ) -> pluck ( 'ftn' );
2024-05-25 12:25:57 +00:00
2024-11-03 22:05:27 +00:00
return Address :: FTN ()
-> ActiveFTN ()
-> addSelect ([ 'addresses.updated_at' ])
2024-09-09 10:16:14 +00:00
-> where ( fn ( $query ) => $query -> where ( 'point_id' , 0 ) -> orWhereNull ( 'point_id' ))
-> whereIn ( 'addresses.id' , our_nodes ( $do ) -> pluck ( 'id' ))
2024-05-25 12:25:57 +00:00
-> when ( $ao , fn ( $query ) => $query -> where ( 'addresses.id' , $ao -> id ))
2024-09-09 10:16:14 +00:00
-> where ( fn ( $q ) => $q -> where ( 'last_session' , '<' , $age ) -> orWhereNull ( 'last_session' ))
-> whereRaw ( sprintf ( '((role IS NULL) OR (role=0) OR ((role & %d) > 0))' , $flags ))
2024-10-09 03:12:19 +00:00
-> whereRaw ( sprintf ( '((role IS NULL) OR ((role & %d) = 0))' , Address :: NODE_KEEP ))
2024-09-09 10:16:14 +00:00
-> join ( 'systems' ,[ 'systems.id' => 'addresses.system_id' ])
2024-11-03 22:05:27 +00:00
//->with(['system','zone.domain'])
2024-11-06 11:44:21 +00:00
-> get ()
-> filter ( fn ( $item ) => $ours -> contains ( $item -> parent () ? -> ftn ));
2024-05-25 12:25:57 +00:00
}
}