Add Cron to delete old, read articles. Update Deno.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
FROM denoland/deno:ubuntu-1.46.2
|
FROM denoland/deno:ubuntu-1.46.3
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
|
|||||||
@@ -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 { isAppEnabled } from '/lib/config.ts';
|
||||||
import { cleanupSessions } from './cleanup.ts';
|
import { cleanupSessions } from './sessions.ts';
|
||||||
import { fetchNewArticles } from './news.ts';
|
import { cleanupOldArticles, fetchNewArticles } from './news.ts';
|
||||||
|
|
||||||
export function startCrons() {
|
export function startCrons() {
|
||||||
new Cron(
|
new Cron(
|
||||||
@@ -14,6 +14,10 @@ export function startCrons() {
|
|||||||
},
|
},
|
||||||
async () => {
|
async () => {
|
||||||
await cleanupSessions();
|
await cleanupSessions();
|
||||||
|
|
||||||
|
if (isAppEnabled('news')) {
|
||||||
|
await cleanupOldArticles();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -25,3 +25,44 @@ export async function fetchNewArticles(forceFetch = false) {
|
|||||||
console.error(error);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user