-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(backend): parallelize watchpoints
Watchpoints are now checked in parallel. Cleanup code and move stuff around. Redis connection is now configured through the config values.
- Loading branch information
1 parent
1e494d6
commit 751d042
Showing
6 changed files
with
165 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,76 @@ | ||
use redis::Connection; | ||
use redis::{Commands, Connection}; | ||
|
||
use crate::get_config; | ||
use crate::watcher::{Watchpoint, WatchpointStatus}; | ||
|
||
/// Get a redis connection using the URL from the config file | ||
pub fn get_redis_connection() -> Connection { | ||
redis::Client::open("redis://127.0.01/") | ||
let settings = get_config(); | ||
let redis_url = settings | ||
.get_string("redis.url") | ||
.expect("unable to get redis string!"); | ||
|
||
redis::Client::open(redis_url) | ||
.expect("unable create redis client") | ||
.get_connection() | ||
.expect("unable to get redis connection") | ||
} | ||
|
||
/// Load the app configuration into memory (redis) | ||
pub fn load_config() -> bool { | ||
let settings = get_config(); | ||
|
||
let raw_watchlist = settings | ||
.get_array("watcher.watchlist") | ||
.expect("Invalid watch list"); | ||
|
||
// Make sure we have something to watch | ||
if raw_watchlist.is_empty() { | ||
println!("No entries in watchlist! Nothing to do"); | ||
return false; | ||
} | ||
|
||
let mut watchlist: Vec<Watchpoint> = Vec::new(); | ||
for i in &raw_watchlist { | ||
watchlist.push(i.clone().try_deserialize().expect("invalid config value")); | ||
} | ||
|
||
let mut con = get_redis_connection(); | ||
let _: () = con | ||
.set( | ||
"config:watchpoints", | ||
serde_json::to_string(&watchlist).unwrap(), | ||
) | ||
.unwrap(); | ||
|
||
true | ||
} | ||
|
||
/// Get a vector with all registered watchpoints | ||
pub fn get_watchpoints() -> Result<Vec<Watchpoint>, ()> { | ||
let mut con = get_redis_connection(); | ||
match con.get::<&str, String>("config:watchpoints") { | ||
Ok(k) => Ok(serde_json::from_str(k.as_str()).unwrap()), | ||
Err(_e) => Err(()), | ||
} | ||
} | ||
|
||
/// Get a vector with all registered watchpoints and their status | ||
pub fn get_watchpoint_status() -> Result<Vec<WatchpointStatus>, ()> { | ||
let mut con = get_redis_connection(); | ||
let watchpoints = match get_watchpoints() { | ||
Ok(d) => d, | ||
Err(()) => return Err(()), | ||
}; | ||
|
||
let mut data: Vec<WatchpointStatus> = Vec::new(); | ||
for watchpoint in watchpoints { | ||
let status = con | ||
.get::<&str, u16>(format!("status:{}:status-code", watchpoint.id).as_str()) | ||
.unwrap(); | ||
|
||
data.push(WatchpointStatus { watchpoint, status }); | ||
} | ||
|
||
Ok(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use actix_web::{get, HttpResponse, Responder}; | ||
|
||
use crate::cache::get_watchpoint_status; | ||
|
||
#[get("/status")] | ||
pub async fn status() -> impl Responder { | ||
match get_watchpoint_status() { | ||
Ok(d) => HttpResponse::Ok().json(d), | ||
Err(()) => HttpResponse::InternalServerError().finish(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use config::Config; | ||
|
||
/// Get config from config file and environmental variables | ||
pub fn get_config() -> Config { | ||
Config::builder() | ||
.add_source(config::File::with_name("config")) | ||
// Add in settings from the environment (with a prefix of APP) | ||
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key | ||
.add_source(config::Environment::with_prefix("APP")) | ||
.build() | ||
.expect("Invalid or missing config file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters