cron: delete old remote events every hour

This commit is contained in:
Alex Gleason 2023-09-04 19:45:33 -05:00
parent 08dd5fa4eb
commit 5d19c21f51
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 20 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import '@/cron.ts';
import {
type Context,
cors,

17
src/cron.ts Normal file
View File

@ -0,0 +1,17 @@
import * as eventsDB from '@/db/events.ts';
import { cron } from '@/deps.ts';
import { Time } from '@/utils/time.ts';
/** Clean up old remote events. */
async function cleanupEvents() {
console.log('Cleaning up old remote events...');
const [result] = await eventsDB.deleteFilters([{
until: Math.floor((Date.now() - Time.days(7)) / 1000),
local: false,
}]);
console.log(`Deleted ${result?.numDeletedRows ?? 0} events.`);
}
cron.hourly(cleanupEvents);

View File

@ -156,7 +156,7 @@ async function getFilters<K extends number>(
/** Delete events based on filters from the database. */
function deleteFilters<K extends number>(filters: DittoFilter<K>[]) {
if (!filters.length) return Promise.resolve();
if (!filters.length) return Promise.resolve([]);
const query = getFiltersQuery(filters);
return db

View File

@ -65,5 +65,6 @@ export {
} from 'npm:kysely@^0.25.0';
export { DenoSqliteDialect } from 'https://gitlab.com/soapbox-pub/kysely-deno-sqlite/-/raw/v1.0.1/mod.ts';
export { default as tldts } from 'npm:tldts@^6.0.14';
export * as cron from 'https://deno.land/x/deno_cron@v1.0.0/cron.ts';
export type * as TypeFest from 'npm:type-fest@^4.3.0';