-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ref(deno): Refactor deno integrations to use functional syntax #9929
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,65 @@ | ||
import { withMonitor } from '@sentry/core'; | ||
import type { Integration } from '@sentry/types'; | ||
import type { DenoClient } from '../client'; | ||
import { convertIntegrationFnToClass, getClient, withMonitor } from '@sentry/core'; | ||
import type { Client, 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 SETUP_CLIENTS: Client[] = []; | ||
|
||
const denoCronIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
mydea marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AbhiPrasad / @timfish , ok, hopefully final refactor here - now this registers once in |
||
// 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> { | ||
if (SETUP_CLIENTS.includes(getClient() as Client)) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we still want to call |
||
} | ||
|
||
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); | ||
}, | ||
}); | ||
}, | ||
setup(client) { | ||
SETUP_CLIENTS.push(client); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that clients dont get GC'd - I wonder if we should track with a WeakMap instead?