Skip to content

Commit 96734ad

Browse files
authored
Merge pull request #528 from AmbireTech/general-improvements
General improvements & sentry graceful shutdown
2 parents 853c769 + 656dc1a commit 96734ad

File tree

12 files changed

+120
-77
lines changed

12 files changed

+120
-77
lines changed

Cargo.lock

Lines changed: 26 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

primitives/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ serde_json = "1.0"
4040
serde-hex = "0.1"
4141
serde_millis = "0.1"
4242
# Used prefixes on field for targeting::Input, and `campaign::Active`
43-
serde_with = "1"
43+
serde_with = "2"
4444
# Configuration
4545
toml = "0.5"
4646
# Logging
@@ -83,4 +83,4 @@ once_cell = "1"
8383
[dev-dependencies]
8484
pretty_assertions = "1"
8585
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
86-
serde_qs = "0.9"
86+
serde_qs = "0.10"

primitives/src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,16 @@ pub fn configuration(
309309
},
310310
}
311311
}
312+
313+
#[cfg(test)]
314+
mod test {
315+
use super::{GANACHE_CONFIG, PRODUCTION_CONFIG};
316+
317+
/// Makes sure that both config files are correct and won't be left in a
318+
/// broken state.
319+
#[test]
320+
fn correct_config_files() {
321+
let _ganache = GANACHE_CONFIG.clone();
322+
let _production = PRODUCTION_CONFIG.clone();
323+
}
324+
}

primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub mod sentry;
4646
pub mod spender;
4747
pub mod supermarket;
4848
pub mod targeting;
49-
#[cfg(feature = "test-util")]
49+
#[cfg(any(test, feature = "test-util"))]
5050
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
5151
pub mod test_util;
5252
pub mod unified_num;

primitives/src/sentry.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,15 +1129,55 @@ mod postgres {
11291129
}
11301130
}
11311131
}
1132+
1133+
#[cfg(test)]
1134+
mod test {
1135+
use chrono::{TimeZone, Utc};
1136+
1137+
use crate::{postgres::POSTGRES_POOL, sentry::DateHour};
1138+
1139+
#[tokio::test]
1140+
pub async fn datehour_from_to_sql() {
1141+
let client = POSTGRES_POOL.get().await.unwrap();
1142+
let sql_type = "TIMESTAMPTZ";
1143+
1144+
let example_datehour = DateHour::<Utc>::from_ymdh(2021, 1, 1, 1);
1145+
let expected_datehour = DateHour::try_from(Utc.ymd(2021, 1, 1).and_hms(1, 0, 0))
1146+
.expect("Should get DateHour");
1147+
assert_eq!(
1148+
example_datehour, expected_datehour,
1149+
"Example and expected datehour must be the same"
1150+
);
1151+
1152+
// from SQL
1153+
let actual_datehour: DateHour<Utc> = client
1154+
.query_one(
1155+
&*format!("SELECT '{}'::{}", example_datehour.to_datetime(), sql_type),
1156+
&[],
1157+
)
1158+
.await
1159+
.unwrap()
1160+
.get(0);
1161+
1162+
assert_eq!(&expected_datehour, &actual_datehour);
1163+
1164+
// to SQL
1165+
let actual_datehour: DateHour<Utc> = client
1166+
.query_one(&*format!("SELECT $1::{}", sql_type), &[&example_datehour])
1167+
.await
1168+
.unwrap()
1169+
.get(0);
1170+
1171+
assert_eq!(&expected_datehour, &actual_datehour);
1172+
}
1173+
}
11321174
}
11331175

