use async_trait::async_trait;
use ironflow_core::error::OperationError;
use ironflow_core::operation::{Operation, OperationContext, TypedOperation};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sqlx::PgPool;
use crate::helpers::{pg_error, to_value};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckOutput {
pub healthy: bool,
}
pub struct HealthCheck {
pool: PgPool,
}
impl HealthCheck {
pub fn new(pool: PgPool) -> Self {
Self { pool }
}
pub async fn run(&self, _ctx: &OperationContext) -> Result<HealthCheckOutput, OperationError> {
sqlx::query("SELECT 1")
.fetch_one(&self.pool)
.await
.map_err(pg_error)?;
Ok(HealthCheckOutput { healthy: true })
}
}
#[async_trait]
impl Operation for HealthCheck {
fn kind(&self) -> &str {
"postgres"
}
async fn execute(&self, ctx: &OperationContext) -> Result<Value, OperationError> {
to_value(&self.run(ctx).await?)
}
}
impl TypedOperation for HealthCheck {
type Output = HealthCheckOutput;
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn kind() {
let pool = PgPool::connect_lazy("postgres://localhost/test").unwrap();
let op = HealthCheck::new(pool);
assert_eq!(op.kind(), "postgres");
}
}