smart-fridge 2.0.0

A tiny service to manage food in your fridge
Documentation
use anyhow::Context;
use sqlx::postgres::PgPoolOptions;
use time::{ext::NumericalDuration, Duration};

use smart_fridge::db::{cleanup, get_db_url};

#[derive(impl_new::New)]
struct CleanupParam {
    food_expiration_period: Duration,
    check_food_period: Duration,
}

#[tokio::main]
#[allow(unreachable_code)]
async fn main() -> anyhow::Result<()> {
    env_logger::init();
    let db = PgPoolOptions::new()
        .max_connections(5)
        .connect(&get_db_url()?)
        .await
        .context("Failed to connect to the database")?;

    let cleanup_param = CleanupParam::new(3.days(), 1.hours());

    // Wait init of the database from the web_api server
    tokio::time::sleep(std::time::Duration::from_secs(30)).await;

    // TODO: Paramétrage statique ou dynamique ? API http pour modifié les paramètres ?
    // TODO: Ajouter une condition de fin de boucle ?
    loop {
        let _nb_deleted = cleanup(db.clone(), cleanup_param.food_expiration_period).await;
        // TODO: Notifié lorsque des aliments sont supprimmer car périmé depuis trop longtemps

        tokio::time::sleep(std::time::Duration::from_secs_f32(
            cleanup_param.check_food_period.as_seconds_f32(),
        ))
        .await;
    }

    Ok(())
}