use async_trait::async_trait;
use chrono::{DateTime, Utc};
use fred::prelude::{Builder, Client, ClientLike as _, Config, LuaInterface as _};
use fred::types::Value;
use crate::error::{KernelError, Result};
use crate::watermark::CronWatermarkStore;
const LAYOUT: i64 = 1;
const MIGRATE_SCRIPT: &str = r"
local stored = tonumber(redis.call('GET', KEYS[1]) or '0')
local mine = tonumber(ARGV[1])
if stored > mine then return stored end
if stored < mine then redis.call('SET', KEYS[1], ARGV[1]) end
return mine
";
const ADVANCE_SCRIPT: &str = r"
local stored = redis.call('HGET', KEYS[1], ARGV[1])
if (not stored) or tonumber(stored) < tonumber(ARGV[2]) then
redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])
return ARGV[2]
end
return stored
";
#[derive(Debug, Clone)]
pub struct RedisCronWatermarkStore {
client: Client,
namespace: String,
}
impl RedisCronWatermarkStore {
pub const DEFAULT_NAMESPACE: &'static str = "adk:kernel";
pub fn from_client(client: Client) -> Self {
Self { client, namespace: Self::DEFAULT_NAMESPACE.to_string() }
}
pub async fn connect(url: &str) -> Result<Self> {
let config =
Config::from_url(url).map_err(|error| store_error("parse the Redis URL", &error))?;
let client = Builder::from_config(config)
.build()
.map_err(|error| store_error("build the Redis client", &error))?;
client.init().await.map_err(|error| store_error("connect to Redis", &error))?;
Ok(Self::from_client(client))
}
#[must_use]
pub fn with_namespace(mut self, namespace: impl Into<String>) -> Self {
self.namespace = namespace.into();
self
}
pub fn client(&self) -> &Client {
&self.client
}
pub fn namespace(&self) -> &str {
&self.namespace
}
pub async fn migrate(&self) -> Result<()> {
let stored: i64 = self
.client
.eval(
MIGRATE_SCRIPT,
vec![self.key("cron-watermarks:layout")],
vec![LAYOUT.to_string()],
)
.await
.map_err(|error| store_error("record the layout version", &error))?;
if stored > LAYOUT {
return Err(KernelError::Store {
backend: "redis",
reason: format!(
"the cron watermark keys under {} have layout v{stored}, but this release \
knows up to v{LAYOUT}; upgrade ADK-Rust",
self.namespace
),
});
}
Ok(())
}
fn key(&self, suffix: &str) -> String {
let namespace = self.namespace.replace('{', "%7B").replace('}', "%7D");
format!("{{{namespace}}}:{suffix}")
}
}
fn store_error(action: &str, error: &dyn std::fmt::Display) -> KernelError {
KernelError::Store { backend: "redis", reason: format!("could not {action}: {error}") }
}
fn instant(value: Value) -> Result<DateTime<Utc>> {
let text = value
.into_string()
.ok_or_else(|| store_error("read a cron watermark", &"the cursor is not text"))?;
text.parse::<i64>()
.ok()
.and_then(DateTime::from_timestamp_micros)
.ok_or_else(|| store_error("read a cron watermark", &format!("`{text}` is no cursor")))
}
#[async_trait]
impl CronWatermarkStore for RedisCronWatermarkStore {
async fn read(&self, source: &str) -> Result<Option<DateTime<Utc>>> {
use fred::prelude::HashesInterface as _;
let value: Value = self
.client
.hget(self.key("cron-watermarks"), source)
.await
.map_err(|error| store_error("read a cron watermark", &error))?;
if value.is_null() { Ok(None) } else { instant(value).map(Some) }
}
async fn advance(&self, source: &str, cursor: DateTime<Utc>) -> Result<DateTime<Utc>> {
let value: Value = self
.client
.eval(
ADVANCE_SCRIPT,
vec![self.key("cron-watermarks")],
vec![source.to_string(), cursor.timestamp_micros().to_string()],
)
.await
.map_err(|error| store_error("advance a cron watermark", &error))?;
instant(value)
}
async fn delete(&self, source: &str) -> Result<bool> {
use fred::prelude::HashesInterface as _;
let removed: i64 = self
.client
.hdel(self.key("cron-watermarks"), source)
.await
.map_err(|error| store_error("delete a cron watermark", &error))?;
Ok(removed > 0)
}
}