clrghouz/app/Jobs/MailSend.php

72 lines
1.9 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\Traits\HubStats;
class MailSend #implements ShouldQueue
{
use HubStats,Dispatchable;
private const LOGKEY = 'JMS';
private bool $mode;
/**
* @param bool $crash Send crash mail only
*/
public function __construct(?bool $mode=NULL) {
$this->mode = $mode;
}
/**
* Execute the job.
*/
public function handle(): void
{
// Return the system we poll
$u = $this
->HubStats(Carbon::now())
->get()
->transform(function($item) {
// Add the list of queued items to the uplink
if ($x=$item->uplink()) {
$x->uncollected_echomail = $item->uncollected_echomail;
$x->uncollected_netmail = $item->uncollected_netmail;
$x->uncollected_files = $item->uncollected_files;
return $x;
} else {
return $item;
}
})
->filter(fn($item)=>(! $item->system->autohold) // System is not on autohold
&& (is_null($this->mode) // If we didnt specify a poll mode, poll anyway
|| ($item->system->pollmode) // If system poll mode is set to crash
|| ($item->system->pollmode === $this->mode))); // If the sytem poll mode is the same as mode calling this send
foreach ($u->groupBy('ftn') as $oo) {
if (! Job::where('queue','poll')->get()->pluck('command.address.id')->contains(($x=$oo->first())->id)) {
Log::info(sprintf('%s:- Polling [%s] - we have mail for [%d] links. (%d Netmail,%d Echomail,%d Files)',
self::LOGKEY,
$x->ftn,
$oo->count(),
$oo->sum('uncollected_netmail'),
$oo->sum('uncollected_echomail'),
$oo->sum('uncollected_files'),
));
AddressPoll::dispatch($x);
} else {
Log::notice(sprintf('%s:= Not scheduling poll to [%s], there is already one in the queue',self::LOGKEY,$x->ftn));
}
}
}
}