Skip to content

feat: Add scheduled triggers to wake container before cron jobs #269

@gabrielstuff

Description

@gabrielstuff

Problem

When SANDBOX_SLEEP_AFTER is configured to reduce costs, the container may be sleeping when OpenClaw's internal cron jobs should fire. This causes important scheduled tasks (like daily briefs) to be skipped entirely.

The cron scheduler runs inside the OpenClaw gateway, which runs inside the container. If the container is asleep, crons don't fire.

Current behavior

  • Container sleeps after inactivity period
  • Cron job scheduled for 07:30 → container is asleep → job skipped
  • Next external request wakes container → scheduler recalculates next run → job scheduled for tomorrow

Expected behavior

  • Container wakes just before critical cron times
  • Jobs fire on schedule

Proposed Solution

Add Cloudflare Workers Scheduled Triggers to wake the container before important cron times.

Changes

1. wrangler.jsonc - Add triggers section:

"triggers": {
  "crons": [
    "25 6 * * *",    // Wake 5 min before typical daily brief
    "55 10 * * 5",   // Wake before Friday weekend brief
    "55 19 * * 0"    // Wake before Sunday week brief
  ]
}

2. src/scheduled-handler.ts - New file:

import { getSandbox } from '@cloudflare/sandbox';
import type { MoltbotEnv } from './types';
import { ensureMoltbotGateway } from './gateway';

export async function handleScheduled(
  event: ScheduledEvent,
  env: MoltbotEnv,
  ctx: ExecutionContext
): Promise<void> {
  console.log('[Scheduled] Wake trigger at', new Date(event.scheduledTime).toISOString());
  
  const sandbox = getSandbox(env.Sandbox, 'moltbot', { keepAlive: true });
  await ensureMoltbotGateway(sandbox, env);
  
  console.log('[Scheduled] Container awakened successfully');
}

3. src/index.ts - Export the handler:

import { handleScheduled } from './scheduled-handler';

export default {
  fetch: app.fetch,
  scheduled: handleScheduled,
};

Benefits

  • No extra cost: Scheduled triggers are free
  • User-configurable: Users can edit wrangler.jsonc to match their cron schedules
  • Documented: README explains how to customize triggers for different timezones

Alternative considered

Documenting that users should set SANDBOX_SLEEP_AFTER=never, but this increases costs (~$30/month vs ~$5-10/month with sleep).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions