Joel Stevick

Running it · The daily cron at 04:17 UTC · stage 5 of 10

The scheduled handler

The Worker's scheduled handler runs once a day at 04:17 UTC, on the cron trigger in demo/wrangler.toml. It awaits four jobs in order: the pipeline's health check (the daily roll-up, the drift check and the canary), the idle purge of demo accounts unused for 10 days, the expiry of questions and archived pages older than 25 months, and the deletion of MCP rate-limit counters older than two days. No visitor is waiting for a response, so the handler streams nothing and returns nothing.

Runs in: Demo Worker, the private Worker behind the DEMO service binding, demo/src/. New to Cloudflare? How these fit together
demo/src/app.ts11 / 11 lines on this page explainedlines 1114-1124 of 1124; the rest are on other stage pages
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own

Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.

demo/src/app.ts The demo Worker: lines 1114–1124, the scheduled handler at the end of createApp.

createApp(): scheduled

segment · lines 1114–1124View on GitHub

The scheduled method of the Worker object that createApp returns.

Calls createApp(): fetch Worker router dailyHealth() Daily health expire() Idle purge and expiry forgetHits() Token validation and limits purge() Idle purge and expiry

    async scheduled(_controller: ScheduledController, env: Env): Promise<void> {      // The daily job: yesterday's health first (README.md, "Watching the pipeline"), while every question of it is      // still there; then idle demos' documents, then whatever has passed KEEP_MONTHS, then old MCP rate counters.

The handler and its order Scheduled

WhatThe Workers runtime calls scheduled on each cron trigger and passes a ScheduledController and the bindings. The handler ignores the controller's cron and scheduledTime because there is only one trigger. The comment gives the order of the four jobs.

WhyHealth runs first because it reads the questions that purge and expire delete. With the current values of CATCH_UP_DAYS and IDLE_DAYS, the order is only a safeguard. dailyHealth rolls up days as far back as CATCH_UP_DAYS (9), and purge deletes questions only from demo accounts (visitors' rows in the demos table) whose last use (an upload, a removal, a question or an MCP request that passed the rate limit) is at least IDLE_DAYS (10) old, so no question that health can still roll up belongs to an account the purge picks. The handler takes no ExecutionContext because it awaits every job, so the invocation lasts until they finish.

cron: 17 4 * * * (demo/wrangler.toml)CATCH_UP_DAYS = 9 (health.ts)IDLE_DAYS = 10

      await dailyHealth(env, modelsFor(env), options)

Health first Scheduled

WhatAwaits dailyHealth with models built from env and the same options every request receives.

WhymodelsFor(env) is called inside the handler because env exists only there. dailyHealth catches its own errors, with the roll-up and the canary in separate try blocks, so a failure inside it never stops the jobs after it.

      await purge(env, modelsFor(env))

The idle purge Scheduled

WhatAwaits purge. Every demo account idle for IDLE_DAYS (10) loses its vectors, chunks, files and unconsented questions, and every app connected to it is disconnected. modelsFor(env) builds the models a second time for this call.

WhyBuilding the models twice costs little, because they are small client objects over the bindings. purge catches each account's failure separately, but it does not catch an error from its first SELECT. Such an error ends this invocation before expire and forgetHits run.

IDLE_DAYS = 10

      await expire(env)

The 25-month expiry Scheduled

WhatAwaits expire, which deletes questions and archived page text older than KEEP_MONTHS (25) in one D1 batch.

WhyThe purge deletes nothing from an account still in use and never deletes kept questions or archived_pages, so this age limit is the only thing that removes them. expire deletes questions by age whether they were kept or not, so it also removes the unconsented questions of an account that was never idle for 10 days, as the comment in expire says.

KEEP_MONTHS = 25

      await forgetHits(env)

Forget the MCP counters Scheduled

WhatAwaits forgetHits (mcp.ts), which deletes the mcp_hits rows whose slot sorts before the UTC date two days ago. A slot is a UTC minute ('YYYY-MM-DDTHH:MM') or a UTC day.

Whymcp_hits holds the MCP server's rate-limit counters, one row per key and slot. A counter whose slot has passed limits nothing, and without this delete the table would grow by a row for every minute each caller was active. A text comparison is enough because both slot formats start with the date.

slot < strftime('%Y-%m-%d', 'now', '-2 days')

    },  }}

End of the Worker object Never runs

WhatCloses the scheduled method, the object createApp returns, and createApp.

Whyindex.ts exports that object as the module's default export, and the runtime finds fetch and scheduled on it by name.