File size: 1,000 Bytes
907b200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php


namespace App\Jobs;

use App\Services\AiService;
use App\Models\AiJob;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class UpdateAiJobStatus implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $aiJob;

    public function __construct(AiJob $aiJob)
    {
        $this->aiJob = $aiJob;
    }

    public function handle(AiService $aiService)
    {
        $statusResponse = $aiService->status($this->aiJob->job_id);
        $previewResponse = $aiService->preview($this->aiJob->job_id);

        $this->aiJob->update([
            'status' => $statusResponse['status'] ?? $this->aiJob->status,
            'preview' => $previewResponse ?? $this->aiJob->preview
        ]);

        if (($statusResponse['status'] ?? '') === 'running') {
            self::dispatch($this->aiJob)->delay(now()->addSeconds(30));
        }
    }
}