ciallo-queue

    Cloud-edge isomorphic JobQueue and Cron Scheduler (Memory, Redis, ORM Transactional Outbox, and Cloudflare Queues/Cron Triggers) for unmbt/ciallo

    queue
    outbox
    cron
    scheduler
    cloudflare-queues
    ciallo
    Download zip
    Author
    Version
    0.1.1
    License
    MIT
    Last updated
    13 hours ago
    Downloads
    7

    #unmbt/ciallo-queue

    Cloud-edge isomorphic JobQueue and Scheduler for unmbt/ciallo:
    • MemoryJobQueue (delayed jobs, retries, idempotency, DLQ)
    • RedisJobQueue (backed by unmbt/ciallo-redis)
    • OutboxQueue (Transactional Outbox over unmbt/ciallo-orm DbTx — zero messages leaked on rollback)
    • Scheduler (CronExpr) + Cloudflare Queues & Cron Triggers bridges

    JobQueue

    pub(open) trait JobQueue {
    async fn enqueue(Self, queue~ : String, payload~ :
    Json
    , options? : EnqueueOptions) -> String raise QueueError
    async fn poll(Self, queue~ : String) -> Job? raise QueueError
    async fn ack(Self, job_id~ : String) -> Unit raise QueueError
    async fn nack(Self, job_id~ : String, delay_ms? : Int64) -> Unit raise QueueError
    }

    QueueError

    pub(all) suberror QueueError {
    InvalidCron(String)
    BackendFailure(String)
    JobNotFound(String)
    } derive(Eq,
    Debug
    )

    CfBatchOutcome

    pub(all) struct CfBatchOutcome {
    acked : Array[String]
    retried : Array[String]
    } derive(Eq,
    Debug
    )

    CfCronEvent

    pub(all) struct CfCronEvent {
    cron : String
    scheduled_time_ms : Int64
    } derive(Eq,
    Debug
    )

    Cloudflare Workers Cron Trigger event envelope.

    CfQueueBatch

    pub(all) struct CfQueueBatch {
    queue : String
    messages : Array[CfQueueMessage]
    } derive(Eq,
    Debug
    )

    CfQueueMessage

    pub(all) struct CfQueueMessage {
    id : String
    timestamp_ms : Int64
    body :
    Json

    attempts : Int
    } derive(Eq,
    Debug
    )

    Cloudflare Workers Queue message envelope for zero-glue queue(batch, env, ctx) handlers.

    CronExpr

    pub struct CronExpr {
    raw : String
    minute : String
    hour : String
    day_of_month : String
    month : String
    day_of_week : String
    } derive(Eq,
    Debug
    )

    CronExpr::matches

    fn CronExpr::matches(self : CronExpr, t : CronTime) -> Bool

    CronExpr::parse

    fn CronExpr::parse(expr : StringView) -> CronExpr raise QueueError

    CronTime

    pub(all) struct CronTime {
    minute : Int
    hour : Int
    day_of_month : Int
    month : Int
    day_of_week : Int
    } derive(Eq,
    Debug
    )

    EnqueueOptions

    pub(all) struct EnqueueOptions {
    delay_ms : Int64
    max_attempts : Int
    idempotency_key : String?
    } derive(Eq,
    Debug
    )

    EnqueueOptions::EnqueueOptions

    fn EnqueueOptions::EnqueueOptions(delay_ms? : Int64, max_attempts? : Int, idempotency_key? : String) -> EnqueueOptions

    Job

    pub(all) struct Job {
    id : String
    queue : String
    payload :
    Json

    attempts : Int
    max_attempts : Int
    available_at_ms : Int64
    idempotency_key : String?
    } derive(Eq,
    Debug
    )

    Job::from_json_value

    fn Job::from_json_value(json :
    Json
    ) -> Job raise QueueError

    Job::to_json

    fn Job::to_json(self : Job) ->
    Json

    JobStatus

    pub(all) enum JobStatus {
    Pending
    Processing
    Completed
    DeadLettered
    } derive(Eq,
    Debug
    )

    MemoryJobQueue

    pub struct MemoryJobQueue {
    clock : &
    Clock

    next_id : Int64
    pending : Array[Job]
    in_flight : Map[String, Job]
    dlq : Array[Job]
    seen_keys : Map[String, String]
    }

    In-memory cooperative JobQueue with delayed delivery, retry backoff, DLQ, and idempotency dedup.

    MemoryJobQueue::MemoryJobQueue

    MemoryJobQueue::ack

    async fn MemoryJobQueue::ack(self : MemoryJobQueue, job_id~ : String) -> Unit raise QueueError

    MemoryJobQueue::dead_letters

    fn MemoryJobQueue::dead_letters(self : MemoryJobQueue) -> Array[Job]

    MemoryJobQueue::enqueue

    async fn MemoryJobQueue::enqueue(self : MemoryJobQueue, queue~ : String, payload~ :
    Json
    , options? : EnqueueOptions) -> String raise QueueError

    MemoryJobQueue::nack

    async fn MemoryJobQueue::nack(self : MemoryJobQueue, job_id~ : String, delay_ms? : Int64) -> Unit raise QueueError

    MemoryJobQueue::pending_count

    fn MemoryJobQueue::pending_count(self : MemoryJobQueue) -> Int

    MemoryJobQueue::poll

    async fn MemoryJobQueue::poll(self : MemoryJobQueue, queue~ : String) -> Job? raise QueueError

    OutboxQueue

    pub struct OutboxQueue {
    clock : &
    Clock

    next_id : Int64
    }

    Transactional Outbox writer & relay over @orm_driver.SqlExecutor (Db / DbTx). Enqueuing inside a Db::transaction guarantees:
    • If the business transaction rolls back, the message is NEVER dispatched.
    • Once committed, poll_and_dispatch reliably delivers it and marks DISPATCHED.

    OutboxQueue::OutboxQueue

    OutboxQueue::enqueue_in_tx

    #callsite(autofill(loc))
    async fn[E :
    SqlExecutor
    ] OutboxQueue::enqueue_in_tx(self : OutboxQueue, exec : E, queue~ : String, payload~ :
    Json
    , loc~ : SourceLoc) -> String raise QueueError

    OutboxQueue::poll_and_dispatch

    #callsite(autofill(loc))
    async fn OutboxQueue::poll_and_dispatch(_self : OutboxQueue, db :
    Db
    , dispatch : async (OutboxRecord) -> Unit, limit? : Int, loc~ : SourceLoc) -> Int raise QueueError

    Polls committed PENDING rows from _ciallo_outbox, invokes dispatch, and marks each successfully delivered row as DISPATCHED.

    OutboxRecord

    pub(all) struct OutboxRecord {
    id : String
    queue : String
    payload :
    Json

    status : String
    created_at_ms : Int64
    } derive(Eq,
    Debug
    )

    RedisJobQueue

    pub struct RedisJobQueue {
    client :
    RedisClient

    clock : &
    Clock

    key_prefix : String
    next_id : Int64
    in_flight : Map[String, Job]
    }

    Redis-backed JobQueue using @redis_client.RedisClient (RPUSH / LPOP).

    RedisJobQueue::RedisJobQueue

    RedisJobQueue::ack

    async fn RedisJobQueue::ack(self : RedisJobQueue, job_id~ : String) -> Unit raise QueueError

    RedisJobQueue::enqueue

    async fn RedisJobQueue::enqueue(self : RedisJobQueue, queue~ : String, payload~ :
    Json
    , options? : EnqueueOptions) -> String raise QueueError

    RedisJobQueue::nack

    async fn RedisJobQueue::nack(self : RedisJobQueue, job_id~ : String, delay_ms? : Int64) -> Unit raise QueueError

    RedisJobQueue::poll

    async fn RedisJobQueue::poll(self : RedisJobQueue, queue~ : String) -> Job? raise QueueError

    ScheduledEntry

    type ScheduledEntry

    Scheduler

    pub struct Scheduler {
    tasks : Array[ScheduledEntry]
    }

    Portable Cron Scheduler usable on Native loops or Cloudflare Cron Triggers.

    Scheduler::Scheduler

    fn Scheduler::Scheduler() -> Scheduler

    Scheduler::schedule

    fn Scheduler::schedule(self : Scheduler, name~ : String, cron~ : StringView, run : async () -> Unit) -> Scheduler raise QueueError

    Scheduler::tick

    async fn Scheduler::tick(self : Scheduler, now : CronTime) -> Array[String]

    dispatch_cf_cron

    async fn dispatch_cf_cron(scheduler : Scheduler, event : CfCronEvent) -> Array[String]

    Dispatches a Cloudflare Workers Cron Trigger event to matching Scheduler tasks.

    dispatch_cf_queue_batch

    async fn dispatch_cf_queue_batch(batch : CfQueueBatch, handler : async (CfQueueMessage) -> Unit) -> CfBatchOutcome

    Dispatches a Cloudflare Queues batch through a typed async handler, recording per-message ack vs retry outcomes.

    outbox_table_ddl

    fn outbox_table_ddl(dialect :
    DialectKind
    ) -> String

    DDL for the _ciallo_outbox transactional outbox table (PostgreSQL or SQLite/D1).