Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ArticleDiscoveryForFeedJob | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| dispatchForAllActiveFeeds | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use App\Models\Feed; |
| 6 | use App\Services\Article\ArticleFetcher; |
| 7 | use App\Services\Log\LogSaver; |
| 8 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | use Illuminate\Foundation\Queue\Queueable; |
| 10 | |
| 11 | class ArticleDiscoveryForFeedJob implements ShouldQueue |
| 12 | { |
| 13 | use Queueable; |
| 14 | |
| 15 | private const FEED_DISCOVERY_DELAY_MINUTES = 5; |
| 16 | |
| 17 | public function __construct( |
| 18 | private readonly Feed $feed |
| 19 | ) { |
| 20 | $this->onQueue('feed-discovery'); |
| 21 | } |
| 22 | |
| 23 | public function handle(): void |
| 24 | { |
| 25 | LogSaver::info('Starting feed article fetch', null, [ |
| 26 | 'feed_id' => $this->feed->id, |
| 27 | 'feed_name' => $this->feed->name, |
| 28 | 'feed_url' => $this->feed->url |
| 29 | ]); |
| 30 | |
| 31 | $articles = ArticleFetcher::getArticlesFromFeed($this->feed); |
| 32 | |
| 33 | LogSaver::info('Feed article fetch completed', null, [ |
| 34 | 'feed_id' => $this->feed->id, |
| 35 | 'feed_name' => $this->feed->name, |
| 36 | 'articles_count' => $articles->count() |
| 37 | ]); |
| 38 | |
| 39 | $this->feed->update(['last_fetched_at' => now()]); |
| 40 | } |
| 41 | |
| 42 | public static function dispatchForAllActiveFeeds(): void |
| 43 | { |
| 44 | Feed::where('is_active', true) |
| 45 | ->get() |
| 46 | ->each(function (Feed $feed, $index) { |
| 47 | // Space jobs apart to avoid overwhelming feeds |
| 48 | $delayMinutes = $index * self::FEED_DISCOVERY_DELAY_MINUTES; |
| 49 | |
| 50 | self::dispatch($feed) |
| 51 | ->delay(now()->addMinutes($delayMinutes)) |
| 52 | ->onQueue('feed-discovery'); |
| 53 | |
| 54 | LogSaver::info('Dispatched feed discovery job', null, [ |
| 55 | 'feed_id' => $feed->id, |
| 56 | 'feed_name' => $feed->name, |
| 57 | 'delay_minutes' => $delayMinutes |
| 58 | ]); |
| 59 | }); |
| 60 | } |
| 61 | } |