diff options
author | FxQnLr <[email protected]> | 2024-02-28 19:05:49 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-28 19:05:49 +0100 |
commit | 0920c86de3523785b5f4ac67e2090f0736f9fcb2 (patch) | |
tree | 15bb3dd411b901238ebce3107345f4976e23f31e /src | |
parent | f0dc13f907a72ffef44f89b5e197567db129b020 (diff) | |
download | webol-0920c86de3523785b5f4ac67e2090f0736f9fcb2.tar webol-0920c86de3523785b5f4ac67e2090f0736f9fcb2.tar.gz webol-0920c86de3523785b5f4ac67e2090f0736f9fcb2.zip |
Closes #18. Added pingthreshold. Cargo update
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 3 | ||||
-rw-r--r-- | src/services/ping.rs | 31 |
2 files changed, 20 insertions, 14 deletions
diff --git a/src/config.rs b/src/config.rs index 4319ffc..58043c2 100644 --- a/src/config.rs +++ b/src/config.rs | |||
@@ -7,6 +7,7 @@ pub struct Config { | |||
7 | pub apikey: String, | 7 | pub apikey: String, |
8 | pub serveraddr: String, | 8 | pub serveraddr: String, |
9 | pub pingtimeout: i64, | 9 | pub pingtimeout: i64, |
10 | pub pingthreshold: i64, | ||
10 | } | 11 | } |
11 | 12 | ||
12 | impl Config { | 13 | impl Config { |
@@ -14,6 +15,7 @@ impl Config { | |||
14 | let config = config::Config::builder() | 15 | let config = config::Config::builder() |
15 | .set_default("serveraddr", "0.0.0.0:7229")? | 16 | .set_default("serveraddr", "0.0.0.0:7229")? |
16 | .set_default("pingtimeout", 10)? | 17 | .set_default("pingtimeout", 10)? |
18 | .set_default("pingthreshold", 1)? | ||
17 | .add_source(File::with_name("config.toml").required(false)) | 19 | .add_source(File::with_name("config.toml").required(false)) |
18 | .add_source(File::with_name("config.dev.toml").required(false)) | 20 | .add_source(File::with_name("config.dev.toml").required(false)) |
19 | .add_source(config::Environment::with_prefix("WEBOL").prefix_separator("_")) | 21 | .add_source(config::Environment::with_prefix("WEBOL").prefix_separator("_")) |
@@ -22,4 +24,3 @@ impl Config { | |||
22 | config.try_deserialize() | 24 | config.try_deserialize() |
23 | } | 25 | } |
24 | } | 26 | } |
25 | |||
diff --git a/src/services/ping.rs b/src/services/ping.rs index 9191f86..8cf6072 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -49,22 +49,27 @@ pub async fn spawn( | |||
49 | }; | 49 | }; |
50 | } | 50 | } |
51 | 51 | ||
52 | trace!(?msg); | ||
53 | |||
52 | let msg = msg.expect("fatal error"); | 54 | let msg = msg.expect("fatal error"); |
53 | 55 | ||
54 | let _ = tx.send(msg.clone()); | 56 | let _ = tx.send(msg.clone()); |
55 | if let BroadcastCommands::Success = msg.command { | 57 | if msg.command == BroadcastCommands::Success { |
56 | sqlx::query!( | 58 | if timer.elapsed().whole_seconds() > config.pingthreshold { |
57 | r#" | 59 | sqlx::query!( |
58 | UPDATE devices | 60 | r#" |
59 | SET times = array_append(times, $1) | 61 | UPDATE devices |
60 | WHERE id = $2; | 62 | SET times = array_append(times, $1) |
61 | "#, | 63 | WHERE id = $2; |
62 | timer.elapsed().whole_seconds(), | 64 | "#, |
63 | device.id | 65 | timer.elapsed().whole_seconds(), |
64 | ) | 66 | device.id |
65 | .execute(db) | 67 | ) |
66 | .await | 68 | .execute(db) |
67 | .unwrap(); | 69 | .await |
70 | .unwrap(); | ||
71 | } | ||
72 | |||
68 | ping_map.insert( | 73 | ping_map.insert( |
69 | uuid.clone(), | 74 | uuid.clone(), |
70 | Value { | 75 | Value { |