-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(deno): Refactor deno integrations to use functional syntax
- Loading branch information
Showing
5 changed files
with
168 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,56 @@ | ||
import { withMonitor } from '@sentry/core'; | ||
import type { Integration } from '@sentry/types'; | ||
import type { DenoClient } from '../client'; | ||
import { convertIntegrationFnToClass, withMonitor } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { parseScheduleToString } from './deno-cron-format'; | ||
|
||
type CronOptions = { backoffSchedule?: number[]; signal?: AbortSignal }; | ||
type CronFn = () => void | Promise<void>; | ||
// Parameters<typeof Deno.cron> doesn't work well with the overloads 🤔 | ||
type CronParams = [string, string | Deno.CronSchedule, CronFn | CronOptions, CronFn | CronOptions | undefined]; | ||
|
||
const INTEGRATION_NAME = 'DenoCron'; | ||
|
||
const denoCronIntegration: IntegrationFn = () => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
// eslint-disable-next-line deprecation/deprecation | ||
if (!Deno.cron) { | ||
// The cron API is not available in this Deno version use --unstable flag! | ||
return; | ||
} | ||
|
||
// eslint-disable-next-line deprecation/deprecation | ||
Deno.cron = new Proxy(Deno.cron, { | ||
apply(target, thisArg, argArray: CronParams) { | ||
const [monitorSlug, schedule, opt1, opt2] = argArray; | ||
let options: CronOptions | undefined; | ||
let fn: CronFn; | ||
|
||
if (typeof opt1 === 'function' && typeof opt2 !== 'function') { | ||
fn = opt1; | ||
options = opt2; | ||
} else if (typeof opt1 !== 'function' && typeof opt2 === 'function') { | ||
fn = opt2; | ||
options = opt1; | ||
} | ||
|
||
async function cronCalled(): Promise<void> { | ||
await withMonitor(monitorSlug, async () => fn(), { | ||
schedule: { type: 'crontab', value: parseScheduleToString(schedule) }, | ||
// (minutes) so 12 hours - just a very high arbitrary number since we don't know the actual duration of the users cron job | ||
maxRuntime: 60 * 12, | ||
// Deno Deploy docs say that the cron job will be called within 1 minute of the scheduled time | ||
checkinMargin: 1, | ||
}); | ||
} | ||
|
||
return target.call(thisArg, monitorSlug, schedule, options || {}, cronCalled); | ||
}, | ||
}); | ||
}, | ||
}; | ||
}; | ||
|
||
/** Instruments Deno.cron to automatically capture cron check-ins */ | ||
export class DenoCron implements Integration { | ||
/** @inheritDoc */ | ||
public static id = 'DenoCron'; | ||
|
||
/** @inheritDoc */ | ||
public name: string = DenoCron.id; | ||
|
||
/** @inheritDoc */ | ||
public setupOnce(): void { | ||
// | ||
} | ||
|
||
/** @inheritDoc */ | ||
public setup(): void { | ||
// eslint-disable-next-line deprecation/deprecation | ||
if (!Deno.cron) { | ||
// The cron API is not available in this Deno version use --unstable flag! | ||
return; | ||
} | ||
|
||
// eslint-disable-next-line deprecation/deprecation | ||
Deno.cron = new Proxy(Deno.cron, { | ||
apply(target, thisArg, argArray: CronParams) { | ||
const [monitorSlug, schedule, opt1, opt2] = argArray; | ||
let options: CronOptions | undefined; | ||
let fn: CronFn; | ||
|
||
if (typeof opt1 === 'function' && typeof opt2 !== 'function') { | ||
fn = opt1; | ||
options = opt2; | ||
} else if (typeof opt1 !== 'function' && typeof opt2 === 'function') { | ||
fn = opt2; | ||
options = opt1; | ||
} | ||
|
||
async function cronCalled(): Promise<void> { | ||
await withMonitor(monitorSlug, async () => fn(), { | ||
schedule: { type: 'crontab', value: parseScheduleToString(schedule) }, | ||
// (minutes) so 12 hours - just a very high arbitrary number since we don't know the actual duration of the users cron job | ||
maxRuntime: 60 * 12, | ||
// Deno Deploy docs say that the cron job will be called within 1 minute of the scheduled time | ||
checkinMargin: 1, | ||
}); | ||
} | ||
|
||
return target.call(thisArg, monitorSlug, schedule, options || {}, cronCalled); | ||
}, | ||
}); | ||
} | ||
} | ||
// eslint-disable-next-line deprecation/deprecation | ||
export const DenoCron = convertIntegrationFnToClass(INTEGRATION_NAME, denoCronIntegration); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.