instrumentation.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Background work, started once per Node server.
  3. *
  4. * `DISABLE_BACKGROUND_JOBS=1` leaves every timer unstarted, which is what a
  5. * test harness wants: the schedulers otherwise tick through a run, writing to
  6. * the same rows the tests are asserting on (the due-reminder scan stamps
  7. * `notifiedAt`, the message and webhook processors send things) and competing
  8. * for the one CPU the suite is using. A spec that needs one of these calls the
  9. * processor itself instead of waiting for its timer.
  10. */
  11. export async function register() {
  12. if (process.env.NEXT_RUNTIME === 'nodejs') {
  13. if (process.env.DISABLE_BACKGROUND_JOBS === '1') {
  14. const { warnAboutAppUrl } = await import('./lib/auth-origin-hint')
  15. warnAboutAppUrl()
  16. const { markBackgroundJobsDisabled } = await import('./lib/background-jobs')
  17. markBackgroundJobsDisabled()
  18. const { createLogger } = await import('./lib/logger')
  19. createLogger('cron').info('DISABLE_BACKGROUND_JOBS=1, no scheduled work started')
  20. return
  21. }
  22. const {
  23. checkLicenses,
  24. syncSubscriptions,
  25. processRecurringInvoices,
  26. cleanupPortalSessions,
  27. sweepOrphanFilesDaily,
  28. cleanupAuditLogs,
  29. processReportSchedules,
  30. processWebhookDeliveries,
  31. cleanupWebhookDeliveries,
  32. startDemoResetCron,
  33. checkLowStock,
  34. checkDueReminders,
  35. processScheduledMessages,
  36. processAppointmentNotifications,
  37. processIntegrationJobs,
  38. cleanupIntegrationLogs,
  39. processBackupRuns,
  40. syncWorkshopHolidays,
  41. startHeartbeat,
  42. } = await import('./cronTasks')
  43. const { warnAboutAppUrl } = await import('./lib/auth-origin-hint')
  44. warnAboutAppUrl()
  45. checkLicenses()
  46. syncSubscriptions()
  47. processRecurringInvoices()
  48. cleanupPortalSessions()
  49. sweepOrphanFilesDaily()
  50. cleanupAuditLogs()
  51. processReportSchedules()
  52. startDemoResetCron()
  53. processWebhookDeliveries()
  54. cleanupWebhookDeliveries()
  55. checkLowStock()
  56. checkDueReminders()
  57. processScheduledMessages()
  58. processAppointmentNotifications()
  59. processIntegrationJobs()
  60. cleanupIntegrationLogs()
  61. processBackupRuns()
  62. syncWorkshopHolidays()
  63. // Last, so the health check only sees beats once everything above started.
  64. startHeartbeat()
  65. }
  66. }