2025-07-05 18:26:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Models\Feed;
|
|
|
|
|
use App\Services\Article\ArticleFetcher;
|
|
|
|
|
use App\Services\Log\LogSaver;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
|
|
|
|
|
|
|
|
class ArticleDiscoveryForFeedJob implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Queueable;
|
|
|
|
|
|
|
|
|
|
private const FEED_DISCOVERY_DELAY_MINUTES = 5;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly Feed $feed
|
|
|
|
|
) {
|
|
|
|
|
$this->onQueue('feed-discovery');
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
public function handle(LogSaver $logSaver, ArticleFetcher $articleFetcher): void
|
2025-07-05 18:26:04 +02:00
|
|
|
{
|
2025-08-15 02:50:42 +02:00
|
|
|
$logSaver->info('Starting feed article fetch', null, [
|
2025-07-05 18:26:04 +02:00
|
|
|
'feed_id' => $this->feed->id,
|
|
|
|
|
'feed_name' => $this->feed->name,
|
|
|
|
|
'feed_url' => $this->feed->url
|
|
|
|
|
]);
|
|
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
$articles = $articleFetcher->getArticlesFromFeed($this->feed);
|
2025-07-05 18:26:04 +02:00
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
$logSaver->info('Feed article fetch completed', null, [
|
2025-07-05 18:26:04 +02:00
|
|
|
'feed_id' => $this->feed->id,
|
|
|
|
|
'feed_name' => $this->feed->name,
|
|
|
|
|
'articles_count' => $articles->count()
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->feed->update(['last_fetched_at' => now()]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function dispatchForAllActiveFeeds(): void
|
|
|
|
|
{
|
2025-08-15 02:50:42 +02:00
|
|
|
$logSaver = app(LogSaver::class);
|
|
|
|
|
|
2025-07-05 18:26:04 +02:00
|
|
|
Feed::where('is_active', true)
|
|
|
|
|
->get()
|
2025-08-15 02:50:42 +02:00
|
|
|
->each(function (Feed $feed, $index) use ($logSaver) {
|
2025-07-05 18:26:04 +02:00
|
|
|
// Space jobs apart to avoid overwhelming feeds
|
|
|
|
|
$delayMinutes = $index * self::FEED_DISCOVERY_DELAY_MINUTES;
|
|
|
|
|
|
|
|
|
|
self::dispatch($feed)
|
|
|
|
|
->delay(now()->addMinutes($delayMinutes))
|
|
|
|
|
->onQueue('feed-discovery');
|
|
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
$logSaver->info('Dispatched feed discovery job', null, [
|
2025-07-05 18:26:04 +02:00
|
|
|
'feed_id' => $feed->id,
|
|
|
|
|
'feed_name' => $feed->name,
|
|
|
|
|
'delay_minutes' => $delayMinutes
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|