-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathcleanupActivity.ts
More file actions
33 lines (28 loc) · 1.3 KB
/
cleanupActivity.ts
File metadata and controls
33 lines (28 loc) · 1.3 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
import { WRITE_DB_CONFIG, getDbConnection } from '@crowd/database'
import { getServiceChildLogger } from '@crowd/logging'
import { SlackChannel, SlackPersona, sendSlackNotification } from '@crowd/slack'
import { MetadataStore, S3Service } from '@crowd/snowflake'
const log = getServiceChildLogger('cleanupActivity')
const PLATFORM = 'pcc'
export async function executeCleanup(intervalHours = 24): Promise<void> {
const db = await getDbConnection(WRITE_DB_CONFIG())
const metadataStore = new MetadataStore(db)
const s3Service = new S3Service()
const jobs = await metadataStore.getCleanableJobS3Paths(intervalHours, PLATFORM, false)
log.info({ jobCount: jobs.length, intervalHours }, 'Found cleanable PCC jobs')
for (const job of jobs) {
try {
await s3Service.deleteFile(job.s3Path)
await metadataStore.markCleaned(job.id)
log.info({ jobId: job.id, s3Path: job.s3Path }, 'Cleaned PCC job')
} catch (err) {
log.error({ jobId: job.id, s3Path: job.s3Path, err }, 'Failed to clean PCC job, skipping')
sendSlackNotification(
SlackChannel.CDP_INTEGRATIONS_ALERTS,
SlackPersona.ERROR_REPORTER,
'PCC S3 Cleanup Failed',
`Failed to clean job \`${job.id}\` at \`${job.s3Path}\`.\n\n*Error:* ${err instanceof Error ? err.message : err}`,
)
}
}
}