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\Classes\FTN\Message ;
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 ;
}
2023-09-05 07:42:41 +00:00
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
{
// Netmail waiting by node (only netmail is routed)
$netmails = Address :: select ([ 'addresses.id' , 'addresses.zone_id' , 'region_id' , 'host_id' , 'node_id' , 'point_id' , 'role' , 'addresses.system_id' , DB :: raw ( 'count(netmails.id) AS nm' )])
-> join ( 'zones' ,[ 'zones.id' => 'addresses.zone_id' ])
-> join ( 'domains' ,[ 'domains.id' => 'zones.domain_id' ])
-> join ( 'netmails' ,[ 'netmails.tftn_id' => 'addresses.id' ])
-> join ( 'systems' ,[ 'systems.id' => 'addresses.system_id' ])
-> where ( 'addresses.active' , TRUE )
-> where ( 'zones.active' , TRUE )
-> where ( 'domains.active' , TRUE )
-> where ( function ( $query ) {
return $query -> whereNull ( 'autohold' )
-> orWhere ( 'autohold' , FALSE );
})
-> where ( function ( $query ) {
return $query -> whereRaw ( sprintf ( '(flags & %d) > 0' , Message :: FLAG_INTRANSIT ))
-> orWhereRaw ( sprintf ( '(flags & %d) > 0' , Message :: FLAG_LOCAL ));
})
-> whereRaw ( sprintf ( '(flags & %d) = 0' , Message :: FLAG_SENT ))
-> 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.id' )
-> havingRaw ( 'count(*) > 0' )
-> get ();
// Echomail waiting by node
$echomails = Address :: select ([ 'addresses.id' , 'addresses.zone_id' , 'region_id' , 'host_id' , 'node_id' , 'point_id' , 'role' , 'addresses.system_id' , DB :: raw ( 'count(*) AS em' )])
-> distinct ()
-> join ( 'zones' ,[ 'zones.id' => 'addresses.zone_id' ])
-> join ( 'domains' ,[ 'domains.id' => 'zones.domain_id' ])
-> join ( 'echomail_seenby' ,[ 'echomail_seenby.address_id' => 'addresses.id' ])
-> join ( 'systems' ,[ 'systems.id' => 'addresses.system_id' ])
-> where ( 'addresses.active' , TRUE )
-> where ( 'zones.active' , TRUE )
-> where ( 'domains.active' , TRUE )
-> where ( function ( $query ) {
return $query -> whereNull ( 'autohold' )
-> orWhere ( 'autohold' , FALSE );
})
-> whereNotNull ( 'export_at' )
-> whereNull ( 'sent_at' )
-> 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.id' ])
-> havingRaw ( 'count(*) > 0' )
-> FTNOrder ()
-> get ();
// Files waiting by node
$files = Address :: select ([ 'addresses.id' , 'addresses.zone_id' , 'region_id' , 'host_id' , 'node_id' , 'point_id' , 'role' , 'addresses.system_id' , DB :: raw ( 'count(*) AS fs' )])
-> distinct ()
-> join ( 'zones' ,[ 'zones.id' => 'addresses.zone_id' ])
-> join ( 'domains' ,[ 'domains.id' => 'zones.domain_id' ])
-> join ( 'file_seenby' ,[ 'file_seenby.address_id' => 'addresses.id' ])
-> join ( 'systems' ,[ 'systems.id' => 'addresses.system_id' ])
-> where ( 'addresses.active' , TRUE )
-> where ( 'zones.active' , TRUE )
-> where ( 'domains.active' , TRUE )
-> where ( function ( $query ) {
return $query -> whereNull ( 'autohold' )
-> orWhere ( 'autohold' , FALSE );
})
-> whereNotNull ( 'export_at' )
-> whereNull ( 'sent_at' )
-> 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.id' ])
-> havingRaw ( 'count(*) > 0' )
-> FTNOrder ()
-> get ();
// Merge our netmails
foreach ( $echomails as $ao ) {
if (( $x = $netmails -> search ( function ( $item ) use ( $ao ) { return $item -> id === $ao -> id ; })) !== FALSE ) {
$netmails -> get ( $x ) -> em = $ao -> em ;
} else {
$netmails -> push ( $ao );
}
}
// Merge our files
foreach ( $files as $ao ) {
if (( $x = $netmails -> search ( function ( $item ) use ( $ao ) { return $item -> id === $ao -> id ; })) !== FALSE ) {
$netmails -> get ( $x ) -> fs = $ao -> fs ;
} else {
$netmails -> push ( $ao );
}
}
// Remove direct links
$netmails = $netmails -> filter ( function ( $item ) { return $item -> parent (); });
foreach ( $netmails -> groupBy ( function ( $item ) { return $item -> parent () -> ftn ; }) as $oo ) {
$ao = $oo -> first ();
Log :: info ( sprintf ( '%s:- Polling [%s] - we have mail for [%d] links. (%d Netmail,%d Echomail,%d Files)' ,
self :: LOGKEY ,
$ao -> ftn ,
$oo -> count (),
$oo -> sum ( 'nm' ),
$oo -> sum ( 'em' ),
$oo -> sum ( 'fs' ),
));
// @todo Only send crash mail - send normal mail with a schedule or hold mail
if ( Job :: where ( 'queue' , $this -> queue ) -> get () -> pluck ( 'command.address.id' ) -> search ( $ao -> id ) !== FALSE ) {
Log :: alert ( sprintf ( '%s:= Not scheduling poll to [%s], there is already one in the queue' , self :: LOGKEY , $ao -> ftn ));
continue ;
}
AddressPoll :: dispatch ( $ao );
}
}
}