node-cron.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var {
  2. _optionalChain
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const core = require('@sentry/core');
  6. const common = require('./common.js');
  7. /**
  8. * Wraps the `node-cron` library with check-in monitoring.
  9. *
  10. * ```ts
  11. * import * as Sentry from "@sentry/node";
  12. * import cron from "node-cron";
  13. *
  14. * const cronWithCheckIn = Sentry.cron.instrumentNodeCron(cron);
  15. *
  16. * cronWithCheckIn.schedule(
  17. * "* * * * *",
  18. * () => {
  19. * console.log("running a task every minute");
  20. * },
  21. * { name: "my-cron-job" },
  22. * );
  23. * ```
  24. */
  25. function instrumentNodeCron(lib) {
  26. return new Proxy(lib, {
  27. get(target, prop) {
  28. if (prop === 'schedule' && target.schedule) {
  29. // When 'get' is called for schedule, return a proxied version of the schedule function
  30. return new Proxy(target.schedule, {
  31. apply(target, thisArg, argArray) {
  32. const [expression, , options] = argArray;
  33. if (!_optionalChain([options, 'optionalAccess', _ => _.name])) {
  34. throw new Error('Missing "name" for scheduled job. A name is required for Sentry check-in monitoring.');
  35. }
  36. return core.withMonitor(
  37. options.name,
  38. () => {
  39. return target.apply(thisArg, argArray);
  40. },
  41. {
  42. schedule: { type: 'crontab', value: common.replaceCronNames(expression) },
  43. timezone: _optionalChain([options, 'optionalAccess', _2 => _2.timezone]),
  44. },
  45. );
  46. },
  47. });
  48. } else {
  49. return target[prop];
  50. }
  51. },
  52. });
  53. }
  54. exports.instrumentNodeCron = instrumentNodeCron;
  55. //# sourceMappingURL=node-cron.js.map