clrghouz/app/Jobs/AddressPoll.php

167 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
2023-07-26 09:44:07 +00:00
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
2023-07-26 09:44:07 +00:00
use Illuminate\Queue\ManuallyFailedException;
use Illuminate\Queue\MaxAttemptsExceededException;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
2023-07-26 09:44:07 +00:00
use Illuminate\Support\Facades\Notification;
use App\Classes\Protocol;
use App\Classes\Protocol\{Binkp,EMSI};
use App\Classes\Sock\Exception\SocketException;
use App\Classes\Sock\SocketClient;
use App\Models\{Address,Mailer,Setup};
2023-07-26 09:44:07 +00:00
use App\Notifications\Netmails\PollingFailed;
use App\Traits\ObjectIssetFix;
2023-07-26 09:44:07 +00:00
class AddressPoll implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, ObjectIssetFix;
2023-07-26 09:44:07 +00:00
private const LOGKEY = 'JAP';
2023-07-26 09:44:07 +00:00
public int $tries = 5;
public int $maxExceptions = 1;
public bool $failOnTimeout = TRUE;
2023-07-26 09:44:07 +00:00
public const QUEUE = 'poll';
private Address $ao;
private ?Mailer $mo;
public function __construct(Address $ao,Mailer $mo=NULL)
{
$this->ao = $ao;
$this->mo = $mo;
2023-07-26 09:44:07 +00:00
$this->onQueue(self::QUEUE);
}
public function __get($key): mixed
{
switch ($key) {
case 'address':
return $this->ao;
case 'jobname':
2023-07-26 09:44:07 +00:00
return $this->ao->ftn;
default:
return NULL;
}
}
/**
* Time to wait between tries
*
* @return int[] in seconds
*/
public function backoff(): array
{
return [
60*5, // 5 mins
60*60, // 1 hr
60*60*6, // 6 hrs
60*60*12 // 12 hrs
];
}
/**
* When calling MessageProcess - we assume that the packet is from a valid source
*/
public function handle()
{
2023-07-26 09:44:07 +00:00
if (! $this->ao->system->mailer_preferred->count() || ($this->mo && (! $this->ao->system->mailer_preferred->find($this->mo)))) {
$this->fail('Missing mailer details');
return;
}
Log::info(sprintf('%s:- Polling [%s] - attempt [%d]',self::LOGKEY,$this->ao->ftn,$this->attempts()));
foreach ($this->ao->system->mailer_preferred as $o) {
// If we chose a protocol, skip to find the mailer details for it
if ($this->mo && ($o->id !== $this->mo->id))
continue;
switch ($o->name) {
case 'BINKP':
$s = new Binkp(Setup::findOrFail(config('app.id')));
$mo = Mailer::where('name','BINKP')->singleOrFail();
break;
case 'EMSI':
$s = new EMSI(Setup::findOrFail(config('app.id')));
$mo = Mailer::where('name','EMSI')->singleOrFail();
break;
default:
2023-07-26 09:44:07 +00:00
$this->fail('Mailer type unhandled');
return;
}
2023-07-26 09:44:07 +00:00
Log::info(sprintf('%s:- Trying a [%s] session to [%s:%d] (%s)',
self::LOGKEY,$o->name,$this->ao->system->address,$o->pivot->port,$this->ao->ftn));
2023-07-26 09:44:07 +00:00
try {
$client = SocketClient::create($this->ao->system->address,$o->pivot->port);
if (($s->session($mo,$client,$this->ao) & Protocol::S_MASK) === Protocol::S_OK) {
2023-07-26 09:44:07 +00:00
Log::info(sprintf('%s:= Connection ended successfully with [%s] (%s)',self::LOGKEY,$client->address_remote,$this->ao->ftn));
return;
} else {
Log::alert(sprintf('%s:! Connection failed to [%s] (%s)',self::LOGKEY,$client->address_remote,$this->ao->ftn));
}
} catch (SocketException $e) {
Log::error(sprintf('%s:! SocketException Unable to connect to [%s]: %s',self::LOGKEY,$this->ao->ftn,$e->getMessage()));
2023-07-26 09:44:07 +00:00
break;
2024-04-16 12:31:09 +00:00
} catch (\ErrorException $e) {
Log::error(sprintf('%s:! Unable to connect to [%s]: %s',self::LOGKEY,$this->ao->ftn,$e->getMessage()));
break;
}
}
2023-07-26 09:44:07 +00:00
$delay = (int)($this->backoff()[$this->attempts()-1] ?? last($this->backoff()));
Log::info(sprintf('%s:= Retrying poll in %d seconds',self::LOGKEY,$delay));
$this->release($delay);
}
public function failed(\Throwable $exception): void
{
switch (get_class($exception)) {
case ManuallyFailedException::class:
Log::error(sprintf('%s:! Address Poll failed for [%s] (%s)',self::LOGKEY,$this->ao->ftn,$exception->getMessage()));
break;
case MaxAttemptsExceededException::class:
Log::error(sprintf('%s:! Address Poll was tried too many times for [%s]',self::LOGKEY,$this->ao->ftn));
Notification::route('netmail',$this->ao)->notify(new PollingFailed);
$this->ao->system->autohold = TRUE;
$this->ao->system->save();
exit(0);
default:
Log::error(sprintf('%s:! Address Poll to [%s] with an unknown exception (%s)[%s]',self::LOGKEY,$this->ao->ftn,get_class($exception),$exception->getMessage()));
2023-07-26 09:44:07 +00:00
}
}
public function uniqueId(): string
{
return $this->ao->id;
}
}