phase 3
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\FetchJobsFromSource;
|
||||
use App\Models\JobSource;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
#[Signature('jobs:fetch {source?} {--limit=50} {--sync}')]
|
||||
#[Description('Fetch jobs from API sources and queue them for processing')]
|
||||
class FetchJobs extends Command
|
||||
{
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$sourceName = $this->argument('source');
|
||||
$limit = (int) $this->option('limit');
|
||||
$sync = $this->option('sync');
|
||||
|
||||
$this->info('Starting job fetch process...');
|
||||
$this->newLine();
|
||||
|
||||
try {
|
||||
if ($sourceName) {
|
||||
// Fetch from specific source
|
||||
$this->fetchFromSource($sourceName, $limit, $sync);
|
||||
} else {
|
||||
// Fetch from all active sources
|
||||
$this->fetchFromAllSources($limit, $sync);
|
||||
}
|
||||
|
||||
$this->newLine();
|
||||
$this->info('Job fetch process completed!');
|
||||
|
||||
return self::SUCCESS;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->error("Failed to fetch jobs: {$e->getMessage()}");
|
||||
return self::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
private function fetchFromAllSources(int $limit, bool $sync): void
|
||||
{
|
||||
$sources = JobSource::active()->get();
|
||||
|
||||
if ($sources->isEmpty()) {
|
||||
$this->warn('No active job sources found.');
|
||||
$this->line('Run: php artisan db:seed --class=JobSourceSeeder');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info("Fetching jobs from {$sources->count()} active sources...");
|
||||
|
||||
foreach ($sources as $source) {
|
||||
$this->line(" • Queuing fetch for: {$source->name}");
|
||||
|
||||
$params = ['limit' => $limit];
|
||||
|
||||
if ($sync) {
|
||||
FetchJobsFromSource::dispatchSync($source, $params);
|
||||
} else {
|
||||
FetchJobsFromSource::dispatch($source, $params);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$sync) {
|
||||
$this->info("\nJobs have been queued for processing.");
|
||||
$this->line('Run: php artisan queue:work to process them.');
|
||||
}
|
||||
}
|
||||
|
||||
private function fetchFromSource(string $sourceName, int $limit, bool $sync): void
|
||||
{
|
||||
$source = JobSource::where('name', 'LIKE', "%{$sourceName}%")
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
|
||||
if (!$source) {
|
||||
$this->error("Active source not found: {$sourceName}");
|
||||
$this->line("\nAvailable sources:");
|
||||
JobSource::active()->get()->each(fn($s) => $this->line(" • {$s->name}"));
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info("Fetching jobs from: {$source->name}");
|
||||
|
||||
$params = ['limit' => $limit];
|
||||
|
||||
if ($sync) {
|
||||
FetchJobsFromSource::dispatchSync($source, $params);
|
||||
$this->info('Fetch completed synchronously.');
|
||||
} else {
|
||||
FetchJobsFromSource::dispatch($source, $params);
|
||||
$this->info('Fetch job has been queued.');
|
||||
$this->line('Run: php artisan queue:work to process it.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user