clrghouz/app/Jobs/NodesNew.php
Deon George db6c569345
All checks were successful
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 29s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 1m32s
Create Docker Image / Final Docker Image Manifest (push) Successful in 8s
Added New Nodes Report
2024-11-04 12:44:46 +11:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use App\Models\{Address, Domain, System};
use App\Notifications\Netmails\NodesNew as NotificationNodesNew;
class NodesNew implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const LOGKEY = 'JNN';
private ?Carbon $since; // New nodes since this date
private Address $ao; // Domain we are processing
private Domain $do; // Domain we are processing
/**
* Create a new job instance.
*/
public function __construct(Domain $do,Carbon $since=NULL,Address $ao=NULL)
{
$this->do = $do->withoutRelations();
$this->ao = $ao?->withoutRelations();
$this->since = $since;
}
/**
* Execute the job.
*/
public function handle(): void
{
$since = ($this->since ?: Carbon::parse('last saturday'))->startOfDay();
$result = Address::FTN()
->ActiveFTN()
->join('systems',['systems.id'=>'addresses.system_id'])
->join('system_zone',['system_zone.system_id'=>'systems.id','system_zone.zone_id'=>'zones.id'])
->whereIn('zones.id',$this->do->zones->pluck('id'))
->where('systems.active',TRUE)
->where('systems.created_at','>=',$since)
->get();
if ($result->count()) {
Log::notice(sprintf('%s:- Sending new nodes since [%s] (%d)',self::LOGKEY,$since,$result->count()));
Notification::route('netmail',$this->ao->withoutRelations())
->notify(new NotificationNodesNew(
$since,
$result,
));
} else
Log::notice(sprintf('%s:- No nodes since [%s]',self::LOGKEY,$since));
}
}