Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
syncstat: send updates to Slack
Browse files Browse the repository at this point in the history
  • Loading branch information
nrdxp committed Jun 8, 2021
1 parent 0887de5 commit 70eef4a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
1 change: 1 addition & 0 deletions jobs/tasks/sync_stat.cue
Expand Up @@ -36,6 +36,7 @@ import (
env: true
data: """
RPC_NODE="http://{{ env "NOMAD_ADDR_rpc" }}"
SLACK_URL="{{with secret "kv/nomad-cluster/mainnet/slack"}}{{.Data.data.url}}{{end}}"
"""
}
}
44 changes: 38 additions & 6 deletions pkgs/syncstat/src/main.rs
@@ -1,11 +1,11 @@
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use percentage::Percentage;
use restson::RestClient;
use stat::{RPCData, RPCRender, RPCResult};
use stat::{RPCData, RPCRender, RPCResult, SlackSend};
use std::env;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use std::time::{Duration, Instant};

mod stat;

Expand All @@ -15,6 +15,8 @@ extern crate log;
fn main() -> Result<()> {
pretty_env_logger::init();

let start_time = Instant::now();

let (tx, rx) = mpsc::channel();

let hours: u64 = match env::args().nth(1) {
Expand All @@ -25,6 +27,7 @@ fn main() -> Result<()> {
})?,
None => 12,
};

let timer = thread::spawn(move || {
stat::timeout(hours);
tx.send(()).unwrap();
Expand All @@ -35,6 +38,11 @@ fn main() -> Result<()> {

let mut client = RestClient::new(&mantis_rpc_addr)?;

let slack_url = env::var("SLACK_URL")?;
debug!("RPC_NODE is {}", mantis_rpc_addr);

let mut slack_client = RestClient::new(&slack_url)?;

let data: RPCData = RPCData {
jsonrpc: String::from("2.0"),
method: String::from("eth_syncing"),
Expand All @@ -57,6 +65,8 @@ fn main() -> Result<()> {
RPCResult::Failure(_) => (0, 0),
};

info!("{}", stat::format_time(start_time.elapsed().as_secs()));

let delta = ratio.apply_to(highest_block);

if (highest_block, current_block) == (0, 0)
Expand All @@ -66,14 +76,36 @@ fn main() -> Result<()> {
if rx.try_recv().is_err() {
continue;
} else {
break;
let message = format!(
"Timed out after {} hours, before sync completed.",
hours
);

let data: SlackSend = SlackSend {
text: format!("Mainnet node: {}", message).to_owned(),
};

slack_client.post((), &data)?;

let err: Result<()> = Err(anyhow!(message));
return err;
};
}

break;
}

info!("sync is complete");
let message = format!(
"Sync completed.\n{}",
stat::format_time(start_time.elapsed().as_secs())
);

let data: SlackSend = SlackSend {
text: format!("Mainnet node: {}", message).to_owned(),
};

slack_client.post((), &data)?;

info!("{}", message);

timer.join().ok();

Expand Down
20 changes: 20 additions & 0 deletions pkgs/syncstat/src/stat.rs
Expand Up @@ -30,6 +30,11 @@ pub struct ResultSuccess {
pub pulled_states: u64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SlackSend {
pub text: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RPCResult {
Expand Down Expand Up @@ -57,7 +62,22 @@ impl RestPath<()> for RPCData {
}
}

impl RestPath<()> for SlackSend {
fn get_path(_: ()) -> Result<String, restson::Error> {
Ok("".to_string())
}
}

pub fn timeout(hours: u64) {
info!("Mantis node will run for {} hours.", hours);
thread::sleep(Duration::new(hours * 60 * 60, 0));
}

pub fn format_time(secs: u64) -> String {
format!(
"time elapsed: {}:{}:{}",
secs / 60 / 60,
(secs / 60) % 60,
secs % 60
)
}

0 comments on commit 70eef4a

Please sign in to comment.