Skip to main content

redis_enterprise/
crdb_tasks.rs

1//! Active-Active database task operations
2//!
3//! ## Overview
4//! - Track CRDB async operations
5//! - Query task status
6//! - Manage replication tasks
7
8use crate::client::RestClient;
9use crate::error::Result;
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12use typed_builder::TypedBuilder;
13
14/// CRDB task information
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct CrdbTask {
17    /// Unique task identifier
18    pub task_id: String,
19    /// Globally unique Active-Active database ID (GUID)
20    pub crdb_guid: String,
21    /// Type of task being executed
22    pub task_type: String,
23    /// Current status of the task (queued, running, completed, failed)
24    pub status: String,
25    /// Task completion progress as a percentage (0.0-100.0)
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub progress: Option<f32>,
28    /// Timestamp when the task was started (ISO 8601 format)
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub start_time: Option<String>,
31    /// Timestamp when the task was completed or failed (ISO 8601 format)
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub end_time: Option<String>,
34    /// Error description if the task failed
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub error: Option<String>,
37}
38
39/// CRDB task creation request
40#[derive(Debug, Clone, Serialize, Deserialize, TypedBuilder)]
41pub struct CreateCrdbTaskRequest {
42    /// Globally unique Active-Active database ID (GUID) for the target CRDB
43    #[builder(setter(into))]
44    pub crdb_guid: String,
45    /// Type of task to create (e.g., "flush", "purge", "update_config")
46    #[builder(setter(into))]
47    pub task_type: String,
48    /// Optional parameters specific to the task type
49    #[serde(skip_serializing_if = "Option::is_none")]
50    #[builder(default, setter(strip_option))]
51    pub params: Option<Value>,
52}
53
54/// CRDB tasks handler
55pub struct CrdbTasksHandler {
56    client: RestClient,
57}
58
59impl CrdbTasksHandler {
60    /// Create a new handler bound to the given REST client.
61    pub fn new(client: RestClient) -> Self {
62        CrdbTasksHandler { client }
63    }
64
65    /// List all CRDB tasks
66    pub async fn list(&self) -> Result<Vec<CrdbTask>> {
67        self.client.get("/v1/crdb_tasks").await
68    }
69
70    /// Get specific CRDB task
71    pub async fn get(&self, task_id: &str) -> Result<CrdbTask> {
72        self.client
73            .get(&format!("/v1/crdb_tasks/{}", task_id))
74            .await
75    }
76
77    /// Create a new CRDB task
78    pub async fn create(&self, request: CreateCrdbTaskRequest) -> Result<CrdbTask> {
79        self.client.post("/v1/crdb_tasks", &request).await
80    }
81
82    /// Cancel a CRDB task
83    pub async fn cancel(&self, task_id: &str) -> Result<()> {
84        self.client
85            .post_action(
86                &format!("/v1/crdb_tasks/{}/actions/cancel", task_id),
87                &serde_json::json!({}),
88            )
89            .await
90    }
91
92    /// Cancel a CRDB task with optional force mode
93    pub async fn cancel_with_force(&self, task_id: &str, force: bool) -> Result<()> {
94        let path = if force {
95            format!("/v1/crdb_tasks/{}/actions/cancel?force=true", task_id)
96        } else {
97            format!("/v1/crdb_tasks/{}/actions/cancel", task_id)
98        };
99
100        self.client.post_action(&path, &serde_json::json!({})).await
101    }
102
103    /// Get tasks for a specific CRDB
104    pub async fn list_by_crdb(&self, crdb_guid: &str) -> Result<Vec<CrdbTask>> {
105        self.client
106            .get(&format!("/v1/crdbs/{}/tasks", crdb_guid))
107            .await
108    }
109}