server: poll for new messages and update clients via WS
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Rocket generates a lot of warnings for handlers
|
||||
// TODO: figure out why
|
||||
#![allow(unreachable_patterns)]
|
||||
use std::{error::Error, io::Cursor, net::SocketAddr, str::FromStr, sync::Arc};
|
||||
use std::{error::Error, io::Cursor, net::SocketAddr, str::FromStr, sync::Arc, time::Duration};
|
||||
|
||||
use async_graphql::{extensions, http::GraphiQLSource, Schema};
|
||||
use async_graphql_axum::{GraphQL, GraphQLSubscription};
|
||||
@@ -21,7 +21,9 @@ use letterbox_server::tantivy::TantivyConnection;
|
||||
use letterbox_server::{
|
||||
config::Config,
|
||||
error::ServerError,
|
||||
graphql::{Attachment, GraphqlSchema, MutationRoot, QueryRoot, SubscriptionRoot},
|
||||
graphql::{
|
||||
compute_catchup_ids, Attachment, GraphqlSchema, MutationRoot, QueryRoot, SubscriptionRoot,
|
||||
},
|
||||
nm::{attachment_bytes, cid_attachment_bytes},
|
||||
ws::ConnectionTracker,
|
||||
};
|
||||
@@ -294,19 +296,44 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
std::fs::create_dir_all(&config.slurp_cache_path)?;
|
||||
}
|
||||
let pool = PgPool::connect(&config.newsreader_database_url).await?;
|
||||
let nm = Notmuch::default();
|
||||
sqlx::migrate!("./migrations").run(&pool).await?;
|
||||
#[cfg(feature = "tantivy")]
|
||||
let tantivy_conn = TantivyConnection::new(&config.newsreader_tantivy_db_path)?;
|
||||
|
||||
let cacher = FilesystemCacher::new(&config.slurp_cache_path)?;
|
||||
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
|
||||
.data(Notmuch::default())
|
||||
.data(nm.clone())
|
||||
.data(cacher)
|
||||
.data(pool.clone());
|
||||
|
||||
let schema = schema.extension(extensions::Logger).finish();
|
||||
|
||||
let conn_tracker = Arc::new(Mutex::new(ConnectionTracker::default()));
|
||||
async fn watch_new(
|
||||
nm: Notmuch,
|
||||
pool: PgPool,
|
||||
conn_tracker: Arc<Mutex<ConnectionTracker>>,
|
||||
poll_time: Duration,
|
||||
) -> Result<(), async_graphql::Error> {
|
||||
let mut old_ids = Vec::new();
|
||||
loop {
|
||||
let ids = compute_catchup_ids(&nm, &pool, "is:unread").await?;
|
||||
if old_ids != ids {
|
||||
info!("old_ids: {old_ids:?}\n ids: {ids:?}");
|
||||
conn_tracker
|
||||
.lock()
|
||||
.await
|
||||
.send_message_all(WebsocketMessage::RefreshMessages)
|
||||
.await
|
||||
}
|
||||
old_ids = ids;
|
||||
tokio::time::sleep(poll_time).await;
|
||||
}
|
||||
}
|
||||
let ct = Arc::clone(&conn_tracker);
|
||||
let poll_time = Duration::from_secs(10);
|
||||
let _h = tokio::spawn(watch_new(nm, pool, ct, poll_time));
|
||||
|
||||
let app = Router::new()
|
||||
.route("/test", get(test_handler))
|
||||
|
||||
Reference in New Issue
Block a user