11341176
#[cfg(test)]
11351177
mod test {
11361178
use super::*;
1137-
use crate::{
1138-
postgres::POSTGRES_POOL,
1139-
test_util::{DUMMY_IPFS, PUBLISHER},
1140-
};
1179+
use crate::test_util::{DUMMY_IPFS, PUBLISHER};
1180+
11411181
use serde_json::{json, Value};
11421182

11431183
#[test]
@@ -1243,39 +1283,4 @@ mod test {
12431283
"UTC+0 value should be equal to UTC+2"
12441284
);
12451285
}
1246-
1247-
#[tokio::test]
1248-
pub async fn datehour_from_to_sql() {
1249-
let client = POSTGRES_POOL.get().await.unwrap();
1250-
let sql_type = "TIMESTAMPTZ";
1251-
1252-
let example_datehour = DateHour::<Utc>::from_ymdh(2021, 1, 1, 1);
1253-
let expected_datehour =
1254-
DateHour::try_from(Utc.ymd(2021, 1, 1).and_hms(1, 0, 0)).expect("Should get DateHour");
1255-
assert_eq!(
1256-
example_datehour, expected_datehour,
1257-
"Example and expected datehour must be the same"
1258-
);
1259-
1260-
// from SQL
1261-
let actual_datehour: DateHour<Utc> = client
1262-
.query_one(
1263-
&*format!("SELECT '{}'::{}", example_datehour.to_datetime(), sql_type),
1264-
&[],
1265-
)
1266-
.await
1267-
.unwrap()
1268-
.get(0);
1269-
1270-
assert_eq!(&expected_datehour, &actual_datehour);
1271-
1272-
// to SQL
1273-
let actual_datehour: DateHour<Utc> = client
1274-
.query_one(&*format!("SELECT $1::{}", sql_type), &[&example_datehour])
1275-
.await
1276-
.unwrap()
1277-
.get(0);
1278-
1279-
assert_eq!(&expected_datehour, &actual_datehour);
1280-
}
12811286
}

sentry/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ edition = "2021"
1111

1212
default-run = "sentry"
1313

14+
[package.metadata.docs.rs]
15+
all-features = true
16+
rustdoc-args = ["--cfg", "docsrs"]
17+
1418
[[bin]]
1519
name = "seed"
1620
test = false # Is tested by default.
@@ -37,7 +41,7 @@ hex = "0.4"
3741
# CLI
3842
clap = { version = "3", features = ["cargo"] }
3943
# Server
40-
tokio = { version = "1", features = ["macros", "time", "rt-multi-thread"] }
44+
tokio = { version = "1", features = ["macros", "time", "rt-multi-thread", "signal"] }
4145
hyper = { version = "0.14", features = ["stream", "runtime", "http1", "http2", "server"] }
4246
simple-hyper-server-tls = { version = "0.3", features = ["tls-rustls"] }
4347
regex = "1"
@@ -63,7 +67,7 @@ envy = "0.4"
6367
# Serde
6468
serde = { version = "1", features = ["derive"] }
6569
serde_json = "1"
66-
serde_qs = "0.9"
70+
serde_qs = "0.10"
6771
# Other
6872
thiserror = "1"
6973
once_cell = "1"
@@ -77,7 +81,3 @@ pretty_assertions = "1"
7781

7882
primitives = { path = "../primitives", features = ["postgres", "test-util"] }
7983
adapter = { version = "0.2", path = "../adapter", features = ["test-util"] }
80-
81-
[package.metadata.docs.rs]
82-
all-features = true
83-
rustdoc-args = ["--cfg", "docsrs"]

sentry/src/application.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ impl<C: Locked + 'static> Application<C> {
183183
}
184184
});
185185

186-
let server = Server::bind(&socket_addr).serve(make_service);
186+
let server = Server::bind(&socket_addr)
187+
.serve(make_service)
188+
.with_graceful_shutdown(shutdown_signal(logger.clone()));
187189

188190
if let Err(e) = server.await {
189191
error!(&logger, "server error: {}", e; "main" => "run");
@@ -201,7 +203,10 @@ impl<C: Locked + 'static> Application<C> {
201203
});
202204

203205
// TODO: Find a way to redirect to HTTPS
204-
let mut server = Server::builder(listener).serve(make_service);
206+
let server = Server::builder(listener)
207+
.serve(make_service)
208+
.with_graceful_shutdown(shutdown_signal(logger.clone()));
209+
tokio::pin!(server);
205210

206211
while let Err(e) = (&mut server).await {
207212
// This is usually caused by trying to connect on HTTP instead of HTTPS
@@ -273,6 +278,16 @@ pub struct Auth {
273278
pub chain: primitives::Chain,
274279
}
275280

281+
/// A Ctrl+C signal to gracefully shutdown the server
282+
async fn shutdown_signal(logger: Logger) {
283+
// Wait for the Ctrl+C signal
284+
tokio::signal::ctrl_c()
285+
.await
286+
.expect("failed to install CTRL+C signal handler");
287+
288+
info!(&logger, "Received Ctrl+C signal. Shutting down..")
289+
}
290+
276291
#[cfg(test)]
277292
mod test {
278293
use serde_json::json;

0 commit comments

Comments
 (0)