clrghouz/app/Jobs/MailSend.php

123 lines
3.3 KiB
PHP
Raw Normal View History

2023-07-26 09:44:07 +00:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Repat\LaravelJobs\Job;
use App\Models\Address;
class MailSend implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const LOGKEY = 'JCM';
private ?bool $crash;
/**
* @param bool $crash Send crash mail only
*/
public function __construct(bool $crash=NULL)
{
$this->crash = $crash;
}
public function __get($key): mixed
{
switch ($key) {
case 'subject':
return __METHOD__;
default:
return NULL;
}
}
2023-07-26 09:44:07 +00:00
/**
* Execute the job.
*/
public function handle(): void
{
$u = Address::select([
'a.id',
'addresses.system_id',
'addresses.zone_id',
'addresses.region_id',
'addresses.host_id',
'addresses.node_id',
'addresses.point_id',
'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')
])
->from(Address::UncollectedEchomail()->union(Address::UncollectedNetmail())->union(Address::UncollectedFiles()),'a')
->where('systems.active',true)
2023-07-26 09:44:07 +00:00
->where('addresses.active',TRUE)
->where('zones.active',TRUE)
->where('domains.active',TRUE)
->join('addresses',['addresses.id'=>'a.id'])
2023-07-26 09:44:07 +00:00
->join('systems',['systems.id'=>'addresses.system_id'])
->join('zones',['zones.id'=>'addresses.zone_id'])
->join('domains',['domains.id'=>'zones.domain_id'])
->where(function($query) {
return $query->whereNull('autohold')
->orWhere('autohold',FALSE);
})
->when(! is_null($this->crash),function($query) {
return $query->when(
$this->crash,
function($query) {
return $query->where('pollmode',$this->crash);
},
function($query) {
return $query->whereNotNull('pollmode');
}
);
})
->groupBy('addresses.system_id','a.id','addresses.zone_id','addresses.region_id','addresses.host_id','addresses.node_id','addresses.point_id','addresses.hub_id','addresses.role')
->with(['system','zone.domain'])
2023-07-26 09:44:07 +00:00
->get();
// Return the system we poll
$u = $u->transform(function($item) {
if ($x=$item->parent()) {
$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 {
return $item;
2023-07-26 09:44:07 +00:00
}
});
2023-07-26 09:44:07 +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
AddressPoll::dispatch($x);
2023-07-26 09:44:07 +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
}
}
}
}