Repository navigation
Expand file tree
/
Copy pathspam-purge.ts
More file actions
59 lines (52 loc) · 1.82 KB
/
Copy pathspam-purge.ts
File metadata and controls
59 lines (52 loc) · 1.82 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
/**
* Scheduled spam purge plugin — specs/behaviors/person-lifecycle.md →
* "Scheduled spam purge". Runs once when the server is ready and then daily;
* in-process because the purge writes through the store's single writer.
*/
import type { FastifyInstance } from 'fastify';
import fp from 'fastify-plugin';
import { SPAM_PURGE_INTERVAL_MS, SpamPurgeService } from '../services/spam-purge.js';
/**
* Whether this pod runs the sweep: only when CFP_SCHEDULED_PURGE is "true",
* and never under tests (they call the sweep directly).
*/
export function scheduledPurgeEnabled(config: { NODE_ENV: string; CFP_SCHEDULED_PURGE: string }): boolean {
return config.NODE_ENV !== 'test' && config.CFP_SCHEDULED_PURGE === 'true';
}
declare module 'fastify' {
interface FastifyInstance {
spamPurge: SpamPurgeService;
}
}
async function spamPurgePlugin(fastify: FastifyInstance): Promise<void> {
const service = new SpamPurgeService({
state: fastify.inMemoryState,
fts: fastify.fts,
store: fastify.store,
log: fastify.log,
sessionMetadata: fastify.sessionMetadata,
});
fastify.decorate('spamPurge', service);
const run = (): void => {
void service.sweep().catch((err) => {
fastify.log.error({ err }, 'scheduled spam purge failed');
});
};
let interval: ReturnType<typeof setInterval> | undefined;
fastify.addHook('onReady', async () => {
if (!scheduledPurgeEnabled(fastify.config)) {
fastify.log.info('scheduled spam purge disabled (CFP_SCHEDULED_PURGE is not "true")');
return;
}
run();
interval = setInterval(run, SPAM_PURGE_INTERVAL_MS);
});
fastify.addHook('onClose', async () => {
if (interval) clearInterval(interval);
});
}
export default fp(spamPurgePlugin, {
name: 'spam-purge',
fastify: '5.x',
dependencies: ['services', 'session-middleware'],
});