Skip to content

Post-migration improvements for Cloudflare Workers #126

@onmax

Description

@onmax

Context

PR #122 migrated from NuxtHub Admin + Cloudflare Pages to self-hosted Cloudflare Workers. Following improvements needed:

Tasks

1. Fix notification parameter semantics

  • File: server/tasks/sync/epochs.ts:46
  • Issue: sendNewEpochNotification(missingEpoch, missingEpochs.length) - second param should be "remaining count" not "total before sync"
  • Fix:
    await sendNewEpochNotification(missingEpoch, missingEpochs.length - 1)

2. Add MAX_EPOCHS_PER_RUN to runtime config

  • Files: nuxt.config.ts, server/tasks/sync/epochs.ts:9
  • Current: const MAX_EPOCHS_PER_RUN = import.meta.dev ? Infinity : 50
  • Fix: Move to runtimeConfig with safeRuntimeConfig validation
  • Benefit: Configurable per environment via env vars

3. Add structured logging to scheduled tasks

  • Files: server/tasks/sync/epochs.ts, server/tasks/sync/snapshot.ts
  • Fix: Add consola.info() at:
    • Task start with config summary
    • Each epoch synced (progress indicator)
    • Task end with timing and summary stats
  • Example:
    consola.info(`[sync:epochs] Starting sync, max epochs: ${MAX_EPOCHS_PER_RUN}`)
    consola.success(`[sync:epochs] Synced ${totalSynced} epochs in ${elapsed}ms`)

4. Standardize environment detection

  • Issue: Mixed use of import.meta.dev and isDevelopment from std-env
  • Files:
    • server/tasks/sync/snapshot.ts:16 (uses import.meta.dev ✅)
    • server/utils/slack.ts:3 (uses isDevelopment from std-env ❌)
  • Fix: Use import.meta.dev consistently, remove std-env import from slack.ts

5. Sanitize stack traces in Slack notifications

  • File: server/utils/slack.ts
  • Issue: Full stack traces expose internal paths, dependency versions
  • Fix: In production, truncate stack to first 3-5 lines or exclude entirely
  • Example:
    stack: import.meta.dev ? error?.stack : error?.stack?.split('\n').slice(0, 3).join('\n')

6. Extract error handling helper (DRY)

  • Files: server/tasks/sync/epochs.ts, server/tasks/sync/snapshot.ts
  • Issue: Repeated pattern in both tasks:
    const error = new Error(rangeError || 'Unable to fetch range')
    await sendSyncFailureNotification('missing-epoch', error)
    return { result: { success: false, error: rangeError } }
  • Fix: Create server/utils/task-error.ts:
    export async function handleTaskError(
      taskName: string, 
      error: unknown, 
      defaultMsg: string
    ) {
      const err = new Error(String(error) || defaultMsg)
      await sendSyncFailureNotification(taskName, err)
      return { result: { success: false, error: String(error) } }
    }
    Usage: return await handleTaskError('missing-epoch', rangeError, 'Unable to fetch range')

Labels

Add: enhancement, refactor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions