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
22 changes: 22 additions & 0 deletions services/cloud-agent-next/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const {
default: worker,
REPORT_RETENTION_CRON,
OUTCOME_AGGREGATE_CRON,
OUTCOME_AGGREGATE_CRONS,
} = await import('./server.js');

const secret = 'test-secret';
Expand Down Expand Up @@ -424,6 +425,26 @@ describe('server background reporting', () => {
expect(removeExpiredCloudAgentReportDataMock).not.toHaveBeenCalled();
});

it('runs the collections on the tolerated legacy 3-minute cron', async () => {
const env = createEnv();
const scheduledTime = Date.parse('2026-02-01T00:10:42.000Z');

await worker.scheduled(
{ cron: '*/3 * * * *', scheduledTime } as ScheduledController,
env as unknown as Env
);

expect(runCloudAgentOutcomeCollectionMock).toHaveBeenCalledTimes(1);
expect(runCloudAgentOutcomeCollectionMock).toHaveBeenCalledWith(
env,
expect.any(Date),
scheduledTime
);
expect(runCloudAgentOpenStockCollectionMock).toHaveBeenCalledTimes(1);
expect(runCloudAgentOpenStockCollectionMock).toHaveBeenCalledWith(env);
expect(loggerWarnMock).not.toHaveBeenCalled();
});

it('still runs the open-stock collection and preserves the outcome error when outcome collection rejects', async () => {
const env = createEnv();
const outcomeError = new Error('outcome collection failed');
Expand Down Expand Up @@ -464,6 +485,7 @@ describe('server background reporting', () => {
expect((config.triggers?.crons ?? []).slice().sort()).toEqual(expected);
expect((config.env?.dev?.triggers?.crons ?? []).slice().sort()).toEqual(expected);
expect(OUTCOME_AGGREGATE_CRON).toBe('*/5 * * * *');
expect([...OUTCOME_AGGREGATE_CRONS]).toEqual([OUTCOME_AGGREGATE_CRON, '*/3 * * * *']);
});
});

Expand Down
6 changes: 5 additions & 1 deletion services/cloud-agent-next/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,10 @@ app.onError(createErrorHandler(logger, { includeMessage: false }));

export const REPORT_RETENTION_CRON = '17 2 * * *';
export const OUTCOME_AGGREGATE_CRON = '*/5 * * * *';
// Cloudflare has been observed to keep firing a previously configured `*/3` trigger after a
// deploy changed it to `*/5`. Accept both so a stale trigger cannot silently disable collection;
// the collector floors `scheduledTime` to 5-minute buckets, so extra ticks only repeat a bucket.
export const OUTCOME_AGGREGATE_CRONS = new Set([OUTCOME_AGGREGATE_CRON, '*/3 * * * *']);

export default {
fetch(request: Request, env: Env, ctx: ExecutionContext): Response | Promise<Response> {
Expand Down Expand Up @@ -1125,7 +1129,7 @@ export default {
await removeExpiredCloudAgentReportData(env);
return;
}
if (controller.cron === OUTCOME_AGGREGATE_CRON) {
if (OUTCOME_AGGREGATE_CRONS.has(controller.cron)) {
try {
await runCloudAgentOutcomeCollection(env, new Date(), controller.scheduledTime);
} finally {
Expand Down