Files

91 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2026-04-03 00:29:05 +03:00
<?php
namespace App\Jobs;
use App\Models\Job;
use App\Models\JobSource;
use App\Services\JobApi\DTO\JobData;
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 ProcessJobListing implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3;
public int $timeout = 60;
/**
* Create a new job instance.
*/
public function __construct(
public JobSource $source,
public JobData $jobData
) {}
/**
* Execute the job.
*/
public function handle(): void
{
2026-05-02 01:40:46 +03:00
$log = Log::channel('jobs_process');
2026-04-03 00:29:05 +03:00
try {
$fingerprint = $this->jobData->generateFingerprint();
$existingJob = Job::where('fingerprint', $fingerprint)
->orWhere(function ($query) {
$query->where('job_source_id', $this->source->id)
->where('external_id', $this->jobData->externalId);
})
->first();
if ($existingJob) {
$existingJob->update(array_merge(
$this->jobData->toArray(),
[
'job_source_id' => $this->source->id,
'fingerprint' => $fingerprint,
]
));
2026-05-02 01:40:46 +03:00
$log->debug("Updated existing job: {$this->jobData->title}");
2026-04-03 00:29:05 +03:00
} else {
Job::create(array_merge(
$this->jobData->toArray(),
[
'job_source_id' => $this->source->id,
'fingerprint' => $fingerprint,
]
));
2026-05-02 01:40:46 +03:00
$log->debug("Created new job: {$this->jobData->title}");
2026-04-03 00:29:05 +03:00
}
} catch (\Exception $e) {
2026-05-02 01:40:46 +03:00
$log->error("Failed to process job listing", [
2026-04-03 00:29:05 +03:00
'source' => $this->source->name,
'external_id' => $this->jobData->externalId,
'title' => $this->jobData->title,
'error' => $e->getMessage(),
]);
throw $e;
}
}
public function failed(\Throwable $exception): void
{
2026-05-02 01:40:46 +03:00
Log::channel('jobs_process')->error("Failed to process job listing after {tries} attempts", [
2026-04-03 00:29:05 +03:00
'source' => $this->source->name,
'external_id' => $this->jobData->externalId,
'tries' => $this->tries,
'error' => $exception->getMessage(),
]);
}
}