Skip to content

Commit 1dd49b6

Browse files
committed
Simplified refresh system, made feed expiry configurable
1 parent 0ad8bf0 commit 1dd49b6

File tree

7 files changed

+87
-27
lines changed

7 files changed

+87
-27
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ APP_CONFIG_FILE=/app/storage/feeds.txt
148148
# when posts are fetched.
149149
# Defaults to true.
150150
APP_LOAD_POST_THUMBNAILS=true
151+
152+
# The number of minutes before a feed is considered outdated and
153+
# therefore should be updated upon request.
154+
# This effectively has a minimum of 5 minutes in the docker setup.
155+
APP_FEED_UPDATE_FREQUENCY=60
151156
```
152157

153158
## Manual Install

app/Config/ConfiguredFeed.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function jsonSerialize(): mixed
3232

3333
public function isOutdated(): bool
3434
{
35-
$expiry = time() - 3600;
35+
$configFrequency = intval(config('app.feed_update_frequency'));
36+
$expiry = time() - intval($configFrequency * 60);
3637
return $this->feed->last_fetched_at <= $expiry;
3738
}
3839

app/Console/Commands/UpdateFeedsCommand.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace App\Console\Commands;
44

5-
use App\Jobs\RefreshFeedJob;
6-
use App\Models\Feed;
5+
use App\Config\ConfiguredFeedProvider;
76
use Illuminate\Console\Command;
8-
use Illuminate\Database\Eloquent\Builder;
9-
use Illuminate\Database\Eloquent\Collection;
107

118
class UpdateFeedsCommand extends Command
129
{
@@ -15,40 +12,24 @@ class UpdateFeedsCommand extends Command
1512
*
1613
* @var string
1714
*/
18-
protected $signature = 'rss:update-outdated-feeds
19-
{--all : Also update dormant feeds}
20-
{--outdated-time=24 : Age (in hours) that\'s considered as outdated}';
15+
protected $signature = 'rss:update-outdated-feeds';
2116

2217
/**
2318
* The console command description.
2419
*
2520
* @var string
2621
*/
27-
protected $description = 'Trigger the processing of outdated feeds';
22+
protected $description = 'Trigger the update of outdated feeds';
2823

2924
/**
3025
* Execute the console command.
3126
*
3227
* @return int
3328
*/
34-
public function handle()
29+
public function handle(ConfiguredFeedProvider $feedProvider)
3530
{
36-
$queryDormant = $this->hasOption('all');
37-
$dormantTime = time() - (86400 * 90); // 90 days
38-
$outdatedTime = time() - ceil(floatval($this->option('outdated-time')) * 3600);
39-
$dormantQuery = function(Builder $query) use ($dormantTime) {
40-
$query->where('last_accessed_at', '>', $dormantTime);
41-
};
42-
43-
Feed::query()
44-
->when(!$queryDormant, $dormantQuery)
45-
->where('last_fetched_at', '<', $outdatedTime)
46-
->chunk(100, function(Collection $feeds) {
47-
/** @var Feed $feed */
48-
foreach ($feeds as $feed) {
49-
dispatch(new RefreshFeedJob($feed));
50-
}
51-
});
31+
$feeds = $feedProvider->getAll();
32+
$feeds->reloadOutdatedFeeds();
5233

5334
return 0;
5435
}

app/Console/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Kernel extends ConsoleKernel
1616
*/
1717
protected function schedule(Schedule $schedule)
1818
{
19-
$schedule->command(UpdateFeedsCommand::class, ['--outdated-time=0'])->hourly();
19+
$schedule->command(UpdateFeedsCommand::class)->everyFiveMinutes();
2020
}
2121

2222
/**

config/app.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444
'load_post_thumbnails' => env('APP_LOAD_POST_THUMBNAILS', true),
4545

4646

47+
/*
48+
|--------------------------------------------------------------------------
49+
| Application Feed Update Frequency
50+
|--------------------------------------------------------------------------
51+
|
52+
| This value specifies how often a feed should be updated. This is not a
53+
| specific guarantee of update on this interval but instead the age in
54+
| minutes of when a feed would be considered outdated.
55+
|
56+
*/
57+
58+
'feed_update_frequency' => env('APP_FEED_UPDATE_FREQUENCY', 60),
59+
60+
4761
/*
4862
|--------------------------------------------------------------------------
4963
| Application Environment

phpunit.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626
<env name="QUEUE_CONNECTION" value="sync"/>
2727
<env name="SESSION_DRIVER" value="array"/>
2828
<env name="TELESCOPE_ENABLED" value="false"/>
29+
<env name="APP_FEED_UPDATE_FREQUENCY" value="60"/>
30+
<env name="APP_LOAD_POST_THUMBNAILS" value="true"/>
2931
</php>
3032
</phpunit>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Config\ConfiguredFeed;
6+
use App\Jobs\RefreshFeedJob;
7+
use App\Models\Feed;
8+
use Illuminate\Support\Facades\Queue;
9+
use Tests\TestCase;
10+
11+
class ConfiguredFeedTest extends TestCase
12+
{
13+
14+
public function test_is_outdated_can_be_controlled_by_config()
15+
{
16+
$now = time();
17+
$feed = new Feed();
18+
$feed->last_fetched_at = $now - (59 * 60);
19+
20+
$configuredFeed = new ConfiguredFeed(
21+
$feed,
22+
'My great feed',
23+
'https://example.com',
24+
'#fff',
25+
['#a']
26+
);
27+
28+
config()->set('app.feed_update_frequency', 60);
29+
$this->assertFalse($configuredFeed->isOutdated());
30+
31+
$feed->last_fetched_at = $now - (61 * 60);
32+
$this->assertTrue($configuredFeed->isOutdated());
33+
34+
config()->set('app.feed_update_frequency', 5);
35+
$feed->last_fetched_at = $now - (4 * 60);
36+
$this->assertFalse($configuredFeed->isOutdated());
37+
38+
$feed->last_fetched_at = $now - (6 * 60);
39+
$this->assertTrue($configuredFeed->isOutdated());
40+
}
41+
42+
public function test_start_reloading_dispatched_refresh_job()
43+
{
44+
$configuredFeed = new ConfiguredFeed(
45+
new Feed(),
46+
'My great feed',
47+
'https://example.com',
48+
'#fff',
49+
['#a']
50+
);
51+
Queue::fake();
52+
53+
$configuredFeed->startReloading();
54+
Queue::assertPushed(RefreshFeedJob::class);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)