(toggl-portal): Add periodic polling config

This commit is contained in:
Joshua Coles 2023-11-29 13:53:09 +00:00
parent cc838ef145
commit 19acabd3da
2 changed files with 11 additions and 3 deletions

View File

@ -36,6 +36,10 @@ struct Config {
#[arg(long = "db", short, env)]
database_url: String,
/// How often to poll the Toggl API for new time entries
#[arg(long, short, env, default_value = "7200")]
poll_period: u64,
}
pub async fn report(
@ -126,7 +130,11 @@ async fn main() -> Result<()> {
Migrator::up(&db, None).await.expect("Failed to migrate");
tokio::spawn(poll::poll_job(toggl_client.clone(), db.clone()));
tokio::spawn(poll::poll_job(
toggl_client.clone(),
db.clone(),
config.poll_period,
));
// build our application with a route
let app = Router::new()

View File

@ -5,9 +5,9 @@ use tracing::instrument;
use crate::entity::{project, client};
#[tracing::instrument(skip(client, db))]
pub async fn poll_job(client: TogglClient, db: DatabaseConnection) {
pub async fn poll_job(client: TogglClient, db: DatabaseConnection, poll_period: u64) {
// Every 2h, poll the Toggl API for new time entries for today to cache them in the database
let period = tokio::time::Duration::from_secs(60 * 60 * 2);
let period = tokio::time::Duration::from_secs(poll_period);
loop {
tracing::info!("Polling Toggl API");