Queues.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const Queue = require('bull')
  2. const Settings = require('@overleaf/settings')
  3. // Bull will keep a fixed number of the most recently completed jobs. This is
  4. // useful to inspect recently completed jobs. The bull prometheus exporter also
  5. // uses the completed job records to report on job duration.
  6. const MAX_COMPLETED_JOBS_RETAINED = 10000
  7. const MAX_FAILED_JOBS_RETAINED = 50000
  8. const QUEUES_JOB_OPTIONS = {
  9. 'analytics-events': {},
  10. 'analytics-editing-sessions': {},
  11. 'analytics-user-properties': {},
  12. 'refresh-features': {
  13. attempts: 3,
  14. },
  15. 'emails-onboarding': {},
  16. 'post-registration-analytics': {},
  17. 'scheduled-jobs': {
  18. attempts: 1,
  19. },
  20. }
  21. const queues = {}
  22. function getQueue(queueName) {
  23. if (!queues[queueName]) {
  24. const jobOptions = QUEUES_JOB_OPTIONS[queueName] || {}
  25. queues[queueName] = new Queue(queueName, {
  26. // this configuration is duplicated in /services/analytics/app/js/Queues.js
  27. // and needs to be manually kept in sync whenever modified
  28. redis: Settings.redis.queues,
  29. defaultJobOptions: {
  30. removeOnComplete: MAX_COMPLETED_JOBS_RETAINED,
  31. removeOnFail: MAX_FAILED_JOBS_RETAINED,
  32. attempts: 11,
  33. backoff: {
  34. type: 'exponential',
  35. delay: 3000,
  36. },
  37. ...jobOptions,
  38. },
  39. })
  40. }
  41. return queues[queueName]
  42. }
  43. async function createScheduledJob(queueName, { name, data, options }, delay) {
  44. await getQueue('scheduled-jobs').add(
  45. { queueName, name, data, options },
  46. {
  47. delay,
  48. }
  49. )
  50. }
  51. module.exports = {
  52. getQueue,
  53. createScheduledJob,
  54. }