feat: with agent db

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-27 19:42:33 +02:00
parent 75d99c2461
commit 43ed89d0d8
10 changed files with 286 additions and 68 deletions

View File

@@ -1,17 +1,21 @@
use std::collections::HashMap;
use std::sync::Arc;
use axum::async_trait;
use churn_domain::ServerEnrollReq;
use tokio::sync::Mutex;
use churn_capnp::CapnpPackExt;
use churn_domain::{Agent, ServerEnrollReq};
use crate::Agent;
use crate::db::Db;
#[derive(Clone)]
pub struct AgentService(Arc<dyn AgentServiceTrait + Send + Sync + 'static>);
impl AgentService {
pub fn new(db: Db) -> Self {
Self(Arc::new(DefaultAgentService::new(db)))
}
}
impl std::ops::Deref for AgentService {
type Target = Arc<dyn AgentServiceTrait + Send + Sync + 'static>;
@@ -28,7 +32,13 @@ impl Default for AgentService {
#[derive(Default)]
struct DefaultAgentService {
agents: Arc<Mutex<HashMap<String, Agent>>>,
agents: Db,
}
impl DefaultAgentService {
pub fn new(db: Db) -> Self {
Self { agents: db }
}
}
#[async_trait]
@@ -41,24 +51,17 @@ impl AgentServiceTrait for DefaultAgentService {
async fn enroll(&self, req: ServerEnrollReq) -> anyhow::Result<String> {
let agent_name = req.agent_name;
let mut agents = self.agents.lock().await;
self.agents
.insert(
"agents",
&agent_name,
&Agent {
name: agent_name.clone(),
}
.serialize_capnp(),
)
.await?;
match agents.insert(
agent_name.clone(),
Agent {
name: agent_name.clone(),
},
) {
Some(_) => {
tracing::debug!("agents store already contained agent, replaced existing");
Ok(agent_name)
}
None => {
tracing::debug!("agents store didn't contain agent, inserted");
Ok(agent_name)
}
}
Ok(agent_name)
}
}

View File

@@ -1,12 +1,12 @@
use core::slice::SlicePattern;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use async_trait::async_trait;
use churn_domain::ServerEnrollReq;
use tokio::sync::Mutex;
#[derive(Clone)]
pub struct Db(Arc<dyn DbTrait + Send + Sync + 'static>);

View File

@@ -1,13 +1,13 @@
use std::collections::HashMap;
use std::sync::Arc;
use axum::async_trait;
use churn_domain::{LogEvent, ServerEnrollReq};
use churn_domain::{LogEvent};
use itertools::Itertools;
use serde::{ser::SerializeStruct, Deserialize, Serialize};
use tokio::sync::{Mutex, RwLock};
use churn_capnp::CapnpPackExt;
@@ -68,14 +68,13 @@ impl EventServiceTrait for DefaultEventService {
let events = events
.iter()
.map(|e| match LogEvent::deserialize_capnp(e) {
.flat_map(|e| match LogEvent::deserialize_capnp(e) {
Ok(o) => Ok(o),
Err(e) => {
tracing::error!("failed to deserialize capnp: {e}");
Err(e)
}
})
.flatten()
.sorted_by_key(|i| i.timestamp)
.skip_while(|item| item.id != cursor)
.skip(1)
@@ -88,8 +87,7 @@ impl EventServiceTrait for DefaultEventService {
let events = events
.iter()
.map(|e| LogEvent::deserialize_capnp(e))
.flatten()
.flat_map(LogEvent::deserialize_capnp)
.sorted_by_key(|i| i.timestamp)
.collect();

View File

@@ -15,11 +15,11 @@ use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use churn_domain::{LeaseResp, LogEvent, ServerEnrollReq, ServerMonitorResp};
use churn_domain::{Agent, LeaseResp, LogEvent, ServerEnrollReq, ServerMonitorResp};
use clap::{Args, Parser, Subcommand, ValueEnum};
use event::EventService;
use lease::LeaseService;
use serde::{Deserialize, Serialize};
use serde::{Deserialize};
use serde_json::json;
use crate::db::Db;
@@ -56,11 +56,6 @@ enum Commands {
},
}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Agent {
pub name: String,
}
#[derive(Clone)]
struct AppState {
agent: AgentService,
@@ -94,9 +89,9 @@ async fn main() -> anyhow::Result<()> {
.route("/lease", post(agent_lease)),
)
.with_state(AppState {
agent: AgentService::default(),
agent: AgentService::new(db.clone()),
leases: LeaseService::default(),
events: EventService::new(db),
events: EventService::new(db.clone()),
});
tracing::info!("churn server listening on {}", host);
@@ -221,7 +216,7 @@ async fn logs(
if events.is_empty() {
return Ok(Json(ServerMonitorResp {
cursor: cursor.cursor.clone(),
cursor: cursor.cursor,
logs: Vec::new(),
}));
}