Skip to content

Commit

Permalink
node_info -> ping
Browse files Browse the repository at this point in the history
  • Loading branch information
Alw3ys committed Dec 18, 2023
1 parent 516da7a commit 57c5a32
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dosei/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Context;
use clap::Parser;
use dosei_proto::node_info::NodeType;
use dosei_proto::ping::NodeType;
use dotenv::dotenv;
use std::fmt::Formatter;
use std::{env, fmt};
Expand Down
10 changes: 5 additions & 5 deletions dosei/src/server/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::Config;
use dosei_proto::ProtoChannel;
use dosei_proto::{cron_job, node_info};
use dosei_proto::{cron_job, ping};
use log::{error, info};
use once_cell::sync::Lazy;
use prost::Message;
Expand All @@ -17,11 +17,11 @@ static CLUSTER_INFO: Lazy<Arc<Mutex<ClusterInfo>>> = Lazy::new(|| {

#[derive(Debug, Clone)]
pub struct ClusterInfo {
pub replicas: Vec<node_info::NodeInfo>,
pub replicas: Vec<ping::Ping>,
}

impl ClusterInfo {
pub fn add_or_update_replica(&mut self, replica: node_info::NodeInfo) {
pub fn add_or_update_replica(&mut self, replica: ping::Ping) {
match self.replicas.iter_mut().find(|r| r.id == replica.id) {
Some(existing_replica) => {
*existing_replica = replica;
Expand Down Expand Up @@ -58,8 +58,8 @@ pub fn start_node(config: &'static Config) {

// Process data based on identified type
match buf.first() {
Some(&node_info::NodeInfo::PROTO_ID) => {
let received_data = match node_info::NodeInfo::decode(buf_slice) {
Some(&ping::Ping::PROTO_ID) => {
let received_data = match ping::Ping::decode(buf_slice) {
Ok(data) => data,
Err(e) => {
error!("Failed to decode ClusterNode: {}", e);
Expand Down
8 changes: 4 additions & 4 deletions dosei/src/server/cron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bollard::system::EventsOptions;
use bollard::Docker;
use chrono::Utc;
use cron::Schedule;
use dosei_proto::{node_info, ProtoChannel};
use dosei_proto::{ping, ProtoChannel};
use futures_util::stream::StreamExt;
use gcp_auth::AuthenticationManager;
use log::{error, info};
Expand Down Expand Up @@ -50,15 +50,15 @@ pub fn start_job_manager(config: &'static Config, pool: Arc<Pool<Postgres>>) {
}

async fn update_status(config: &'static Config) -> Result<(), Box<dyn Error>> {
let node_info = node_info::NodeInfo {
let node_info = ping::Ping {
id: config.node_info.id.to_string(),
r#enum: i32::from(config.node_info.node_type),
node_type: i32::from(config.node_info.node_type),
address: config.address.to_string(),
version: config::VERSION.to_string(),
};

let mut buf = Vec::with_capacity(node_info.encoded_len() + 1);
buf.push(node_info::NodeInfo::PROTO_ID);
buf.push(ping::Ping::PROTO_ID);

// Serialize the CronJob instance to a buffer
node_info.encode(&mut buf)?;
Expand Down
4 changes: 2 additions & 2 deletions proto/src/cluster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ enum NodeType {
REPLICA = 1;
}

message NodeInfo {
message Ping {
string id = 1;
NodeType enum = 2;
NodeType node_type = 2;
string address = 3;
string version = 4;
}
7 changes: 4 additions & 3 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ pub trait ProtoChannel {
const PROTO_ID: u8;
}

pub mod node_info {
pub mod ping {
include!(concat!(env!("OUT_DIR"), "/dosei.cluster.rs"));
}

impl ProtoChannel for node_info::NodeInfo {
impl ProtoChannel for ping::Ping {
// PING
const PROTO_ID: u8 = 0x00;
// 0x02 reserved for PONG
// Reserve for PONG
//const RESPONSE_PROTO_ID: u8 = 0x01;
}

pub mod cron_job {
Expand Down

0 comments on commit 57c5a32

Please sign in to comment.