{"version":3,"file":"node-cron.js","sources":["../../../src/cron/node-cron.ts"],"sourcesContent":["import { captureException, type MonitorConfig, withMonitor } from '@sentry/core';\nimport { replaceCronNames } from './common';\n\nexport interface NodeCronOptions {\n  name: string;\n  timezone?: string;\n}\n\nexport interface NodeCron {\n  schedule: (\n    cronExpression: string,\n    callback: (context?: unknown) => void,\n    options: NodeCronOptions | undefined,\n  ) => unknown;\n}\n\n/**\n * Wraps the `node-cron` library with check-in monitoring.\n *\n * ```ts\n * import * as Sentry from \"@sentry/node\";\n * import * as cron from \"node-cron\";\n *\n * const cronWithCheckIn = Sentry.cron.instrumentNodeCron(cron);\n *\n * cronWithCheckIn.schedule(\n *   \"* * * * *\",\n *   () => {\n *     console.log(\"running a task every minute\");\n *   },\n *   { name: \"my-cron-job\" },\n * );\n * ```\n */\nexport function instrumentNodeCron<T>(\n  lib: Partial<NodeCron> & T,\n  monitorConfig: Pick<MonitorConfig, 'isolateTrace'> = {},\n): T {\n  return new Proxy(lib, {\n    get(target, prop) {\n      if (prop === 'schedule' && target.schedule) {\n        // When 'get' is called for schedule, return a proxied version of the schedule function\n        return new Proxy(target.schedule, {\n          apply(target, thisArg, argArray: Parameters<NodeCron['schedule']>) {\n            const [expression, callback, options] = argArray;\n\n            const name = options?.name;\n            const timezone = options?.timezone;\n\n            if (!name) {\n              throw new Error('Missing \"name\" for scheduled job. A name is required for Sentry check-in monitoring.');\n            }\n\n            const monitoredCallback = async (...args: Parameters<typeof callback>): Promise<void> => {\n              return withMonitor(\n                name,\n                async () => {\n                  // We have to manually catch here and capture the exception because node-cron swallows errors\n                  // https://github.com/node-cron/node-cron/issues/399\n                  try {\n                    // oxlint-disable-next-line typescript/await-thenable -- callback may be async at runtime\n                    return await callback(...args);\n                  } catch (e) {\n                    captureException(e, {\n                      mechanism: {\n                        handled: false,\n                        type: 'auto.function.node-cron.instrumentNodeCron',\n                      },\n                    });\n                    throw e;\n                  }\n                },\n                {\n                  schedule: { type: 'crontab', value: replaceCronNames(expression) },\n                  timezone,\n                  ...monitorConfig,\n                },\n              );\n            };\n\n            return target.apply(thisArg, [expression, monitoredCallback, options]);\n          },\n        });\n      } else {\n        return target[prop as keyof T];\n      }\n    },\n  });\n}\n"],"names":[],"mappings":";;;AAgBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,kBAAkB;AAClC,EAAE,GAAG;AACL,EAAE,aAAa,GAAwC,EAAE;AACzD,EAAK;AACL,EAAE,OAAO,IAAI,KAAK,CAAC,GAAG,EAAE;AACxB,IAAI,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE;AACtB,MAAM,IAAI,IAAA,KAAS,cAAc,MAAM,CAAC,QAAQ,EAAE;AAClD;AACA,QAAQ,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE;AAC1C,UAAU,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAoC;AAC7E,YAAY,MAAM,CAAC,UAAU,EAAE,QAAQ,EAAE,OAAO,CAAA,GAAI,QAAQ;;AAE5D,YAAY,MAAM,IAAA,GAAO,OAAO,EAAE,IAAI;AACtC,YAAY,MAAM,QAAA,GAAW,OAAO,EAAE,QAAQ;;AAE9C,YAAY,IAAI,CAAC,IAAI,EAAE;AACvB,cAAc,MAAM,IAAI,KAAK,CAAC,sFAAsF,CAAC;AACrH,YAAY;;AAEZ,YAAY,MAAM,iBAAA,GAAoB,OAAO,GAAG,IAAI,KAAiD;AACrG,cAAc,OAAO,WAAW;AAChC,gBAAgB,IAAI;AACpB,gBAAgB,YAAY;AAC5B;AACA;AACA,kBAAkB,IAAI;AACtB;AACA,oBAAoB,OAAO,MAAM,QAAQ,CAAC,GAAG,IAAI,CAAC;AAClD,kBAAkB,CAAA,CAAE,OAAO,CAAC,EAAE;AAC9B,oBAAoB,gBAAgB,CAAC,CAAC,EAAE;AACxC,sBAAsB,SAAS,EAAE;AACjC,wBAAwB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