92 lines
2.3 KiB
Rust
92 lines
2.3 KiB
Rust
use std::{sync::Arc, time::Duration};
|
|
|
|
use crate::{
|
|
agent::AgentRole,
|
|
store::{ChaosKvStore, InMemoryKvStore, NatsKvStore},
|
|
};
|
|
|
|
// mod agent_loop;
|
|
mod agent;
|
|
pub mod store;
|
|
mod workflow;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
env_logger::init();
|
|
|
|
let heartbeat_interval = Duration::from_millis(2000);
|
|
let failover_timeout = Duration::from_secs(10);
|
|
|
|
// let (health_kv, cluster_kv) = get_chaos_store(&heartbeat_interval, &failover_timeout);
|
|
|
|
let nats_store = get_local_nats_store().await;
|
|
let health_kv = nats_store.clone();
|
|
let cluster_kv = nats_store.clone();
|
|
|
|
let _ = tokio::join!(
|
|
agent::launch_agent(
|
|
AgentRole::Primary,
|
|
health_kv.clone(),
|
|
cluster_kv.clone(),
|
|
heartbeat_interval,
|
|
failover_timeout
|
|
),
|
|
agent::launch_agent(
|
|
AgentRole::Replica,
|
|
health_kv,
|
|
cluster_kv,
|
|
heartbeat_interval,
|
|
failover_timeout
|
|
),
|
|
);
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
fn get_chaos_store(
|
|
heartbeat_interval: &Duration,
|
|
failover_timeout: &Duration,
|
|
) -> (
|
|
Arc<ChaosKvStore<InMemoryKvStore>>,
|
|
Arc<ChaosKvStore<InMemoryKvStore>>,
|
|
) {
|
|
let health_kv = Arc::new(ChaosKvStore::new(
|
|
InMemoryKvStore::new(),
|
|
10,
|
|
10,
|
|
heartbeat_interval.as_millis().try_into().unwrap(),
|
|
));
|
|
let cluster_kv = Arc::new(ChaosKvStore::new(
|
|
InMemoryKvStore::new(),
|
|
5,
|
|
5,
|
|
failover_timeout.as_millis().try_into().unwrap(),
|
|
));
|
|
|
|
(health_kv, cluster_kv)
|
|
}
|
|
|
|
async fn get_local_nats_store() -> Arc<NatsKvStore> {
|
|
let client = async_nats::ConnectOptions::new()
|
|
// .require_tls(true)
|
|
.user_and_password("admin".into(), "admin2".into())
|
|
.ping_interval(std::time::Duration::from_secs(10))
|
|
.connect("localhost")
|
|
.await
|
|
.expect("Connection to nats failed");
|
|
|
|
// let client = async_nats::connect("localhost").await.unwrap();
|
|
let jetstream = async_nats::jetstream::new(client);
|
|
let kv = jetstream
|
|
.create_key_value(async_nats::jetstream::kv::Config {
|
|
bucket: "kv".to_string(),
|
|
history: 10,
|
|
..Default::default()
|
|
})
|
|
.await
|
|
.unwrap();
|
|
let status = kv.status().await.unwrap();
|
|
println!("status: {:?}", status);
|
|
|
|
Arc::new(NatsKvStore::new(kv))
|
|
}
|