-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.php
More file actions
105 lines (90 loc) · 2.67 KB
/
scheduler.php
File metadata and controls
105 lines (90 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Feed refresh scheduler script.
*
* Queues feed refresh jobs for all feeds that need updating.
* Should be run periodically via cron (e.g., every 15-30 minutes).
*
* Usage:
* php scheduler.php [--interval=minutes]
*
* Example cron job (every 15 minutes):
* 0,15,30,45 * * * * cd /path/to/vibereader && php scheduler.php
*/
require_once __DIR__ . '/vendor/autoload.php';
use PhpRss\Config;
use PhpRss\Database;
use PhpRss\Queue\JobQueue;
// Initialize
Config::reset();
Database::init();
Database::setup();
// Parse command line arguments
$refreshInterval = 15; // Default: refresh feeds every 15 minutes
foreach ($argv as $arg) {
if (strpos($arg, '--interval=') === 0) {
$refreshInterval = (int)substr($arg, 11);
}
}
// Check if jobs are enabled
if (! Config::get('jobs.enabled', false)) {
echo "Background jobs are disabled. Set JOBS_ENABLED=1 to enable scheduled refreshes.\n";
exit(1);
}
$db = Database::getConnection();
$dbType = Database::getDbType();
// Get all feeds that need refreshing
// Refresh feeds that haven't been fetched in the last N minutes
if ($dbType === 'pgsql') {
$stmt = $db->query("
SELECT id, url, last_fetched
FROM feeds
WHERE last_fetched IS NULL
OR last_fetched < CURRENT_TIMESTAMP - INTERVAL '{$refreshInterval} minutes'
ORDER BY last_fetched ASC NULLS FIRST
");
} else {
// SQLite
$stmt = $db->query("
SELECT id, url, last_fetched
FROM feeds
WHERE last_fetched IS NULL
OR last_fetched < datetime('now', '-{$refreshInterval} minutes')
ORDER BY last_fetched ASC
");
}
$feeds = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$queued = 0;
$skipped = 0;
foreach ($feeds as $feed) {
// Check if there's already a pending job for this feed
$existingJob = $db->prepare("
SELECT id FROM jobs
WHERE type = ?
AND payload LIKE ?
AND status = ?
LIMIT 1
");
$payloadPattern = '%"feed_id":' . $feed['id'] . '%';
$existingJob->execute([
JobQueue::TYPE_FETCH_FEED,
$payloadPattern,
JobQueue::STATUS_PENDING
]);
if ($existingJob->fetch()) {
// Job already queued for this feed, skip
$skipped++;
continue;
}
// Queue the refresh job
try {
JobQueue::push(
JobQueue::TYPE_FETCH_FEED,
['feed_id' => (int)$feed['id']]
);
$queued++;
} catch (\Exception $e) {
echo "Error queueing feed {$feed['id']}: " . $e->getMessage() . "\n";
}
}
echo "Scheduled refresh: {$queued} feeds queued, {$skipped} already queued, " . count($feeds) . " total need refreshing\n";