---
title: Cloudflare Workers
description: Run jobs with Cloudflare Queues and recover missed work with a Cron Trigger.
---

`cloudflareQueue` connects a registry to Cloudflare Queues.

1. **Keep jobs in the transaction**

    Migrate the jobs and cron runs tables. Use interactive Postgres transactions, for example through Hyperdrive. Build the registry per invocation.

    ```ts
    import { drizzle } from "drizzle-orm/postgres-js";
    import { drizzleAdapter, outbox } from "logic-sdk/drizzle/pg";
    import { cloudflareQueue } from "logic-sdk/cloudflare";
    import postgres from "postgres";

    function makeRegistry(env: Env, ctx: ExecutionContext) {
      const client = postgres(env.HYPERDRIVE.connectionString, { max: 5, fetch_types: false });
      return createRegistry({
        modules,
        database: { adapter: drizzleAdapter(drizzle({ client })) },
        waitUntil: (p) => ctx.waitUntil(p),
        jobs: {
          adapter: outbox(cloudflareQueue(env.JOBS)),
          scope: { store, load },
        },
      });
    }
    ```

    Bind `HYPERDRIVE`. The outbox stores jobs in the mutation's transaction and signals after commit.

2. **Connect both host functions**

    ```ts
    import type { Job } from "logic-sdk";
    import { handleQueue, handleScheduled } from "logic-sdk/cloudflare";

    export default {
      fetch: app.fetch,
      async queue(batch, env, ctx) {
        await handleQueue(makeRegistry(env, ctx), batch);
      },
      async scheduled(controller, env, ctx) {
        await handleScheduled(makeRegistry(env, ctx), controller);
      },
    } satisfies ExportedHandler<Env, Job>;
    ```

    In `app.fetch`, build the registry per request with `makeRegistry(env, ctx)` too.

    `handleQueue` acknowledges completed messages and signals for missing rows. On a throw, exponential backoff overrides `retry_delay`.

    `handleScheduled` ticks at `controller.scheduledTime`, then sweeps. The outbox sweep takes rows at least 30 seconds past due.

3. **Configure Wrangler and check delivery**

    ```jsonc
    {
      "queues": {
        "producers": [{ "binding": "JOBS", "queue": "jobs" }],
        "consumers": [{ "queue": "jobs", "max_batch_size": 1 }]
      },
      "triggers": { "crons": ["* * * * *"] }
    }
    ```

    - **Batch.** Use one message per invocation for a 15-minute wall time limit per message.
    - **Trigger.** Use a minute trigger for sweeps and crons. Cloudflare schedules use UTC. Use weekday names in separate platform triggers and matching crons.
    - **Bindings.** For named queues, pass a binding map including `default`. Attach a consumer to each queue.

    Run a mutation that queues a job. Check that the consumer runs it or an eligible sweep recovers it.

## Database queue on Workers

Set `databaseQueue()` and keep `handleScheduled`. HTTP `waitUntil` gets 30 seconds after the response. Give longer listeners a positive `delay` so they run in a sweep. Scheduled invocations get 15 minutes.

## Standalone queue

- **Loss.** A crash between commit and send can lose jobs. Use `jobs.failed: failedJobs()` to keep terminal failures.
- **Attempts.** Set `max_retries` to at least your largest `attempts`. A crash can spend an attempt. A dead-letter queue can keep exhausted messages. An outbox counts attempts in the table.
- **Limits.** The adapter sends batches of up to 100 messages and 256 KB. Keep job payloads to IDs. Each message allows 128 KB, including overhead. See [delay and ordering limits](/docs/reference/adapters).

## See also

- [Adapters](/docs/reference/adapters)
- [Crons](/docs/concepts/crons)
- [Jobs](/docs/concepts/jobs)
