Skip to content

Commit

Permalink
chore: log informative err msg on startup failures
Browse files Browse the repository at this point in the history
  • Loading branch information
kasbuunk committed Dec 14, 2024
1 parent 8d90f52 commit f3847fa
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::process;
use std::sync::Arc;

use chrono::prelude::*;
use log::info;
use log::{error, info};
use tokio::signal::unix::{signal, SignalKind};
use tokio_util::sync::CancellationToken;

Expand Down Expand Up @@ -46,7 +46,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Construct transmitter.
let transmitter: Arc<dyn contract::Transmitter> = match config.transmitter {
config::Transmitter::Nats(nats_config) => {
let nats_client = nats::connect_to_nats(nats_config).await?;
let nats_client = match nats::connect_to_nats(nats_config).await {
Ok(client) => client,
Err(err) => {
error!("Failed to initialise nats connection: {}", err);
process::exit(1);
}
};

let transmitter = transmitter_nats::NatsPublisher::new(nats_client);
info!("Initialised nats transmitter.");

Expand All @@ -57,19 +64,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Construct repository.
let repository: Arc<dyn contract::Repository> = match config.repository {
config::Repository::Postgres(postgres_config) => {
let postgres_connection = postgres::connect_to_database(postgres_config).await?;
let postgres_connection = match postgres::connect_to_database(postgres_config).await {
Ok(client) => client,
Err(err) => {
error!("Failed to initialise postgres connection: {}", err);
process::exit(1);
}
};

let repository = repository_postgres::RepositoryPostgres::new(postgres_connection);

info!("Initialised postgres repository.");

if config.automigrate {
info!("Running migrations.");

repository.migrate().await?;
match repository.migrate().await {
Ok(_) => (),
Err(err) => {
error!("Failed to run sql migrations: {}", err);
process::exit(1);
}
};
}

if config.reset_state {
repository.clear_all().await?;
match repository.clear_all().await {
Ok(_) => (),
Err(err) => {
error!("Failed to reset repository state: {}", err);
process::exit(1);
}
};
}

Arc::new(repository)
Expand Down

0 comments on commit f3847fa

Please sign in to comment.