node-schedule.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const common = require('./common.js');
  4. /**
  5. * Instruments the `node-schedule` library to send a check-in event to Sentry for each job execution.
  6. *
  7. * ```ts
  8. * import * as Sentry from '@sentry/node';
  9. * import * as schedule from 'node-schedule';
  10. *
  11. * const scheduleWithCheckIn = Sentry.cron.instrumentNodeSchedule(schedule);
  12. *
  13. * const job = scheduleWithCheckIn.scheduleJob('my-cron-job', '* * * * *', () => {
  14. * console.log('You will see this message every minute');
  15. * });
  16. * ```
  17. */
  18. function instrumentNodeSchedule(lib) {
  19. return new Proxy(lib, {
  20. get(target, prop) {
  21. if (prop === 'scheduleJob') {
  22. // eslint-disable-next-line @typescript-eslint/unbound-method
  23. return new Proxy(target.scheduleJob, {
  24. apply(target, thisArg, argArray) {
  25. const [nameOrExpression, expressionOrCallback] = argArray;
  26. if (typeof nameOrExpression !== 'string' || typeof expressionOrCallback !== 'string') {
  27. throw new Error(
  28. "Automatic instrumentation of 'node-schedule' requires the first parameter of 'scheduleJob' to be a job name string and the second parameter to be a crontab string",
  29. );
  30. }
  31. const monitorSlug = nameOrExpression;
  32. const expression = expressionOrCallback;
  33. return core.withMonitor(
  34. monitorSlug,
  35. () => {
  36. return target.apply(thisArg, argArray);
  37. },
  38. {
  39. schedule: { type: 'crontab', value: common.replaceCronNames(expression) },
  40. },
  41. );
  42. },
  43. });
  44. }
  45. return target[prop];
  46. },
  47. });
  48. }
  49. exports.instrumentNodeSchedule = instrumentNodeSchedule;
  50. //# sourceMappingURL=node-schedule.js.map