Skip to main content

enqueue_all

Function enqueue_all 

Source
pub fn enqueue_all<J: Serialize>(
    ctx: &Ctx,
    jobs: &[J],
) -> impl Future<Output = Result<()>> + Send + use<J>
Expand description

Sends every job of jobs to the JOBS queue in as few calls as possible, like Rails’ perform_all_later.

One sendBatch call carries up to 100 messages and 256 KB, so 250 jobs take 3 calls instead of 250: use it whenever a handler or a scheduled task enqueues more than a few jobs (a Worker invocation may only make a limited number of calls to bindings). The jobs may be different variants of the app’s Job enum; they run in any order. Each call is atomic, the whole list is not: when a later call fails, the earlier jobs are queued. An empty list sends nothing.

Free-plan cost: the same as enqueue for each job (3 Queues operations each); only the number of calls shrinks.

§Errors

Every error of enqueue; when one job cannot be serialized or is over 128 KB, nothing is sent.

§Examples

use ocre::{Ctx, Result};
use serde::Serialize;

#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
enum Job {
    SendDigest { user_id: i64 },
}

async fn digests(ctx: &Ctx, user_ids: &[i64]) -> Result<()> {
    let jobs: Vec<Job> = user_ids.iter().map(|&user_id| Job::SendDigest { user_id }).collect();
    ocre::jobs::enqueue_all(ctx, &jobs).await
}