Add Cron to delete old, read articles. Update Deno.

This commit is contained in:
Bruno Bernardino
2024-10-14 15:13:55 +01:00
parent 8062df1bb5
commit fbcaab53d0
5 changed files with 50 additions and 5 deletions

2
.dvmrc
View File

@@ -1 +1 @@
1.46.2
1.46.3

View File

@@ -1,4 +1,4 @@
FROM denoland/deno:ubuntu-1.46.2
FROM denoland/deno:ubuntu-1.46.3
EXPOSE 8000

View File

@@ -1,8 +1,8 @@
import { Cron } from 'https://deno.land/x/croner@8.0.1/dist/croner.js';
import { Cron } from 'https://deno.land/x/croner@8.1.2/dist/croner.js';
import { isAppEnabled } from '/lib/config.ts';
import { cleanupSessions } from './cleanup.ts';
import { fetchNewArticles } from './news.ts';
import { cleanupSessions } from './sessions.ts';
import { cleanupOldArticles, fetchNewArticles } from './news.ts';
export function startCrons() {
new Cron(
@@ -14,6 +14,10 @@ export function startCrons() {
},
async () => {
await cleanupSessions();
if (isAppEnabled('news')) {
await cleanupOldArticles();
}
},
);

View File

@@ -25,3 +25,44 @@ export async function fetchNewArticles(forceFetch = false) {
console.error(error);
}
}
export async function cleanupOldArticles() {
const oneMonthAgo = new Date(new Date().setUTCMonth(new Date().getUTCMonth() - 1));
try {
console.info('Will cleanup old articles');
const feedIdsToSkip = new Set<string>();
const feeds = await db.query<Pick<NewsFeed, 'id' | 'feed_url'>>(
sql`SELECT "id", "feed_url" FROM "bewcloud_news_feeds" ORDER BY "last_crawled_at" ASC`,
);
for (const feed of feeds) {
const recentArticlesCount = (await db.query<{ count: number }>(
sql`SELECT COUNT("id") AS "count" FROM "bewcloud_news_feed_articles" WHERE "feed_id" = $1 AND "article_date" >= $2`,
[feed.id, oneMonthAgo],
))[0].count;
// Don't delete old articles if the feed doesn't have recent articles (to skip feeds with less items in total)
if (recentArticlesCount <= 5) {
feedIdsToSkip.add(feed.id);
}
}
const result = await db.query<{ count: number }>(
sql`WITH "deleted" AS (
DELETE FROM "bewcloud_news_feed_articles" WHERE "is_read" = TRUE AND "article_date" <= $1 AND "feed_id" != ANY($2) RETURNING *
)
SELECT COUNT(*) FROM "deleted"`,
[
oneMonthAgo.toISOString().substring(0, 10),
[...feedIdsToSkip],
],
);
console.info('Deleted', result[0].count, 'old articles');
} catch (error) {
console.error(error);
}
}