Files
jobifygarden/app/Console/Commands/FetchJobs.php
T
2026-05-01 23:33:15 +03:00

107 lines
3.3 KiB
PHP

<?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
{
protected $signature = 'jobs:fetch {source?} {--limit=50} {--sync}';
protected $description = 'Fetch jobs from API sources and queue them for processing';
/**
* 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.');
}
}
}