Skip to main content

qstash_rs/client/
schedules.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5use super::{request::build_schedule_headers, request::ScheduleRequest, Client};
6use crate::error::Result;
7
8/// A persisted QStash schedule.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Schedule {
12    /// Unique identifier for the schedule.
13    pub schedule_id: String,
14    /// Cron expression.
15    pub cron: String,
16    /// Destination URL or URL Group.
17    pub destination: String,
18    /// Creation timestamp in milliseconds.
19    pub created_at: u64,
20    /// Delivery method.
21    pub method: String,
22    /// Forwarded headers.
23    pub header: Option<HashMap<String, Vec<String>>>,
24    /// UTF-8 body when available.
25    pub body: Option<String>,
26    /// Base64-encoded body for non-UTF-8 payloads.
27    pub body_base64: Option<String>,
28    /// Retry count.
29    pub retries: Option<u32>,
30    /// Delay in seconds.
31    pub delay: Option<u32>,
32    /// Callback URL.
33    pub callback: Option<String>,
34    /// Failure callback URL.
35    pub failure_callback: Option<String>,
36    /// Queue name when the schedule publishes through a queue.
37    pub queue_name: Option<String>,
38    /// Caller IP recorded by QStash.
39    pub caller_ip: Option<String>,
40    /// Whether the schedule is paused.
41    pub is_paused: bool,
42    /// Flow control key used by the created message.
43    pub flow_control_key: Option<String>,
44    /// Flow control rate.
45    pub rate: Option<u32>,
46    /// Flow control period in seconds.
47    pub period: Option<u32>,
48    /// Flow control parallelism.
49    pub parallelism: Option<u32>,
50    /// Retry delay expression.
51    pub retry_delay_expression: Option<String>,
52    /// User-defined label.
53    pub label: Option<String>,
54    /// Last schedule trigger timestamp in milliseconds.
55    pub last_schedule_time: Option<u64>,
56    /// Next schedule trigger timestamp in milliseconds.
57    pub next_schedule_time: Option<u64>,
58    /// States of the most recently triggered messages.
59    pub last_schedule_states: Option<HashMap<String, String>>,
60}
61
62/// Response returned when a schedule is created.
63#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
64#[serde(rename_all = "camelCase")]
65pub struct ScheduleCreateResponse {
66    /// Created schedule identifier.
67    pub schedule_id: String,
68}
69
70/// Schedule operations.
71pub struct SchedulesApi<'a> {
72    pub(crate) client: &'a Client,
73}
74
75impl SchedulesApi<'_> {
76    /// Creates or updates a schedule.
77    pub async fn create(&self, request: ScheduleRequest) -> Result<ScheduleCreateResponse> {
78        self.client
79            .http
80            .send_json(
81                Method::POST,
82                &format!("v2/schedules/{}", request.destination.path_value()),
83                &[],
84                Some(build_schedule_headers(&request)?),
85                request.body,
86            )
87            .await
88    }
89
90    /// Retrieves a schedule by identifier.
91    pub async fn get(&self, schedule_id: &str) -> Result<Schedule> {
92        self.client
93            .http
94            .send_json(
95                Method::GET,
96                &format!("v2/schedules/{schedule_id}"),
97                &[],
98                None,
99                None,
100            )
101            .await
102    }
103
104    /// Lists all schedules.
105    pub async fn list(&self) -> Result<Vec<Schedule>> {
106        self.client
107            .http
108            .send_json(Method::GET, "v2/schedules", &[], None, None)
109            .await
110    }
111
112    /// Deletes a schedule.
113    pub async fn delete(&self, schedule_id: &str) -> Result<()> {
114        self.client
115            .http
116            .send_empty(
117                Method::DELETE,
118                &format!("v2/schedules/{schedule_id}"),
119                &[],
120                None,
121                None,
122            )
123            .await
124    }
125
126    /// Pauses a schedule.
127    pub async fn pause(&self, schedule_id: &str) -> Result<()> {
128        self.client
129            .http
130            .send_empty(
131                Method::POST,
132                &format!("v2/schedules/{schedule_id}/pause"),
133                &[],
134                None,
135                None,
136            )
137            .await
138    }
139
140    /// Resumes a schedule.
141    pub async fn resume(&self, schedule_id: &str) -> Result<()> {
142        self.client
143            .http
144            .send_empty(
145                Method::POST,
146                &format!("v2/schedules/{schedule_id}/resume"),
147                &[],
148                None,
149                None,
150            )
151            .await
152    }
153}