Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import { MeteringMetricsSchedulerConfig } from './metering-scheduler.config';
import { MeteringSchedulerService } from './metering-scheduler.service';
import type { MetricsReportAllResult } from './metering.types';

jest.mock('@nestjs/schedule', () =>
jest
.requireActual('../testing')
.withSentryCronWrapper(jest.requireActual('@nestjs/schedule'))
);

const METRICS_RESULT: MetricsReportAllResult = { total: 2, failed: 0 };
const OTHER_INSTANCE = 'other-instance';
const HELD_MS = 60_000;
Expand Down
4 changes: 2 additions & 2 deletions libs/metering/src/lib/metering-metrics-scheduler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export class MeteringMetricsSchedulerService implements OnApplicationBootstrap {
);
}

@OnOneInstance({ key: 'metrics' })
@WithoutOverlapping()
@Cron(SCHEDULED_AT_BOOTSTRAP, {
name: 'metrics',
waitForCompletion: true,
disabled: true,
})
@OnOneInstance({ key: 'metrics' })
@WithoutOverlapping()
async reportMetrics(): Promise<void> {
await this.meteringSchedulerService.run('metrics', async () => {
const result = await this.meteringMetricsService.reportAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import { MeteringSweepService } from './metering-sweep.service';
import { UsageRefillService } from './usage-refill.service';
import type { SweepAllResult } from './metering.types';

jest.mock('@nestjs/schedule', () =>
jest
.requireActual('../testing')
.withSentryCronWrapper(jest.requireActual('@nestjs/schedule'))
);

jest.mock('@sentry/nestjs', () => ({
captureException: jest.fn(),
}));
Expand Down
4 changes: 2 additions & 2 deletions libs/metering/src/lib/metering-sweep-scheduler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ export class MeteringSweepSchedulerService implements OnApplicationBootstrap {
);
}

@OnOneInstance({ key: 'sweep' })
@WithoutOverlapping()
@Cron(SCHEDULED_AT_BOOTSTRAP, {
name: 'sweep',
waitForCompletion: true,
disabled: true,
})
@OnOneInstance({ key: 'sweep' })
@WithoutOverlapping()
async sweep(): Promise<void> {
await this.meteringSchedulerService.run('sweep', async () => {
const refilled = await this.refillUsageCounters();
Expand Down
24 changes: 24 additions & 0 deletions libs/metering/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,27 @@ export function anchoredClock(): {
at: (offsetMs: number) => new Date(anchor.getTime() + offsetMs),
};
}

/**
* `@nestjs/schedule` with `@Cron` replacing the method with a plain wrapper, as
* Sentry's Nest instrumentation does in production.
*/
export function withSentryCronWrapper<
T extends { Cron: (...args: never[]) => MethodDecorator },
>(schedule: T): T {
return {
...schedule,
Cron: (...args: Parameters<T['Cron']>): MethodDecorator => {
const decorate = schedule.Cron(...args);
return (target, propertyKey, descriptor) => {
const handler = descriptor.value as unknown as (
...handlerArgs: unknown[]
) => unknown;
descriptor.value = function (this: unknown, ...handlerArgs: unknown[]) {
return handler.apply(this, handlerArgs);
} as typeof descriptor.value;
return decorate(target, propertyKey, descriptor);
};
},
};
}