ej_dispatcher_sdk/
lib.rs

1//! Dispatcher SDK for the EJ framework.
2//!
3//! Provides client interfaces for communicating with the EJ dispatcher service.
4//!
5//! # Usage
6//!
7//! ```rust,no_run
8//! use ej_dispatcher_sdk::{EjJob, EjJobType, dispatch_run};
9//! use std::time::Duration;
10//! use std::path::Path;
11//!
12//! # tokio_test::block_on(async {
13//!
14//! // Dispatch a run job to the dispatcher and wait for completion
15//! let result = dispatch_run(
16//!     Path::new("/tmp/ejd.sock"),
17//!     "abc123".to_string(),
18//!     "https://github.com/user/repo.git".to_string(),
19//!     None,
20//!     Duration::from_secs(600),
21//! ).await.unwrap();
22//!# });
23//! ```
24
25use crate::{ejsocket_message::EjSocketClientMessage, prelude::*};
26use std::{collections::HashMap, fmt, path::Path, time::Duration};
27use tokio::{
28    io::{AsyncBufReadExt, AsyncWriteExt, BufReader, Lines},
29    net::UnixStream,
30};
31use tracing::{error, info};
32use uuid::Uuid;
33
34pub use crate::{
35    build::dispatch_build,
36    ejjob::{
37        EjBuildResult, EjDeployableJob, EjJob, EjJobCancelReason, EjJobType, EjJobUpdate,
38        EjRunResult,
39    },
40    fetch_jobs::fetch_jobs,
41    fetch_run_result::fetch_run_result,
42    run::dispatch_run,
43};
44
45pub mod build;
46pub mod ejbuilder;
47pub mod ejclient;
48pub mod ejjob;
49pub mod ejsocket_message;
50pub mod ejws_message;
51pub mod error;
52pub mod fetch_jobs;
53pub mod fetch_run_result;
54pub mod prelude;
55pub mod run;
56mod socket;
57
58/// Dispatch a job to the EJ dispatcher.
59///
60/// Sends a job request to the dispatcher via Unix socket with a maximum duration timeout.
61///
62/// # Arguments
63///
64/// * `stream` - Unix socket connection to the dispatcher
65/// * `job` - Job configuration to dispatch
66/// * `max_duration` - Maximum time to wait for job completion
67/// ```
68async fn dispatch(stream: &mut UnixStream, job: EjJob, max_duration: Duration) -> Result<()> {
69    let message = EjSocketClientMessage::Dispatch {
70        job,
71        timeout: max_duration,
72    };
73
74    let payload = serde_json::to_string(&message)?;
75    stream.write_all(payload.as_bytes()).await;
76    stream.write_all(b"\n").await;
77    stream.flush().await;
78    Ok(())
79}