Skip to content

Commit

Permalink
Use envars instead of cmd arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lun4m authored and intarga committed Oct 11, 2024
1 parent 8c791fa commit 0ad64f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
22 changes: 10 additions & 12 deletions ingestion/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@ const PARAMCONV: &str = "resources/paramconversions.csv";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
println!("LARD ingestion service starting up...");
// TODO: use clap for argument parsing
let args: Vec<String> = std::env::args().collect();

if args.len() < 5 {
panic!(concat!(
"not enough args passed in. At least the group for the kafka queue,",
"and host, user, dbname needed, optionally password, for postgres"
))
if args.len() != 2 {
panic!(
"USAGE: lard_ingestion <kafka_group>\nEnv vars LARD_CONN_STRING and STINFO_CONN_STRING are also needed"
// env var format: host={} user={} dbname={} ...
)
}

let mut connect_string = format!("host={} user={} dbname={}", &args[2], &args[3], &args[4]);
if args.len() > 5 {
connect_string.push_str(" password=");
connect_string.push_str(&args[5])
};

// Permit tables handling (needs connection to stinfosys database)
let permit_tables = Arc::new(RwLock::new(permissions::fetch_permits().await?));
let background_permit_tables = permit_tables.clone();

println!("Spawing task to fetch permissions from StInfoSys...");
// background task to refresh permit tables every 30 mins
tokio::task::spawn(async move {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(30 * 60));
Expand All @@ -47,7 +43,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
});

// Set up postgres connection pool
let manager = PostgresConnectionManager::new_from_stringlike(connect_string, NoTls)?;
let manager =
PostgresConnectionManager::new_from_stringlike(std::env::var("LARD_CONN_STRING")?, NoTls)?;
let db_pool = bb8::Pool::builder().build(manager).await?;

// Spawn kvkafka reader
Expand All @@ -62,5 +59,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}

// Set up and run our server + database
println!("Ingestion server started!");
lard_ingestion::run(db_pool, PARAMCONV, permit_tables).await
}
3 changes: 2 additions & 1 deletion ingestion/src/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub type StationPermitTable = HashMap<StationId, PermitId>;
/// Get a fresh cache of permits from stinfosys
pub async fn fetch_permits() -> Result<(ParamPermitTable, StationPermitTable), Error> {
// get stinfo conn
let (client, conn) = tokio_postgres::connect(&std::env::var("STINFO_STRING")?, NoTls).await?;
let (client, conn) =
tokio_postgres::connect(&std::env::var("STINFO_CONN_STRING")?, NoTls).await?;

// conn object independently performs communication with database, so needs it's own task.
// it will return when the client is dropped
Expand Down

0 comments on commit 0ad64f2

Please sign in to comment.