node-schedule.js 1.7 KB

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