pub fn enqueue<J: Serialize>(
ctx: &Ctx,
job: &J,
) -> impl Future<Output = Result<()>> + Send + use<J>Expand description
Sends job to the JOBS queue, to run in the background through consume.
Returns once Cloudflare stored the message; the queue event runs it
within seconds (maxBatchTimeout: 5 in cloudflare.config.ts). The job is
serialized to JSON and wrapped as {"at": <now>, "job": ...}; the whole
message must fit in 128 KB, so pass ids, not records. The returned future
is Send, so axum handlers can await it.
Free-plan cost: each job is one message, 3 of the 10,000 daily Queues operations (a write now, a read and a delete when consumed), plus one read per retry and one write if it ends in the dead-letter queue: about 3,300 jobs a day.
§Errors
Error::Internal (500) when the job does not
serialize to JSON, the message is over the 128 KB limit, the
JOBS: bindings.queue(...) entry is missing from cloudflare.config.ts
(run ocre g job <Name> once), or Queues refuses the message.
§Examples
use axum::extract::State;
use ocre::{Ctx, Result};
use serde::Serialize;
#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
enum Job {
SendWelcome { user_id: i64 },
}
async fn create(State(ctx): State<Ctx>) -> Result<&'static str> {
ocre::jobs::enqueue(&ctx, &Job::SendWelcome { user_id: 1 }).await?;
Ok("created")
}