Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rm loop run #26

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/default.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
reporting_interval_secs = 3600

[mqtt]
client_id = "affaldvarme"
host = "10.0.1.3"
Expand Down
15 changes: 15 additions & 0 deletions scripts/deploy-latest-and-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

image_name="untagged6785/ha-mitaffald"
container_name="ha-mitaffald"

docker pull $image_name:latest

echo "Stopping container $container_name"
docker stop $container_name || true

echo "Removing container $container_name"
docker rm $container_name || true

echo "Starting new container"
docker run -d --name $container_name -e affaldvarme.address.id -e mqtt.username -e mqtt.password --restart=on-failure:5 $image_name:latest
28 changes: 9 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@ use ha_mitaffald::sync_data;

#[tokio::main]
async fn main() {
loop {
println!("Starting data synchronization");
println!("Starting data synchronization");

let settings = Settings::new().expect("Failed to read settings");
let report_interval = tokio::time::Duration::from_secs(settings.reporting_interval_secs);
let report = sync_data(settings).await;
let settings = Settings::new().expect("Failed to read settings");
let report = sync_data(settings).await;

match report {
Ok(_) => println!("Data synchronization completed"),
Err(x) => eprintln!(
"Data synchronization failed (some entities may have been updated), error: {}",
x
),
}

println!(
"Next synchronization will take place at: {}",
chrono::Local::now() + report_interval
);

tokio::time::sleep(report_interval).await;
match report {
Ok(_) => println!("Data synchronization completed"),
Err(x) => eprintln!(
"Data synchronization failed (some entities may have been updated), error: {}",
x
),
}
}
3 changes: 1 addition & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use serde::Deserialize;
pub struct Settings {
pub mqtt: MQTTConfig,
pub affaldvarme: AffaldVarmeConfig,
pub reporting_interval_secs: u64,
}

impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let settings = Config::builder()
.add_source(File::with_name("config/default.toml"))
.add_source(File::with_name("config/secrets.toml").required(false))
.add_source(Environment::default())
.add_source(Environment::default().separator("_"))
.build()?;

settings.try_deserialize()
Expand Down
1 change: 0 additions & 1 deletion tests/full_flow_insta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async fn smoke_test_insta() {
.await;

let settings = Settings {
reporting_interval_secs: 100,
affaldvarme: AffaldVarmeConfig {
address: Address::Id(AddressId { id: address_id }),
base_url: mit_affald_server_url,
Expand Down