Add notifier to break relay loop
This commit is contained in:
parent
83c1f033d5
commit
8785186dfa
12 changed files with 124 additions and 45 deletions
emgauwa-controller/src
|
@ -1,8 +1,11 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use actix::{Actor, Context, Handler, Message};
|
||||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use emgauwa_lib::models::Controller;
|
||||
use futures::executor::block_on;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use tokio::sync::Notify;
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Result<(), EmgauwaError>")]
|
||||
|
@ -12,14 +15,27 @@ pub struct Reload {}
|
|||
#[rtype(result = "Controller")]
|
||||
pub struct GetThis {}
|
||||
|
||||
#[derive(Message)]
|
||||
#[rtype(result = "Arc<Notify>")]
|
||||
pub struct GetNotifier {}
|
||||
|
||||
pub struct AppState {
|
||||
pub pool: Pool<Sqlite>,
|
||||
pub this: Controller,
|
||||
pub notifier: Arc<Notify>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(pool: Pool<Sqlite>, this: Controller) -> AppState {
|
||||
AppState { pool, this }
|
||||
AppState {
|
||||
pool,
|
||||
this,
|
||||
notifier: Arc::new(Notify::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn notify_change(&self) {
|
||||
self.notifier.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,6 +51,8 @@ impl Handler<Reload> for AppState {
|
|||
|
||||
self.this.reload(&mut pool_conn)?;
|
||||
|
||||
self.notify_change();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -46,3 +64,11 @@ impl Handler<GetThis> for AppState {
|
|||
self.this.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<GetNotifier> for AppState {
|
||||
type Result = Arc<Notify>;
|
||||
|
||||
fn handle(&mut self, _msg: GetNotifier, _ctx: &mut Self::Context) -> Self::Result {
|
||||
Arc::clone(&self.notifier)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue