Skip to main content

tasker_pgmq/
types.rs

1//! # Types for tasker-pgmq client
2//!
3//! Contains generic types and shared structures used by the tasker-pgmq client.
4//! Tasker-specific types like `PgmqStepMessage` are defined in tasker-shared.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10/// Queue metrics for monitoring
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
12pub struct QueueMetrics {
13    /// Name of the queue
14    pub queue_name: String,
15    /// Current message count in queue
16    pub message_count: i64,
17    /// Number of active consumers (if available)
18    pub consumer_count: Option<i64>,
19    /// Age of oldest message in seconds (if any)
20    pub oldest_message_age_seconds: Option<i64>,
21}
22
23/// Client status information
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct ClientStatus {
26    /// Type of client (e.g., "tasker-pgmq")
27    pub client_type: String,
28    /// Whether client is connected
29    pub connected: bool,
30    /// Connection information
31    pub connection_info: HashMap<String, serde_json::Value>,
32    /// Last activity timestamp
33    pub last_activity: Option<DateTime<Utc>>,
34}
35
36/// Error type for messaging operations
37#[derive(Debug, thiserror::Error)]
38pub enum MessagingError {
39    #[error("Database error: {0}")]
40    Database(#[from] sqlx::Error),
41
42    #[error("Serialization error: {0}")]
43    Serialization(#[from] serde_json::Error),
44
45    #[error("PGMQ error: {0}")]
46    Pgmq(String),
47
48    #[error("Configuration error: {0}")]
49    Configuration(String),
50
51    #[error("Generic error: {0}")]
52    Generic(String),
53}
54
55impl From<String> for MessagingError {
56    fn from(msg: String) -> Self {
57        Self::Generic(msg)
58    }
59}
60
61impl From<&str> for MessagingError {
62    fn from(msg: &str) -> Self {
63        Self::Generic(msg.to_string())
64    }
65}
66
67impl From<anyhow::Error> for MessagingError {
68    fn from(err: anyhow::Error) -> Self {
69        Self::Generic(err.to_string())
70    }
71}
72
73impl From<Box<dyn std::error::Error + Send + Sync>> for MessagingError {
74    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
75        Self::Generic(err.to_string())
76    }
77}