8b386ff078
* feat(server task scheduler): sketch out core task scheduler implementation * feat(server weekly activity digests): add function lock duration to the weekly digest execution * feat(server scheduled tasks): add scheduled tasks type definition, db schema and migration * feat(server scheduled tasks): add scheduled tasks repository * feat(server task scheduler): add task scheduler service implementation * chore(server deps): add mocha type definitions * refactor(server scheduled tasks): refactor scheduled tasks migration * refactor(server scheduled tasks): refactor scheduled task db schema and type definitions * feat(server scheduled tasks): implement db side lock acquire * refactor(server scheduled tasks): refactor task scheduler with lock on query mechanism * test(server scheduled tasks): add tests for scheduled tasks implementation * refactor(server weekly activity digests): refactor to new task scheduler implementation * feat(server weekly activity digest): switch to a 1000 seconds trigger period for testing purposes * fix(server task scheduler): fix not catching lock acquire function errors * feat(server weekly digest): switch weekly digest cron trigger to the prod ready value
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import { SpeckleModule } from '@/modules/shared/helpers/typeHelper'
|
|
import cron from 'node-cron'
|
|
import { sendActivityNotifications } from '@/modules/activitystream/services/summary'
|
|
import { initializeEventListener } from '@/modules/activitystream/services/eventListener'
|
|
import { modulesDebug } from '@/modules/shared/utils/logger'
|
|
import { publishNotification } from '@/modules/notifications/services/publication'
|
|
import { scheduleExecution } from '@/modules/core/services/taskScheduler'
|
|
|
|
const activitiesDebug = modulesDebug.extend('activities')
|
|
|
|
let scheduledTask: cron.ScheduledTask | null = null
|
|
|
|
const scheduleWeeklyActivityNotifications = () => {
|
|
// just to test stuff
|
|
// every 1000 seconds
|
|
// const cronExpression = '*/1000 * * * * *'
|
|
// at 00 minutest, 10 (am) hours, every month, every year,
|
|
// every 1st day of the week (monday)
|
|
// cheat sheet https://crontab.guru
|
|
const cronExpression = '00 10 * * 1'
|
|
// configure the number of days, the activities are scraped for
|
|
const numberOfDays = 7
|
|
return scheduleExecution(
|
|
cronExpression,
|
|
'weeklyActivityNotification',
|
|
//task should be locked for 10 minutes
|
|
async (now: Date) => {
|
|
activitiesDebug('Sending weekly activity digests notifications.')
|
|
const end = now
|
|
const start = new Date(end.getTime())
|
|
start.setDate(start.getDate() - numberOfDays)
|
|
await sendActivityNotifications(start, end, publishNotification)
|
|
},
|
|
10 * 60 * 1000
|
|
)
|
|
}
|
|
|
|
const activityModule: SpeckleModule = {
|
|
init: async (_, isInitial) => {
|
|
modulesDebug('🤺 Init activity module')
|
|
if (isInitial) {
|
|
initializeEventListener()
|
|
scheduledTask = scheduleWeeklyActivityNotifications()
|
|
}
|
|
},
|
|
shutdown: () => {
|
|
if (scheduledTask) scheduledTask.stop()
|
|
}
|
|
}
|
|
|
|
export = {
|
|
...activityModule
|
|
}
|