mirror of
https://git.funtimes909.xyz/ServerSeekerV2/ServerSeekerV2.git
synced 2026-10-11 18:08:14 +08:00
Moderately more robust error handling
This commit is contained in:
1 parent
2454771c7a
commit
3044131580
5 files changed
+17
-11
No files matched your search
Generated
+1
@@ -7,6 +7,7 @@ name = "ServerSeekerV2-Rescanner"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bigdecimal",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
|
||||
@@ -11,3 +11,4 @@ toml = "0.8.19"
|
||||
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }
|
||||
bigdecimal = "0.4.7"
|
||||
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio"] }
|
||||
log = "0.4.25"
|
||||
+6
-8
@@ -14,22 +14,20 @@ pub async fn connect(database_url: &str) -> PgConnection {
|
||||
}
|
||||
|
||||
// TODO! Return a stream of results instead of a Vec for performance
|
||||
pub async fn fetch_servers(conn: &mut PgConnection) -> Vec<String> {
|
||||
pub async fn fetch_servers(conn: &mut PgConnection) -> Result<Vec<String>, Error> {
|
||||
// Sort results by oldest
|
||||
sqlx::query("SELECT address FROM servers ORDER BY lastseen ASC")
|
||||
.fetch_all(conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.await?
|
||||
.iter()
|
||||
.map(|row| {
|
||||
row.try_get(0).unwrap()
|
||||
})
|
||||
.collect()
|
||||
row.try_get(0)
|
||||
}).collect()
|
||||
}
|
||||
|
||||
pub async fn update_server(server: Server, conn: &mut PgConnection, address: &str) -> Result<PgQueryResult, Error> {
|
||||
let lastseen = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||
Ok(t) => t.as_secs() as i32,
|
||||
Ok(t) => t.as_secs(),
|
||||
Err(_) => panic!("System clock set before unix epoch!")
|
||||
};
|
||||
|
||||
@@ -49,7 +47,7 @@ pub async fn update_server(server: Server, conn: &mut PgConnection, address: &st
|
||||
.bind(server.motd)
|
||||
.bind(server.prevents_reports)
|
||||
.bind(server.enforces_secure_chat)
|
||||
.bind(lastseen)
|
||||
.bind(lastseen as i32)
|
||||
.bind(address);
|
||||
|
||||
query.execute(conn).await
|
||||
|
||||
+7
-1
@@ -10,6 +10,7 @@ use config::{load_config, Config};
|
||||
use std::io::Error;
|
||||
use std::io::{Read, Write};
|
||||
use std::str::FromStr;
|
||||
use log::error;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -26,7 +27,12 @@ async fn main() {
|
||||
config.database.table);
|
||||
|
||||
let mut conn = connect(database_url.as_str()).await;
|
||||
let servers = fetch_servers(&mut conn).await;
|
||||
|
||||
let servers = match fetch_servers(&mut conn).await {
|
||||
Ok(servers) => servers,
|
||||
// Error will eventually break from main loop
|
||||
Err(err) => panic!("{}", err),
|
||||
};
|
||||
|
||||
for address in servers {
|
||||
println!("Pinging: {}", &address);
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@ const REQUEST: [u8; 9] = [
|
||||
|
||||
pub async fn ping_server(address: &str, port: u16) -> Result<Server, Error> {
|
||||
let address = format!("{}:{}", address, port);
|
||||
let socket = SocketAddr::from_str(address.as_str()).unwrap();
|
||||
let socket = SocketAddr::from_str(address.as_str()).map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;
|
||||
|
||||
match TcpStream::connect_timeout(&socket, Duration::from_secs(3)) {
|
||||
Ok(mut stream) => {
|
||||
@@ -51,7 +51,7 @@ pub async fn ping_server(address: &str, port: u16) -> Result<Server, Error> {
|
||||
Ok(parse_response(response.as_str())?)
|
||||
}
|
||||
Err(_) => {
|
||||
Err(Error::new(ErrorKind::NetworkUnreachable, "Server did not respond."))
|
||||
Err(Error::new(ErrorKind::ConnectionRefused, "Server did not respond."))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user