LAR_Maintenance/app/Jobs/ScheduleNextMaintenance.php
2024-08-06 08:31:51 +02:00

59 lines
1.5 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Maintenance;
use App\Models\MaintenanceHistory;
use Cron\CronExpression;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ScheduleNextMaintenance implements ShouldQueue
{
use Queueable;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
$maintenances = Maintenance::all();
foreach ($maintenances as $maintenance) {
$valid = CronExpression::isValidExpression($maintenance->schedule);
if (!$valid) {
dd($maintenance->schedule);
return;
}
$cron = new CronExpression($maintenance->schedule);
$maintenancePlanned = $maintenance->history()->create([
'start_at' => $cron->getNextRunDate(null, 2)
]);
$maintenancePlanned->refresh();
$tasks = $maintenancePlanned->tasks;
if(!empty($tasks)){
dd($tasks);
foreach ($tasks as $key => $task) {
$maintenancePlanned->tasks()->create([
'host_id' => $task->host_id,
]);
}
}
}
die();
}
}