zirv_queue/
lib.rs

1/// The `zirv_queue` crate provides a job-queue system with autoscaling workers,
2/// configurable runtime settings, and database migration support.
3///
4/// This module defines submodules that handle core functionality:
5/// - [`config`]: Manages application and database-related configuration.
6/// - [`models`]: Contains data structures (`Job`, `FailedJob`, etc.) representing database tables.
7/// - [`traits`]: Defines common behavior for jobs (`Queueable` trait) and other shared traits.
8/// - [`utils`]: Utility functions for serialization, deserialization, and more.
9/// - [`worker`]: Handles the worker logic for fetching and processing jobs.
10/// - [`schema`]: Diesel-generated schema definitions linking Rust types to database tables.
11pub mod config;
12pub mod models;
13pub mod traits;
14pub mod utils;
15pub mod worker;
16
17/// Automatically generated by Diesel to map database tables into Rust structs.
18pub mod schema;
19
20// Re-export commonly used items for convenience:
21pub use config::Config;
22
23use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
24use worker::start_autoscaling_worker;
25use zirv_db::DB;
26
27/// An embedded collection of Diesel migrations stored at compile time.
28///
29/// This constant is used during initialization to run any pending migrations
30/// before spinning up the worker threads. The migrations directory is specified
31/// by the `"./migrations"` path at compile time.
32pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
33
34/// Initializes the job queue system by:
35/// 1. Setting up a logger via [`env_logger::init`].
36/// 2. Attempting to acquire a database connection using [`DB::get_conn`].
37/// 3. Running any pending database migrations using [`MIGRATIONS`].
38/// 4. Starting the autoscaling worker system with [`start_autoscaling_worker`].
39///
40/// # Returns
41///
42/// - `Ok(())` if all steps succeed.
43/// - `Err(diesel::result::Error)` if a connection or migration step fails.
44///
45/// # Examples
46///
47/// ```rust,ignore
48/// fn main() {
49///     // Initialize everything
50///     if let Err(e) = zirv_queue::init() {
51///         eprintln!("Failed to initialize queue system: {:?}", e);
52///         std::process::exit(1);
53///     }
54///
55///     // The worker threads are now running in the background.
56///     // ...
57/// }
58/// ```
59pub fn init() -> Result<(), diesel::result::Error> {
60    // Initialize logger
61    env_logger::init();
62
63    // Initialize database connection pool
64    let mut conn = match DB::get_conn() {
65        Ok(conn) => conn,
66        Err(e) => {
67            eprintln!("Error getting DB connection: {:?}", e);
68            return Err(diesel::result::Error::NotFound);
69        }
70    };
71
72    // Run migrations
73    conn.run_pending_migrations(MIGRATIONS).expect("Migrations failed");
74
75    // Initialize the worker system
76    start_autoscaling_worker();
77
78    Ok(())
79}