@@ -5,7 +5,10 @@ import {
55} from './server/identity/bindings' ;
66import { handleApiRequest , runIdentityCleanup , type ApiDependencies } from './server/http/api' ;
77import { dashboardAssetResponse } from './server/dashboard/response' ;
8- import { runScheduledBackups } from './server/backup/r2-backup' ;
8+ import {
9+ reconcileStaleBackupRuns ,
10+ runScheduledBackups ,
11+ } from './server/backup/r2-backup' ;
912import {
1013 bumpPublicCacheEpoch ,
1114 mutationInvalidatesPublicCache ,
@@ -25,6 +28,32 @@ interface ExecutionContextLike {
2528 waitUntil ( promise : Promise < unknown > ) : void ;
2629}
2730
31+ interface ScheduledControllerLike {
32+ cron ?: string ;
33+ scheduledTime ?: number ;
34+ }
35+
36+ interface ScheduledDependencies {
37+ runIdentityCleanup ( env : OrbitBindings , now : number ) : Promise < unknown > ;
38+ runScheduledBackups ( env : OrbitBindings , now : number ) : Promise < unknown > ;
39+ cleanupMedia ( env : OrbitBindings , now : number ) : Promise < unknown > ;
40+ reconcileStaleBackupRuns ( env : OrbitBindings , now : number ) : Promise < unknown > ;
41+ }
42+
43+ export const BACKUP_CRON = '17 3 * * *' ;
44+ export const BACKUP_RECONCILIATION_CRON = '0 4 * * *' ;
45+
46+ const scheduledDependencies : ScheduledDependencies = {
47+ runIdentityCleanup,
48+ runScheduledBackups,
49+ cleanupMedia : async ( env , now ) => await cleanupMedia (
50+ env ,
51+ new D1MediaRepository ( env . DB ) ,
52+ now ,
53+ ) ,
54+ reconcileStaleBackupRuns,
55+ } ;
56+
2857interface WorkerDependencies extends Omit < ApiDependencies , 'requestId' > {
2958 publicRepository ?: PublicRepository ;
3059 agentRepository ?: AgentRepository ;
@@ -140,17 +169,46 @@ export async function handleWorkerRequest(
140169 return protectFromIndexing ( response , env ) ;
141170}
142171
172+ export async function runScheduledMaintenance (
173+ controller : ScheduledControllerLike ,
174+ env : OrbitBindings ,
175+ dependencies : ScheduledDependencies = scheduledDependencies ,
176+ ) : Promise < void > {
177+ const now = controller . scheduledTime ?? Date . now ( ) ;
178+ if ( controller . cron === BACKUP_RECONCILIATION_CRON ) {
179+ await dependencies . reconcileStaleBackupRuns ( env , now ) ;
180+ return ;
181+ }
182+
183+ const tasks = [
184+ [ 'identity_cleanup' , ( ) => dependencies . runIdentityCleanup ( env , now ) ] ,
185+ [ 'backup' , ( ) => dependencies . runScheduledBackups ( env , now ) ] ,
186+ [ 'media_cleanup' , ( ) => dependencies . cleanupMedia ( env , now ) ] ,
187+ ] as const ;
188+ const results = await Promise . allSettled ( tasks . map ( ( [ , run ] ) => Promise . resolve ( ) . then ( run ) ) ) ;
189+ const failedTasks : string [ ] = [ ] ;
190+ results . forEach ( ( result , index ) => {
191+ if ( result . status === 'fulfilled' ) return ;
192+ const task = tasks [ index ] ?. [ 0 ] ?? 'unknown' ;
193+ failedTasks . push ( task ) ;
194+ console . error ( JSON . stringify ( {
195+ event : 'scheduled.task' ,
196+ task,
197+ status : 'failed' ,
198+ } ) ) ;
199+ } ) ;
200+ if ( failedTasks . length > 0 ) {
201+ throw new Error ( `scheduled_maintenance_failed:${ failedTasks . join ( ',' ) } ` ) ;
202+ }
203+ }
204+
143205export default {
144206 async fetch ( request : Request , env : OrbitBindings ) : Promise < Response > {
145207 return await handleWorkerRequest ( request , env ) ;
146208 } ,
147209
148- scheduled ( _controller : unknown , env : OrbitBindings , ctx : ExecutionContextLike ) : void {
210+ scheduled ( controller : ScheduledControllerLike , env : OrbitBindings , ctx : ExecutionContextLike ) : void {
149211 assertDeploymentBindings ( env ) ;
150- ctx . waitUntil ( Promise . all ( [
151- runIdentityCleanup ( env ) ,
152- runScheduledBackups ( env ) ,
153- cleanupMedia ( env , new D1MediaRepository ( env . DB ) ) ,
154- ] ) ) ;
212+ ctx . waitUntil ( runScheduledMaintenance ( controller , env ) ) ;
155213 } ,
156214} ;
0 commit comments