Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.67% covered (danger)
16.67%
1 / 6
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RefreshCompanyResearchJob
16.67% covered (danger)
16.67%
1 / 6
33.33% covered (danger)
33.33%
1 / 3
8.21
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 failed
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Jobs;
4
5use App\Http\Models\CompanyResearch;
6use App\Http\Services\RolePlay\CompanyResearchService;
7use Illuminate\Bus\Queueable;
8use Illuminate\Contracts\Queue\ShouldQueue;
9use Illuminate\Foundation\Bus\Dispatchable;
10use Illuminate\Queue\InteractsWithQueue;
11use Illuminate\Queue\SerializesModels;
12
13/**
14 * Refreshes a stale CompanyResearch record asynchronously.
15 *
16 * Dispatched by {@see CompanyResearchService::getOrCreateForUrl()} when
17 * the cached research exists but has exceeded the TTL window. The caller
18 * returns the stale record immediately while this job re-runs the
19 * research pipeline in the background.
20 */
21class RefreshCompanyResearchJob implements ShouldQueue
22{
23    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
24
25    /**
26     * The number of times the job may be attempted.
27     *
28     * @var int
29     */
30    public $tries = 3;
31
32    /**
33     * The number of seconds to wait before retrying the job.
34     *
35     * @var array<int, int>
36     */
37    public $backoff = [30, 60, 120];
38
39    /**
40     * Create a new job instance.
41     *
42     * @param  CompanyResearch  $research  The research record to refresh
43     */
44    public function __construct(
45        public CompanyResearch $research
46    ) {}
47
48    /**
49     * Execute the job.
50     */
51    public function handle(CompanyResearchService $service): void
52    {
53        $service->runResearch($this->research);
54    }
55
56    /**
57     * Handle a job failure.
58     *
59     * Marks the research record as failed so the polling endpoint reflects
60     * the error state.
61     */
62    public function failed(\Throwable $e): void
63    {
64        $this->research->update([
65            'status' => 'failed',
66            'error' => $e->getMessage(),
67        ]);
68    }
69}