* One cron invocation at `iso`. `chunkMs` is the wall-clock time each legacy-usage * fold chunk is charged; a statement that starts after Cloudflare's 15-minute limit * throws, the way the runtime ends the invocation there.
(iso: string, chunkMs = 0)
| 153 | * throws, the way the runtime ends the invocation there. |
| 154 | */ |
| 155 | async function nightly(iso: string, chunkMs = 0): Promise<{ latestStartMs: number; summary: Row | undefined }> { |
| 156 | at(iso); |
| 157 | const started = Date.now(); |
| 158 | let latestStartMs = 0; |
| 159 | d1.onStatement = (sql) => { |
| 160 | const elapsed = Date.now() - started; |
| 161 | if (elapsed > CRON_LIMIT_MS) { |
| 162 | throw new Error(`statement started ${elapsed / MINUTE} min into the cron run — past the 15-minute limit`); |
| 163 | } |
| 164 | latestStartMs = Math.max(latestStartMs, elapsed); |
| 165 | if (/INSERT INTO usage_daily/.test(sql) && /FROM events/.test(sql)) vi.setSystemTime(Date.now() + chunkMs); |
| 166 | }; |
| 167 | const before = logged.length; |
| 168 | try { |
| 169 | await runNightly(env, Date.parse(iso)); |
| 170 | } finally { |
| 171 | d1.onStatement = null; |
| 172 | } |
| 173 | const summary = logged.slice(before).find((line) => line.msg === 'nightly rollup'); |
| 174 | return { latestStartMs, summary }; |
| 175 | } |
| 176 | |
| 177 | beforeEach(() => { |
| 178 | vi.useFakeTimers({ toFake: ['Date'] }); |
no test coverage detected