Reschedule the reminders when adding a new one

The thread handling the reminders is unparked and reschedules the
execution of sending out reminders.
This commit is contained in:
finga 2023-02-02 08:35:11 +01:00
parent 7ded3ef430
commit a4a1234f06
4 changed files with 61 additions and 32 deletions

View file

@ -1,4 +1,4 @@
use crate::{schema, NewReminder};
use crate::{schema, AppState, NewReminder};
use anyhow::Error;
use axum::{
extract::State,
@ -6,10 +6,7 @@ use axum::{
response::{IntoResponse, Response, Result},
Json,
};
use diesel::{
prelude::*,
r2d2::{ConnectionManager, Pool},
};
use diesel::prelude::*;
use serde::Deserialize;
use time::OffsetDateTime;
use tracing::{error, trace};
@ -33,7 +30,7 @@ where
}
#[derive(Debug, Deserialize)]
pub struct CreateReminder {
pub struct Reminder {
#[serde(with = "time::serde::iso8601")]
planned: OffsetDateTime,
title: String,
@ -43,8 +40,8 @@ pub struct CreateReminder {
#[allow(clippy::unused_async)]
pub async fn create_reminder(
State(db_pool): State<Pool<ConnectionManager<PgConnection>>>,
Json(data): Json<CreateReminder>,
State(state): State<AppState>,
Json(data): Json<Reminder>,
) -> Result<impl IntoResponse, ServerError> {
let reminder = NewReminder {
created: OffsetDateTime::now_utc(),
@ -58,7 +55,10 @@ pub async fn create_reminder(
diesel::insert_into(schema::reminders::table)
.values(&reminder)
.execute(&mut db_pool.get()?)?;
.execute(&mut state.db_pool.get()?)?;
trace!("unpark reminder thread to reschedule next run");
state.reminder.thread().unpark();
Ok((StatusCode::CREATED, "Reminder created".to_string()))
}