You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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 App\Models\ProcessHistories;
class ProcessImagesJob implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public $historyId;
public $tries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($historyId)
{
$this->historyId = $historyId;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$history = ProcessHistories::find($this->historyId);
if (!$history) {
return;
}
$startQueue = now();
// Допустим тут время ожидания очереди (между startQueue и started_at)
$queueWaitTime = $startQueue->diffInSeconds($history->started_at);
// Эмуляция обработки
// 1. Загрузка файлов (у нас уже загружены локально, можно считать что это этап)
$uploadTime = rand(1,3); // псевдо
sleep($uploadTime);
// 2. Запрос к внешнему API для обработки каждого изображения
$processingStart = now();
$processedCount = 0;
foreach ($history->images as $index => $img) {
// Имитируем обращение к API
sleep(2); // эмуляция API задержки
$processedCount++;
$history->processed_count = $processedCount;
$history->save();
}
$processingTime = $processingStart->diffInSeconds(now());
// 3. Сохранение итогового step-файла
$savingStart = now();
// Имитируем сохранение файла (например API возвращает имя файла)
$stepFileName = 'step_' . time() . '.txt';
Storage::disk('public')->put($stepFileName, "Данные шага обработки.");
$savingTime = $savingStart->diffInSeconds(now());
$history->step_file_name = $stepFileName;
$history->finished_at = now();
$history->stages_timing = [
'queue_wait_time' => $queueWaitTime,
'upload_time' => $uploadTime,
'processing_time' => $processingTime,
'saving_time' => $savingTime
];
$history->save();
}
}