From 333e7e773dc1f83cd381c85b1f35c58b794d470e Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 22 Sep 2024 17:33:08 +1000 Subject: [PATCH] More work on finding idle nodes --- app/Jobs/AddressIdle.php | 230 ++++++++++++++++++++++----------------- 1 file changed, 129 insertions(+), 101 deletions(-) diff --git a/app/Jobs/AddressIdle.php b/app/Jobs/AddressIdle.php index c57f54e..f3683f2 100644 --- a/app/Jobs/AddressIdle.php +++ b/app/Jobs/AddressIdle.php @@ -49,127 +49,151 @@ class AddressIdle implements ShouldQueue $result = collect(); // Delist DOWN nodes - foreach ($this->old($this->do,config('fido.idle.delist'),Address::NODE_DOWN,$this->ao) as $ao) { - // 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; + if (config('fido.idle.delist')) { + $age = Carbon::now()->subDays(config('fido.idle.delist')); - Log::info(sprintf('%s:- Delisting [%s], not seen for [%d] days',self::LOGKEY,$ao->ftn,config('fido.idle.delist'))); - $contact = FALSE; + foreach ($this->old($this->do,config('fido.idle.delist'),Address::NODE_DOWN,$this->ao) as $ao) { + // 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; - // Remove echomail not collected from echomail_seenby - DB::table('echomail_seenby') - ->where('address_id',$ao->id) - ->whereNotNull('export_at') - ->whereNull('sent_at') - ->delete(); + // Validate that the last seen was infact that long ago + if ($ao->system->last_seen->greaterThan($age)) + continue; - // Remove FLAG_INTRANSIT from netmail that hasnt been delivered - DB::table('netmails') - ->where('tftn_id',$ao->id) - ->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT)) - ->update(['flags'=>DB::raw(sprintf('(flags & ~%d)',Message::FLAG_INTRANSIT))]); + Log::info(sprintf('%s:- Delisting [%s], not seen for [%d] days',self::LOGKEY,$ao->ftn,config('fido.idle.delist'))); + $contact = FALSE; - // Remove files not collected - DB::table('file_seenby') - ->where('address_id',$ao->id) - ->whereNotNull('export_at') - ->whereNull('sent_at') - ->delete(); + // Remove echomail not collected from echomail_seenby + DB::table('echomail_seenby') + ->where('address_id',$ao->id) + ->whereNotNull('export_at') + ->whereNull('sent_at') + ->delete(); - // Remove subscribed echoareas - $ao->echoareas()->detach(); + // Remove FLAG_INTRANSIT from netmail that hasnt been delivered + DB::table('netmails') + ->where('tftn_id',$ao->id) + ->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT)) + ->update(['flags'=>DB::raw(sprintf('(flags & ~%d)',Message::FLAG_INTRANSIT))]); - // Remove subscribed fileareas - $ao->fileareas()->detach(); + // Remove files not collected + DB::table('file_seenby') + ->where('address_id',$ao->id) + ->whereNotNull('export_at') + ->whereNull('sent_at') + ->delete(); - $ao->active = FALSE; - $ao->validated = FALSE; - $ao->save(); + // Remove subscribed echoareas + $ao->echoareas()->detach(); - // Email Alert - if ($ao->system->users->count()) { - Notification::send($ao->system->users,new NodeDelistedEmail($ao->withoutRelations())); - $contact = TRUE; + // Remove subscribed fileareas + $ao->fileareas()->detach(); + + $ao->active = FALSE; + $ao->validated = FALSE; + $ao->save(); + + // Email Alert + if ($ao->system->users->count()) { + Notification::send($ao->system->users,new NodeDelistedEmail($ao->withoutRelations())); + $contact = TRUE; + } + + // Netmail Alert (to othernet network address) + if ($ao->system->uncommon()->count()) { + Notification::route('netmail',$ao->system->uncommon()->first()->withoutRelations())->notify(new NodeDelistedNetmail($ao->withoutRelations())); + $contact = TRUE; + } + + $ao->contacted = $contact; + $result->push($ao); } - - // Netmail Alert (to othernet network address) - if ($ao->system->uncommon()->count()) { - Notification::route('netmail',$ao->system->uncommon()->first()->withoutRelations())->notify(new NodeDelistedNetmail($ao->withoutRelations())); - $contact = TRUE; - } - - $ao->contacted = $contact; - $result->push($ao); } // Mark nodes DOWN - foreach ($this->old($this->do,config('fido.idle.down'),Address::NODE_HOLD,$this->ao) as $ao) { - // 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; + if (config('fido.idle.down')) { + $age = Carbon::now()->subDays(config('fido.idle.down')); - Log::info(sprintf('%s:- Marking [%s] as DOWN, not seen for [%d] days',self::LOGKEY,$ao->ftn,config('fido.idle.down'))); - $contact = FALSE; + foreach ($this->old($this->do,config('fido.idle.down'),Address::NODE_HOLD,$this->ao) as $ao) { + // 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; - // Email Alert - if ($ao->system->users->count()) { - Notification::send($ao->system->users,new NodeMarkedDownEmail($ao->withoutRelations())); - $contact = TRUE; + // Validate that the last seen was infact that long ago + if ($ao->system->last_seen->greaterThan($age)) + continue; + + Log::info(sprintf('%s:- Marking [%s] as DOWN, not seen for [%d] days',self::LOGKEY,$ao->ftn,config('fido.idle.down'))); + $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) + if ($ao->system->uncommon()->count()) { + Notification::route('netmail',$ao->system->uncommon()->first()->withoutRelations())->notify(new NodeMarkedDownNetmail($ao->withoutRelations())); + $contact = TRUE; + } + + // Mark as DOWN + $ao->role &= ~Address::NODE_HOLD; + $ao->role |= Address::NODE_DOWN; + $ao->save(); + + $ao->contacted = $contact; + $result->push($ao); } - - // Netmail Alert (to othernet network address) - if ($ao->system->uncommon()->count()) { - Notification::route('netmail',$ao->system->uncommon()->first()->withoutRelations())->notify(new NodeMarkedDownNetmail($ao->withoutRelations())); - $contact = TRUE; - } - - // Mark as DOWN - $ao->role &= ~Address::NODE_HOLD; - $ao->role |= Address::NODE_DOWN; - $ao->save(); - - $ao->contacted = $contact; - $result->push($ao); } // @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) // Mark nodes as HOLD - foreach ($this->old($this->do,config('fido.idle.hold'),Address::NODE_ALL,$this->ao) as $ao) { - // 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; + 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) { + // 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 + if ($ao->system->last_seen->greaterThan($age)) + continue; + + $contact = FALSE; + + Log::info(sprintf('%s:- Marking [%s] as HOLD, not seen for [%d] days',self::LOGKEY,$ao->ftn,config('fido.idle.hold'))); + + // Email Alert + if ($ao->system->users->count()) { + Notification::send($ao->system->users,new NodeMarkedHoldEmail($ao->withoutRelations())); + $contact = TRUE; + } + + // Netmail Alert (to othernet network address) + if ($ao->system->uncommon()->count()) { + Notification::route('netmail',$ao->system->uncommon()->first()->withoutRelations())->notify(new NodeMarkedHoldNetmail($ao->withoutRelations())); + $contact = TRUE; + } + + // Mark as DOWN + $ao->role |= Address::NODE_HOLD; + $ao->save(); + + $ao->contacted = $contact; + $result->push($ao); } - - // Ignore any systems already marked hold or down - if ($ao->role & (Address::NODE_DOWN|Address::NODE_HOLD)) - continue; - - $contact = FALSE; - - Log::info(sprintf('%s:- Marking [%s] as HOLD, not seen for [%d] days',self::LOGKEY,$ao->ftn,config('fido.idle.hold'))); - - // Email Alert - if ($ao->system->users->count()) { - Notification::send($ao->system->users,new NodeMarkedHoldEmail($ao->withoutRelations())); - $contact = TRUE; - } - - // Netmail Alert (to othernet network address) - if ($ao->system->uncommon()->count()) { - Notification::route('netmail',$ao->system->uncommon()->first()->withoutRelations())->notify(new NodeMarkedHoldNetmail($ao->withoutRelations())); - $contact = TRUE; - } - - // Mark as DOWN - $ao->role |= Address::NODE_HOLD; - $ao->save(); - - $ao->contacted = $contact; - $result->push($ao); } if ($result->count()) @@ -178,6 +202,10 @@ class AddressIdle implements ShouldQueue private function old(Domain $do,int $days,int $flags=0,Address $ao=NULL): Collection { + // Ignore dates that are zero + if (! $days) + return collect(); + $age = Carbon::now()->subDays($days)->endOfDay(); return Address::select([