2023-07-26 09:44:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Repat\LaravelJobs\Job;
|
|
|
|
|
|
|
|
use App\Models\Address;
|
|
|
|
|
2023-11-26 00:32:21 +00:00
|
|
|
class MailSend #implements ShouldQueue
|
2023-07-26 09:44:07 +00:00
|
|
|
{
|
2023-11-26 00:32:21 +00:00
|
|
|
use Dispatchable;
|
2023-07-26 09:44:07 +00:00
|
|
|
|
2023-11-27 05:00:02 +00:00
|
|
|
private const LOGKEY = 'JMS';
|
2023-07-26 09:44:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $crash Send crash mail only
|
|
|
|
*/
|
2023-11-26 00:32:21 +00:00
|
|
|
public function __construct(private ?bool $crash=NULL) {}
|
2023-09-05 07:42:41 +00:00
|
|
|
|
2023-07-26 09:44:07 +00:00
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*/
|
|
|
|
public function handle(): void
|
|
|
|
{
|
2023-11-25 10:52:05 +00:00
|
|
|
$u = Address::select([
|
|
|
|
'a.id',
|
2023-12-08 04:16:49 +00:00
|
|
|
'a.system_id',
|
|
|
|
'a.zone_id',
|
2023-11-25 10:52:05 +00:00
|
|
|
'addresses.region_id',
|
2023-12-08 04:16:49 +00:00
|
|
|
'a.host_id',
|
|
|
|
'a.node_id',
|
|
|
|
'a.point_id',
|
2023-11-25 10:52:05 +00:00
|
|
|
'addresses.hub_id',
|
|
|
|
'addresses.role',
|
|
|
|
DB::raw('sum(a.uncollected_echomail) as uncollected_echomail'),
|
|
|
|
DB::raw('sum(a.uncollected_netmail) as uncollected_netmail'),
|
|
|
|
DB::raw('sum(a.uncollected_files) as uncollected_files')
|
|
|
|
])
|
2023-12-16 12:22:23 +00:00
|
|
|
->from(Address::UncollectedEchomailTotal()->union(Address::UncollectedNetmailTotal())->union(Address::UncollectedFilesTotal()),'a')
|
2023-11-25 10:52:05 +00:00
|
|
|
->where('systems.active',true)
|
2023-07-26 09:44:07 +00:00
|
|
|
->where('addresses.active',TRUE)
|
|
|
|
->where('zones.active',TRUE)
|
|
|
|
->where('domains.active',TRUE)
|
2023-11-25 10:52:05 +00:00
|
|
|
->join('addresses',['addresses.id'=>'a.id'])
|
2023-12-08 04:16:49 +00:00
|
|
|
->join('systems',['systems.id'=>'a.system_id'])
|
|
|
|
->join('zones',['zones.id'=>'a.zone_id'])
|
2023-07-26 09:44:07 +00:00
|
|
|
->join('domains',['domains.id'=>'zones.domain_id'])
|
2023-12-08 04:16:49 +00:00
|
|
|
->groupBy('a.system_id','a.id','a.zone_id','addresses.region_id','a.host_id','a.node_id','a.point_id','addresses.hub_id','addresses.role')
|
2023-11-25 10:52:05 +00:00
|
|
|
->with(['system','zone.domain'])
|
2023-07-26 09:44:07 +00:00
|
|
|
->get();
|
|
|
|
|
2023-11-25 10:52:05 +00:00
|
|
|
// Return the system we poll
|
|
|
|
$u = $u->transform(function($item) {
|
2024-05-09 11:22:30 +00:00
|
|
|
if ($x=$item->uplink()) {
|
2023-11-25 10:52:05 +00:00
|
|
|
$x->uncollected_echomail = $item->uncollected_echomail;
|
|
|
|
$x->uncollected_netmail = $item->uncollected_netmail;
|
|
|
|
$x->uncollected_files = $item->uncollected_files;
|
|
|
|
|
|
|
|
return $x;
|
2023-07-26 09:44:07 +00:00
|
|
|
|
|
|
|
} else {
|
2023-11-25 10:52:05 +00:00
|
|
|
return $item;
|
2023-07-26 09:44:07 +00:00
|
|
|
}
|
2024-06-26 06:47:10 +00:00
|
|
|
})
|
|
|
|
->filter(function($item) {
|
|
|
|
if ($item->system->autohold)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return is_null($this->crash) || ($item->system->pollmode) || ($item->system->pollmode === $this->crash) ? $item : NULL;
|
|
|
|
});
|
2023-07-26 09:44:07 +00:00
|
|
|
|
2023-11-25 10:52:05 +00:00
|
|
|
foreach ($u->groupBy('ftn') as $oo) {
|
|
|
|
if (Job::where('queue','poll')->get()->pluck('command.address.id')->search(($x=$oo->first())->id) === FALSE) {
|
|
|
|
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'),
|
|
|
|
));
|
2023-07-26 09:44:07 +00:00
|
|
|
|
2023-11-25 10:52:05 +00:00
|
|
|
AddressPoll::dispatch($x);
|
2023-07-26 09:44:07 +00:00
|
|
|
|
2023-11-25 10:52:05 +00:00
|
|
|
} else {
|
|
|
|
Log::notice(sprintf('%s:= Not scheduling poll to [%s], there is already one in the queue',self::LOGKEY,$x->ftn));
|
2023-07-26 09:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|