From eccc5667bc0bcfd930e5d9955400fabe35593cce Mon Sep 17 00:00:00 2001 From: Jason Schrader Date: Fri, 20 Dec 2024 09:59:21 -0700 Subject: [PATCH] fix: remove unused alarms, documented format in #35 --- src/auth/auth-do.ts | 16 +--------------- src/cdn/cdn-do.ts | 16 +--------------- src/context/context-do.ts | 16 +--------------- src/database/database-do.ts | 16 +--------------- src/index.ts | 7 ++++++- src/scheduler/scheduler-do.ts | 16 +--------------- src/tools/tools-do.ts | 16 +--------------- 7 files changed, 12 insertions(+), 91 deletions(-) diff --git a/src/auth/auth-do.ts b/src/auth/auth-do.ts index 525d620..d94cd7d 100644 --- a/src/auth/auth-do.ts +++ b/src/auth/auth-do.ts @@ -26,21 +26,7 @@ export class AuthDO extends DurableObject { this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS; // Set up alarm to run at configured interval - ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - - async alarm(): Promise { - try { - console.log(`AuthDO: alarm activated`); - } catch (error) { - console.error(`AuthDO: alarm execution failed: ${error instanceof Error ? error.message : String(error)}`); - } finally { - // Always schedule next alarm if one isn't set - const currentAlarm = await this.ctx.storage.getAlarm(); - if (currentAlarm === null) { - this.ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - } + // ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); } async fetch(request: Request): Promise { diff --git a/src/cdn/cdn-do.ts b/src/cdn/cdn-do.ts index 55c9b89..18c8598 100644 --- a/src/cdn/cdn-do.ts +++ b/src/cdn/cdn-do.ts @@ -20,21 +20,7 @@ export class CdnDO extends DurableObject { this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS; // Set up alarm to run at configured interval - ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - - async alarm(): Promise { - try { - console.log(`CdnDO: alarm activated`); - } catch (error) { - console.error(`CdnDO: alarm execution failed: ${error instanceof Error ? error.message : String(error)}`); - } finally { - // Always schedule next alarm if one isn't set - const currentAlarm = await this.ctx.storage.getAlarm(); - if (currentAlarm === null) { - this.ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - } + // ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); } async fetch(request: Request): Promise { diff --git a/src/context/context-do.ts b/src/context/context-do.ts index 2687661..2ed0ecd 100644 --- a/src/context/context-do.ts +++ b/src/context/context-do.ts @@ -21,21 +21,7 @@ export class ContextDO extends DurableObject { this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS; // Set up alarm to run at configured interval - ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - - async alarm(): Promise { - try { - console.log(`ContextDO: alarm activated`); - } catch (error) { - console.error(`ContextDO: alarm execution failed: ${error instanceof Error ? error.message : String(error)}`); - } finally { - // Always schedule next alarm if one isn't set - const currentAlarm = await this.ctx.storage.getAlarm(); - if (currentAlarm === null) { - this.ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - } + // ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); } async fetch(request: Request): Promise { diff --git a/src/database/database-do.ts b/src/database/database-do.ts index 518f7c4..c8e4fb1 100644 --- a/src/database/database-do.ts +++ b/src/database/database-do.ts @@ -76,21 +76,7 @@ export class DatabaseDO extends DurableObject { this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS; // Set up alarm to run at configured interval - ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - - async alarm(): Promise { - try { - console.log(`DatabaseDO: alarm activated`); - } catch (error) { - console.error(`DatabaseDO: alarm execution failed: ${error instanceof Error ? error.message : String(error)}`); - } finally { - // Always schedule next alarm if one isn't set - const currentAlarm = await this.ctx.storage.getAlarm(); - if (currentAlarm === null) { - this.ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - } + // ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); } async fetch(request: Request): Promise { diff --git a/src/index.ts b/src/index.ts index 9050ebd..498724f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,7 +46,11 @@ export default { }); } - // For the Durable Object responses, the CORS headers will be added by the DO handlers + // Each service is represented by its own Durable Object + // - services are grouped by folder in the src directory + // - each service has its own reources and logic + // - we pass the request to the DO and it constructs the response + // - response headers and formatting are handled by shared utils if (path.startsWith('/auth')) { const id: DurableObjectId = env.AUTH_DO.idFromName('auth-do'); // create the instance @@ -99,6 +103,7 @@ export default { // Return 404 for any other path return createUnsupportedEndpointResponse(path, config.SUPPORTED_SERVICES); } catch (error) { + // Return 500 for any unhandled error return createApiResponse(`Unknown error: ${error instanceof Error ? error.message : String(error)}`, 500); } }, diff --git a/src/scheduler/scheduler-do.ts b/src/scheduler/scheduler-do.ts index 38d1771..f0b160b 100644 --- a/src/scheduler/scheduler-do.ts +++ b/src/scheduler/scheduler-do.ts @@ -21,21 +21,7 @@ export class SchedulerDO extends DurableObject { this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS; // Set up alarm to run at configured interval - ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - - async alarm(): Promise { - try { - console.log(`SchedulerDO: alarm activated`); - } catch (error) { - console.error(`SchedulerDO: alarm execution failed: ${error instanceof Error ? error.message : String(error)}`); - } finally { - // Always schedule next alarm if one isn't set - const currentAlarm = await this.ctx.storage.getAlarm(); - if (currentAlarm === null) { - this.ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - } + // ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); } async fetch(request: Request): Promise { diff --git a/src/tools/tools-do.ts b/src/tools/tools-do.ts index 4912bf8..d002b10 100644 --- a/src/tools/tools-do.ts +++ b/src/tools/tools-do.ts @@ -21,21 +21,7 @@ export class ToolsDO extends DurableObject { this.ALARM_INTERVAL_MS = config.ALARM_INTERVAL_MS; // Set up alarm to run at configured interval - ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - - async alarm(): Promise { - try { - console.log(`ToolsDO: alarm activated`); - } catch (error) { - console.error(`ToolsDO: alarm execution failed: ${error instanceof Error ? error.message : String(error)}`); - } finally { - // Always schedule next alarm if one isn't set - const currentAlarm = await this.ctx.storage.getAlarm(); - if (currentAlarm === null) { - this.ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); - } - } + // ctx.storage.setAlarm(Date.now() + this.ALARM_INTERVAL_MS); } async fetch(request: Request): Promise {