use std::collections::BTreeMap;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::client::Bibby;
use crate::error::Result;
use crate::pagination::{Page, PageParams};
const PRODUCT: &str = "cron";
const MANUAL_RUN_TIMEOUT: Duration = Duration::from_secs(60);
#[derive(Debug, Clone, Copy)]
pub struct CronClient<'a> {
client: &'a Bibby,
}
impl<'a> CronClient<'a> {
pub(crate) fn new(client: &'a Bibby) -> Self {
Self { client }
}
pub async fn create_job(&self, request: &CreateJobRequest) -> Result<Job> {
self.client.post_json(PRODUCT, "jobs", request).await
}
pub async fn list_jobs(&self, params: &ListJobsParams) -> Result<Page<Job>> {
self.client.get_json_query(PRODUCT, "jobs", params).await
}
pub async fn get_job(&self, id: i64) -> Result<Job> {
self.client.get_json(PRODUCT, &format!("jobs/{id}")).await
}
pub async fn update_job(&self, id: i64, changes: &UpdateJobRequest) -> Result<Job> {
self.client
.patch_json(PRODUCT, &format!("jobs/{id}"), changes)
.await
}
pub async fn delete_job(&self, id: i64) -> Result<Job> {
self.client
.delete_json(PRODUCT, &format!("jobs/{id}"))
.await
}
pub async fn run_now(&self, id: i64) -> Result<JobRun> {
self.client
.post_empty_with_timeout(PRODUCT, &format!("jobs/{id}/run"), MANUAL_RUN_TIMEOUT)
.await
}
pub async fn list_runs(&self, id: i64, params: &PageParams) -> Result<Page<JobRun>> {
self.client
.get_json_query(PRODUCT, &format!("jobs/{id}/runs"), params)
.await
}
pub async fn preview_schedule(
&self,
schedule: &str,
count: Option<u32>,
) -> Result<SchedulePreview> {
#[derive(Serialize)]
struct Query<'a> {
schedule: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
count: Option<u32>,
}
self.client
.get_json_query(PRODUCT, "schedules/preview", &Query { schedule, count })
.await
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum HttpMethod {
Get,
Post,
Put,
Patch,
Delete,
}
#[derive(Debug, Clone, Serialize)]
pub struct CreateJobRequest {
pub name: String,
pub schedule: String,
pub url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub method: Option<HttpMethod>,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<BTreeMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub body: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub notify: Option<bool>,
}
impl CreateJobRequest {
pub fn new(
name: impl Into<String>,
schedule: impl Into<String>,
url: impl Into<String>,
) -> Self {
Self {
name: name.into(),
schedule: schedule.into(),
url: url.into(),
method: None,
headers: None,
body: None,
enabled: None,
notify: None,
}
}
pub fn method(mut self, method: HttpMethod) -> Self {
self.method = Some(method);
self
}
pub fn headers(mut self, headers: BTreeMap<String, String>) -> Self {
self.headers = Some(headers);
self
}
pub fn body(mut self, body: impl Into<String>) -> Self {
self.body = Some(body.into());
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = Some(enabled);
self
}
pub fn notify(mut self, notify: bool) -> Self {
self.notify = Some(notify);
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateJobRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub schedule: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub method: Option<HttpMethod>,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<BTreeMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub body: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub notify: Option<bool>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListJobsParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub q: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<i64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Job {
pub id: i64,
pub name: String,
pub schedule: String,
pub method: HttpMethod,
pub url: String,
#[serde(default)]
pub headers: BTreeMap<String, String>,
pub body: Option<String>,
pub enabled: bool,
pub notify: bool,
pub next_run_at: Option<String>,
pub last_run_at: Option<String>,
pub last_succeeded: Option<bool>,
pub created_at: String,
pub updated_at: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RunTrigger {
Schedule,
Manual,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JobRun {
pub id: i64,
pub job_id: i64,
pub trigger: RunTrigger,
pub started_at: String,
pub duration_ms: i64,
pub status_code: Option<i32>,
pub succeeded: bool,
pub error: Option<String>,
pub response_body: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulePreview {
pub schedule: String,
pub description: String,
pub next_runs: Vec<String>,
}