omni_orchestrator/schemas/v1/db/queries/worker.rs
1use anyhow::Context;
2use sqlx::{MySql, Pool};
3use tracing;
4
5use libomni::types::db::v1 as types;
6use types::worker::Worker;
7/// Retrieves a paginated list of workers from the database.
8///
9/// This function fetches workers from the database with optional pagination support.
10/// Results are ordered by creation time with the most recently created workers first.
11///
12/// # Arguments
13///
14/// * `pool` - Database connection pool for executing the query
15/// * `limit` - Optional maximum number of workers to return (defaults to 100 if not specified)
16/// * `offset` - Optional number of workers to skip (for pagination)
17///
18/// # Returns
19///
20/// * `Ok(Vec<Worker>)` - Successfully retrieved list of workers
21/// * `Err(anyhow::Error)` - Failed to fetch workers
22///
23// Check your database connection code
24pub async fn list_workers(
25 pool: &sqlx::Pool<sqlx::MySql>,
26 page: Option<u64>,
27 per_page: Option<u64>
28) -> Result<Vec<Worker>, sqlx::Error> {
29 let page = page.unwrap_or(1);
30 let per_page = per_page.unwrap_or(10);
31 let offset = (page - 1) * per_page;
32
33 // Use a simple query first to test
34 let workers = sqlx::query_as::<_, Worker>(
35 "SELECT * FROM workers LIMIT ? OFFSET ?"
36 )
37 .bind(per_page as i64)
38 .bind(offset as i64)
39 .fetch_all(pool)
40 .await?;
41
42 Ok(workers)
43}
44
45/// Retrieves a worker by its ID from the database.
46///
47/// This function fetches a worker from the database using its unique ID.
48///
49/// # Arguments
50///
51/// * `pool` - Database connection pool for executing the query
52/// * `worker_id` - Unique identifier of the worker to fetch
53///
54/// # Returns
55///
56/// * `Ok(Worker)` - Successfully retrieved worker
57/// * `Err(anyhow::Error)` - Failed to fetch worker
58///
59/// # Errors
60///
61/// * `sqlx::Error` - If the query fails or the worker is not found
62pub async fn get_worker_by_id(
63 pool: &sqlx::Pool<sqlx::MySql>,
64 worker_id: i64,
65) -> Result<Worker, sqlx::Error> {
66 let worker = sqlx::query_as::<_, Worker>(
67 "SELECT * FROM workers WHERE id = ?"
68 )
69 .bind(worker_id)
70 .fetch_one(pool)
71 .await?;
72
73 Ok(worker)
74}