73cc7e67d3
* feat(webhooks): multi region webhook resolver * feat(webhooks): multi region webhook cleanup * fix(webhooks): DI fixes * feat(activitystream): region aware save activity * feat(accessrequests): multi region * feat(cli): allow multi region project and commit download * feat(postgres): make docker postgres 0 day multi region ready * feat(cli): allow multi region project and commit download properly * fix(cross-server-sync): di fix * feat(activitystream): non region aware activities, they are not project data * fix(webhooks): triggers need to be included * feat(stream/projectCreate): activity save is not needed any more, its all event based * feat(multiRegion): get all registered db clients * fix(regions): test equal in any order * fix(projectDownload): need to await
44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import cron from 'node-cron'
|
|
import { SpeckleModule } from '@/modules/shared/helpers/typeHelper'
|
|
import { activitiesLogger, moduleLogger } from '@/logging/logging'
|
|
import { scheduleExecutionFactory } from '@/modules/core/services/taskScheduler'
|
|
import {
|
|
acquireTaskLockFactory,
|
|
releaseTaskLockFactory
|
|
} from '@/modules/core/repositories/scheduledTasks'
|
|
import { cleanOrphanedWebhookConfigsFactory } from '@/modules/webhooks/repositories/cleanup'
|
|
import { Knex } from 'knex'
|
|
import { db } from '@/db/knex'
|
|
import { getRegisteredDbClients } from '@/modules/multiregion/dbSelector'
|
|
|
|
const scheduleWebhookCleanupFactory = ({ db }: { db: Knex }) => {
|
|
const scheduleExecution = scheduleExecutionFactory({
|
|
acquireTaskLock: acquireTaskLockFactory({ db }),
|
|
releaseTaskLock: releaseTaskLockFactory({ db })
|
|
})
|
|
|
|
const cronExpression = '0 4 * * 1'
|
|
return scheduleExecution(cronExpression, 'weeklyWebhookCleanup', async () => {
|
|
activitiesLogger.info('Starting weekly webhooks cleanup')
|
|
const dbClients = await getRegisteredDbClients()
|
|
await Promise.all(
|
|
dbClients.map((regionDb) =>
|
|
cleanOrphanedWebhookConfigsFactory({ db: regionDb })()
|
|
)
|
|
)
|
|
await cleanOrphanedWebhookConfigsFactory({ db })()
|
|
activitiesLogger.info('Finished cleanup')
|
|
})
|
|
}
|
|
|
|
let scheduledTask: cron.ScheduledTask | null = null
|
|
|
|
export const init: SpeckleModule['init'] = () => {
|
|
moduleLogger.info('🎣 Init webhooks module')
|
|
scheduledTask = scheduleWebhookCleanupFactory({ db })
|
|
}
|
|
|
|
export const shutdown: SpeckleModule['shutdown'] = () => {
|
|
if (scheduledTask) scheduledTask.stop()
|
|
}
|