LAR_Maintenance/app/Jobs/ScheduleNextMaintenance.php

64 lines
1.8 KiB
PHP
Raw Normal View History

2024-07-31 15:04:33 +00:00
<?php
namespace App\Jobs;
use App\Models\Maintenance;
use App\Models\MaintenanceHistory;
2024-08-06 09:02:45 +00:00
use App\Models\MaintenanceTaskHistory;
2024-07-31 15:04:33 +00:00
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) {
2024-08-06 05:17:35 +00:00
$valid = CronExpression::isValidExpression($maintenance->schedule);
if (!$valid) {
dd($maintenance->schedule);
return;
}
2024-07-31 15:04:33 +00:00
2024-08-06 05:17:35 +00:00
$cron = new CronExpression($maintenance->schedule);
2024-08-06 06:31:51 +00:00
$maintenancePlanned = $maintenance->history()->create([
2024-07-31 15:04:33 +00:00
'start_at' => $cron->getNextRunDate(null, 2)
]);
2024-08-06 06:31:51 +00:00
$maintenancePlanned->refresh();
2024-08-06 09:02:45 +00:00
$hosts = $maintenance->hosts;
foreach ($hosts as $key => $host) {
$maintenancePlannedHost = $maintenancePlanned->historyHosts()->create([
'host_id' => $host->id
]);
$tasks = $maintenance->tasks;
2024-08-06 06:31:51 +00:00
foreach ($tasks as $key => $task) {
2024-08-06 09:02:45 +00:00
$maintenancePlannedHost->historyTasks()->create([
'maintenance_task_id' => $task->id,
'maintenance_history_id' => $maintenancePlanned->id
2024-08-06 06:31:51 +00:00
]);
}
}
2024-07-31 15:04:33 +00:00
}
2024-08-06 06:31:51 +00:00
die();
2024-07-31 15:04:33 +00:00
}
}