Files

88 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2026-04-03 00:29:05 +03:00
<?php
namespace App\Jobs;
use App\Models\JobSource;
use App\Services\JobApi\Exceptions\ApiException;
use App\Services\JobApi\JobApiClientFactory;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class FetchJobsFromSource implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3;
public int $timeout = 300; // 5 minutes
public int $backoff = 60; // Wait 60 seconds between retries
/**
* Create a new job instance.
*/
public function __construct(
public JobSource $source,
public array $params = []
) {}
/**
* Execute the job.
*/
public function handle(): void
{
2026-05-02 01:40:46 +03:00
$log = Log::channel('jobs_fetch');
$log->info("Fetching jobs from source: {$this->source->name}");
2026-04-03 00:29:05 +03:00
try {
$client = JobApiClientFactory::make($this->source);
$jobsData = $client->fetchJobs($this->params);
2026-05-02 01:40:46 +03:00
$log->info("Fetched {count} jobs from {source}", [
2026-04-03 00:29:05 +03:00
'count' => count($jobsData),
'source' => $this->source->name,
]);
foreach ($jobsData as $jobData) {
ProcessJobListing::dispatch($this->source, $jobData);
}
} catch (ApiException $e) {
2026-05-02 01:40:46 +03:00
$log->error("API error fetching jobs from {source}", [
2026-04-03 00:29:05 +03:00
'source' => $this->source->name,
'error' => $e->getMessage(),
'context' => $e->getContext(),
]);
if ($e instanceof \App\Services\JobApi\Exceptions\RateLimitException) {
if ($e->retryAfter && $e->retryAfter > 3600) {
$this->fail($e);
return;
}
}
throw $e;
} catch (\Exception $e) {
2026-05-02 01:40:46 +03:00
$log->error("Unexpected error fetching jobs from {source}", [
2026-04-03 00:29:05 +03:00
'source' => $this->source->name,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
throw $e;
}
}
public function failed(\Throwable $exception): void
{
2026-05-02 01:40:46 +03:00
Log::channel('jobs_fetch')->error("Failed to fetch jobs from {source} after {tries} attempts", [
2026-04-03 00:29:05 +03:00
'source' => $this->source->name,
'tries' => $this->tries,
'error' => $exception->getMessage(),
]);
}
}