#task-scheduling #server #template

rsketch-common-worker

Background worker and task scheduling utilities for rsketch

2 releases

Uses new Rust 2024

0.0.17 Jan 20, 2026
0.0.16 Jan 20, 2026

#43 in #task-scheduling

Apache-2.0

130KB
2K SLoC

Just my personal rust project template

codecov

Quick set up a rust project.

Features

  • Automated release management with release-plz
  • Comprehensive CI/CD pipeline with GitHub Actions
  • Code coverage reporting with Codecov
  • GitHub Copilot integration for PR reviews

lib.rs:

Worker abstraction for task scheduling and execution.

This crate provides a flexible worker system with:

  • Multiple trigger types: Once, Notify, Interval, Cron, and hybrid triggers
  • Type-safe builder API: Compile-time guarantees for trigger configuration
  • Shared state: Generic state support with Clone constraint
  • Lifecycle hooks: on_start, work, on_shutdown
  • Graceful shutdown: Coordinated cancellation with timeout
  • Pause/Resume/Notify: Runtime control via handle traits

Quick Start

use std::time::Duration;

use rsketch_common_worker::{Handle, Manager, Pausable, Worker, WorkerContext};

struct MyWorker;

#[async_trait::async_trait]
impl Worker for MyWorker {
    async fn work<S: Clone + Send + Sync>(&mut self, ctx: WorkerContext<S>) {
        println!("Worker {} executed", ctx.name());
    }
}

#[tokio::main]
async fn main() {
    let mut manager = Manager::new();

    // Spawn an interval worker (handle contains worker id)
    let handle = manager
        .worker(MyWorker)
        .name("my-worker")
        .interval(Duration::from_secs(5))
        .spawn();

    // Pause/resume control
    handle.pause();
    handle.resume();

    // Access worker id via handle
    let _id = handle.id();

    // Graceful shutdown
    manager.shutdown().await;
}

Architecture

  • Worker: Trait defining work logic with lifecycle hooks
  • Manager: Orchestrates worker lifecycle and shared state
  • WorkerContext: Execution context with state, cancellation, and notify
  • Trigger: Execution schedule (Once, Notify, Interval, Cron, etc.)
  • Handle traits: Handle, Pausable, Notifiable for runtime control

Dependencies

~12–16MB
~205K SLoC