Make it public!
This commit is contained in:
23
crons/cleanup.ts
Normal file
23
crons/cleanup.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import Database, { sql } from '/lib/interfaces/database.ts';
|
||||
|
||||
const db = new Database();
|
||||
|
||||
export async function cleanupSessions() {
|
||||
const yesterday = new Date(new Date().setUTCDate(new Date().getUTCDate() - 1));
|
||||
|
||||
try {
|
||||
const result = await db.query<{ count: number }>(
|
||||
sql`WITH "deleted" AS (
|
||||
DELETE FROM "bewcloud_user_sessions" WHERE "expires_at" <= $1 RETURNING *
|
||||
)
|
||||
SELECT COUNT(*) FROM "deleted"`,
|
||||
[
|
||||
yesterday.toISOString().substring(0, 10),
|
||||
],
|
||||
);
|
||||
|
||||
console.log('Deleted', result[0].count, 'user sessions');
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
32
crons/index.ts
Normal file
32
crons/index.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Cron } from 'https://deno.land/x/croner@7.0.1/dist/croner.js';
|
||||
|
||||
import { cleanupSessions } from './cleanup.ts';
|
||||
import { fetchNewArticles } from './news.ts';
|
||||
|
||||
export function startCrons() {
|
||||
new Cron(
|
||||
// At 03:06 every day.
|
||||
'6 3 * * *',
|
||||
{
|
||||
name: 'cleanup',
|
||||
protect: true,
|
||||
},
|
||||
async () => {
|
||||
await cleanupSessions();
|
||||
},
|
||||
);
|
||||
|
||||
new Cron(
|
||||
// Every 30 minutes.
|
||||
'*/30 * * * *',
|
||||
{
|
||||
name: 'news',
|
||||
protect: true,
|
||||
},
|
||||
async () => {
|
||||
await fetchNewArticles();
|
||||
},
|
||||
);
|
||||
|
||||
console.log('Crons starting...');
|
||||
}
|
||||
25
crons/news.ts
Normal file
25
crons/news.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import Database, { sql } from '/lib/interfaces/database.ts';
|
||||
import { NewsFeed } from '/lib/types.ts';
|
||||
import { concurrentPromises } from '/lib/utils.ts';
|
||||
import { crawlNewsFeed } from '/lib/data/news.ts';
|
||||
|
||||
const db = new Database();
|
||||
|
||||
export async function fetchNewArticles(forceFetch = false) {
|
||||
const fourHoursAgo = forceFetch ? new Date() : new Date(new Date().setUTCHours(new Date().getUTCHours() - 4));
|
||||
|
||||
try {
|
||||
const feedsToCrawl = await db.query<NewsFeed>(
|
||||
sql`SELECT * FROM "bewcloud_news_feeds" WHERE "last_crawled_at" IS NULL OR "last_crawled_at" <= $1`,
|
||||
[
|
||||
fourHoursAgo.toISOString().substring(0, 10),
|
||||
],
|
||||
);
|
||||
|
||||
await concurrentPromises(feedsToCrawl.map((newsFeed) => () => crawlNewsFeed(newsFeed)), 3);
|
||||
|
||||
console.log('Crawled', feedsToCrawl.length, 'news feeds');
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user