pub mod types;
use std::sync::Arc;
use crate::{error::Error, http::HttpClient};
use types::{
CreateCronJobParams, CreateDelayedJobParams, CronJob, DelayedJob, Execution,
UpdateCronJobParams,
};
pub struct Clockwork {
http: Arc<HttpClient>,
}
impl std::fmt::Debug for Clockwork {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Clockwork").finish_non_exhaustive()
}
}
impl Clockwork {
pub fn new(api_key: impl Into<String>) -> Self {
Self::builder()
.api_key(api_key)
.build()
.expect("failed to build Clockwork client")
}
pub fn builder() -> ClockworkBuilder {
ClockworkBuilder::default()
}
pub(crate) fn from_http(http: Arc<HttpClient>) -> Self {
Self { http }
}
pub fn jobs(&self) -> CronJobsClient {
CronJobsClient {
http: Arc::clone(&self.http),
}
}
pub fn delayed(&self) -> DelayedJobsClient {
DelayedJobsClient {
http: Arc::clone(&self.http),
}
}
}
#[derive(Default)]
pub struct ClockworkBuilder {
api_key: Option<String>,
base_url: Option<String>,
timeout_secs: Option<u64>,
}
impl ClockworkBuilder {
pub fn api_key(mut self, key: impl Into<String>) -> Self {
self.api_key = Some(key.into());
self
}
pub fn base_url(mut self, url: impl Into<String>) -> Self {
self.base_url = Some(url.into());
self
}
pub fn timeout_secs(mut self, secs: u64) -> Self {
self.timeout_secs = Some(secs);
self
}
pub fn build(self) -> Result<Clockwork, Error> {
let key = self
.api_key
.ok_or_else(|| Error::Config("clockwork API key is required".into()))?;
let http = HttpClient::new(key, self.base_url, self.timeout_secs)?;
Ok(Clockwork {
http: Arc::new(http),
})
}
}
pub struct CronJobsClient {
http: Arc<HttpClient>,
}
impl CronJobsClient {
pub async fn list(&self) -> Result<Vec<CronJob>, Error> {
self.http.get("/v1/clockwork/jobs").await
}
pub async fn create(&self, params: CreateCronJobParams) -> Result<CronJob, Error> {
self.http.post("/v1/clockwork/jobs", ¶ms, false).await
}
pub async fn update(
&self,
job_id: &str,
params: UpdateCronJobParams,
) -> Result<CronJob, Error> {
self.http
.patch(&format!("/v1/clockwork/jobs/{job_id}"), ¶ms)
.await
}
pub async fn delete(&self, job_id: &str) -> Result<(), Error> {
self.http
.delete(&format!("/v1/clockwork/jobs/{job_id}"))
.await
}
pub async fn executions(&self, job_id: &str) -> Result<Vec<Execution>, Error> {
self.http
.get(&format!("/v1/clockwork/jobs/{job_id}/executions"))
.await
}
}
pub struct DelayedJobsClient {
http: Arc<HttpClient>,
}
impl DelayedJobsClient {
pub async fn list(&self) -> Result<Vec<DelayedJob>, Error> {
self.http.get("/v1/clockwork/delayed").await
}
pub async fn create(&self, params: CreateDelayedJobParams) -> Result<DelayedJob, Error> {
self.http
.post("/v1/clockwork/delayed", ¶ms, false)
.await
}
pub async fn cancel(&self, job_id: &str) -> Result<(), Error> {
self.http
.delete(&format!("/v1/clockwork/delayed/{job_id}"))
.await
}
pub async fn executions(&self, job_id: &str) -> Result<Vec<Execution>, Error> {
self.http
.get(&format!("/v1/clockwork/delayed/{job_id}/executions"))
.await
}
}