phase 3
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
<?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
|
||||
{
|
||||
Log::info("Fetching jobs from source: {$this->source->name}");
|
||||
|
||||
try {
|
||||
// Create API client for this source
|
||||
$client = JobApiClientFactory::make($this->source);
|
||||
|
||||
// Fetch jobs from the API
|
||||
$jobsData = $client->fetchJobs($this->params);
|
||||
|
||||
Log::info("Fetched {count} jobs from {source}", [
|
||||
'count' => count($jobsData),
|
||||
'source' => $this->source->name,
|
||||
]);
|
||||
|
||||
// Dispatch individual jobs to process each job listing
|
||||
foreach ($jobsData as $jobData) {
|
||||
ProcessJobListing::dispatch($this->source, $jobData);
|
||||
}
|
||||
|
||||
} catch (ApiException $e) {
|
||||
Log::error("API error fetching jobs from {source}", [
|
||||
'source' => $this->source->name,
|
||||
'error' => $e->getMessage(),
|
||||
'context' => $e->getContext(),
|
||||
]);
|
||||
|
||||
// Don't retry if it's a rate limit error with a long wait time
|
||||
if ($e instanceof \App\Services\JobApi\Exceptions\RateLimitException) {
|
||||
if ($e->retryAfter && $e->retryAfter > 3600) {
|
||||
$this->fail($e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw $e;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Unexpected error fetching jobs from {source}", [
|
||||
'source' => $this->source->name,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
]);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a job failure.
|
||||
*/
|
||||
public function failed(\Throwable $exception): void
|
||||
{
|
||||
Log::error("Failed to fetch jobs from {source} after {tries} attempts", [
|
||||
'source' => $this->source->name,
|
||||
'tries' => $this->tries,
|
||||
'error' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?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
|
||||
{
|
||||
try {
|
||||
$fingerprint = $this->jobData->generateFingerprint();
|
||||
|
||||
// Check if job already exists by fingerprint or external_id
|
||||
$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) {
|
||||
// Update existing job with fresh data
|
||||
$existingJob->update(array_merge(
|
||||
$this->jobData->toArray(),
|
||||
[
|
||||
'job_source_id' => $this->source->id,
|
||||
'fingerprint' => $fingerprint,
|
||||
]
|
||||
));
|
||||
|
||||
Log::debug("Updated existing job: {$this->jobData->title}");
|
||||
} else {
|
||||
// Create new job
|
||||
Job::create(array_merge(
|
||||
$this->jobData->toArray(),
|
||||
[
|
||||
'job_source_id' => $this->source->id,
|
||||
'fingerprint' => $fingerprint,
|
||||
]
|
||||
));
|
||||
|
||||
Log::debug("Created new job: {$this->jobData->title}");
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Failed to process job listing", [
|
||||
'source' => $this->source->name,
|
||||
'external_id' => $this->jobData->externalId,
|
||||
'title' => $this->jobData->title,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a job failure.
|
||||
*/
|
||||
public function failed(\Throwable $exception): void
|
||||
{
|
||||
Log::error("Failed to process job listing after {tries} attempts", [
|
||||
'source' => $this->source->name,
|
||||
'external_id' => $this->jobData->externalId,
|
||||
'tries' => $this->tries,
|
||||
'error' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user