2024-07-31 17:04:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Models\Maintenance;
|
|
|
|
use App\Models\MaintenanceHistory;
|
2024-08-06 11:02:45 +02:00
|
|
|
use App\Models\MaintenanceTaskHistory;
|
2024-08-06 16:57:48 +02:00
|
|
|
use Carbon\Carbon;
|
2024-07-31 17:04:33 +02: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;
|
2024-08-06 16:57:48 +02:00
|
|
|
use Poliander\Cron\CronExpression as CronCronExpression;
|
2024-07-31 17:04:33 +02:00
|
|
|
|
|
|
|
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 16:57:48 +02:00
|
|
|
$cron = new CronCronExpression($maintenance->schedule);
|
2024-08-16 18:20:45 +02:00
|
|
|
$nextRunTime = Carbon::createFromTimestamp($cron->getNext());
|
|
|
|
|
2024-08-16 23:37:42 +02:00
|
|
|
if(MaintenanceHistory::where('hash', md5($maintenance->id . $nextRunTime))->first() !== null){
|
2024-08-16 18:20:45 +02:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2024-08-06 08:31:51 +02:00
|
|
|
$maintenancePlanned = $maintenance->history()->create([
|
2024-08-16 18:20:45 +02:00
|
|
|
'start_at' => $nextRunTime,
|
2024-08-08 10:33:21 +02:00
|
|
|
'guestor_id' => $maintenance->guestor_id,
|
2024-07-31 17:04:33 +02:00
|
|
|
]);
|
2024-09-10 15:17:13 +02:00
|
|
|
|
2024-08-06 08:31:51 +02:00
|
|
|
$maintenancePlanned->refresh();
|
2024-08-06 11:02:45 +02:00
|
|
|
|
|
|
|
$hosts = $maintenance->hosts;
|
|
|
|
foreach ($hosts as $key => $host) {
|
|
|
|
$maintenancePlannedHost = $maintenancePlanned->historyHosts()->create([
|
|
|
|
'host_id' => $host->id
|
|
|
|
]);
|
|
|
|
|
2024-08-07 21:45:21 +02:00
|
|
|
$tasks = $maintenance->hosts->find($host->id)->tasks;
|
2024-08-06 08:31:51 +02:00
|
|
|
foreach ($tasks as $key => $task) {
|
2024-08-06 11:02:45 +02:00
|
|
|
$maintenancePlannedHost->historyTasks()->create([
|
|
|
|
'maintenance_task_id' => $task->id,
|
|
|
|
'maintenance_history_id' => $maintenancePlanned->id
|
2024-08-06 08:31:51 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2024-07-31 17:04:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|