Source: scheduler/index.js

  1. /**
  2. * @module scheduler
  3. */
  4. const schedule = require('node-schedule');
  5. const recache = require("../cache/recache");
  6. const config = require("../../config").scheduler;
  7. const log = require("../../util/log");
  8. const downloadService = require("../download");
  9. const adminService = require("../admin");
  10. /**
  11. * Schedule the 'recacheAll()' task according to the configuration module.
  12. */
  13. const scheduleRecache = () => {
  14. if (process.env.SCHEDULE_RECACHE === '1') {
  15. schedule.scheduleJob(config.recache,
  16. /* istanbul ignore next */
  17. async () => {
  18. log.info("SCHEDULER: Executing scheduled task 'recacheAll()' at %s...", (new Date).toUTCString());
  19. await recache.recacheAll();
  20. });
  21. } else {
  22. log.info("SCHEDULER: SKIPPING scheduling of task 'recacheAll()' due to environment config");
  23. }
  24. };
  25. /**
  26. * Schedule the 'deleteExportFiles()' task according to the configuration module.
  27. */
  28. const scheduleDeleteExportFiles = () => {
  29. schedule.scheduleJob(config.deleteExports,
  30. /* istanbul ignore next */
  31. async () => {
  32. log.info("SCHEDULER: Executing scheduled task 'deleteExportFiles()' at %s...",
  33. (new Date).toUTCString());
  34. downloadService.deleteExportFiles();
  35. });
  36. };
  37. /**
  38. * Schedule the 'sendSummary()' task according to the configuration module.
  39. */
  40. const scheduleWeeklySummary = () => {
  41. schedule.scheduleJob(config.weeklySummary,
  42. /* istanbul ignore next */
  43. async () => {
  44. log.info("SCHEDULER: Executing scheduled task 'scheduleWeeklySummary()' at %s...",
  45. (new Date).toUTCString());
  46. adminService.sendSummary();
  47. });
  48. };
  49. /**
  50. * Schedule all tasks.
  51. */
  52. const scheduleAllTasks = () => {
  53. scheduleRecache();
  54. scheduleDeleteExportFiles();
  55. scheduleWeeklySummary();
  56. };
  57. module.exports = {
  58. scheduleAllTasks,
  59. };