server & web: run label_unprocessed before notifying web client
This commit is contained in:
parent
c10ad00ca7
commit
5226fe090e
@ -21,7 +21,7 @@ use letterbox_notmuch::Notmuch;
|
|||||||
use letterbox_server::tantivy::TantivyConnection;
|
use letterbox_server::tantivy::TantivyConnection;
|
||||||
use letterbox_server::{
|
use letterbox_server::{
|
||||||
graphql::{compute_catchup_ids, Attachment, MutationRoot, QueryRoot, SubscriptionRoot},
|
graphql::{compute_catchup_ids, Attachment, MutationRoot, QueryRoot, SubscriptionRoot},
|
||||||
nm::{attachment_bytes, cid_attachment_bytes},
|
nm::{attachment_bytes, cid_attachment_bytes, label_unprocessed},
|
||||||
ws::ConnectionTracker,
|
ws::ConnectionTracker,
|
||||||
};
|
};
|
||||||
use letterbox_shared::WebsocketMessage;
|
use letterbox_shared::WebsocketMessage;
|
||||||
@ -29,7 +29,7 @@ use serde::Deserialize;
|
|||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
use tokio::{net::TcpListener, sync::Mutex};
|
use tokio::{net::TcpListener, sync::Mutex};
|
||||||
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
|
use tower_http::trace::{DefaultMakeSpan, TraceLayer};
|
||||||
use tracing::info;
|
use tracing::{error, info};
|
||||||
|
|
||||||
// Make our own error that wraps `ServerError`.
|
// Make our own error that wraps `ServerError`.
|
||||||
struct AppError(letterbox_server::ServerError);
|
struct AppError(letterbox_server::ServerError);
|
||||||
@ -176,11 +176,15 @@ async fn start_ws(
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct NotificationParams {
|
struct NotificationParams {
|
||||||
delay_ms: Option<u64>,
|
delay_ms: Option<u64>,
|
||||||
|
num_unprocessed: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_refresh_websocket_handler(
|
async fn send_refresh_websocket_handler(
|
||||||
State(AppState {
|
State(AppState {
|
||||||
connection_tracker, ..
|
nm,
|
||||||
|
pool,
|
||||||
|
connection_tracker,
|
||||||
|
..
|
||||||
}): State<AppState>,
|
}): State<AppState>,
|
||||||
params: Query<NotificationParams>,
|
params: Query<NotificationParams>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
@ -190,6 +194,15 @@ async fn send_refresh_websocket_handler(
|
|||||||
info!("sleeping {delay:?}");
|
info!("sleeping {delay:?}");
|
||||||
tokio::time::sleep(delay).await;
|
tokio::time::sleep(delay).await;
|
||||||
}
|
}
|
||||||
|
let limit = match params.num_unprocessed {
|
||||||
|
Some(0) => None,
|
||||||
|
Some(limit) => Some(limit),
|
||||||
|
None => Some(10),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(err) = label_unprocessed(&nm, &pool, false, limit, "tag:unprocessed").await {
|
||||||
|
error!("Failed to label_unprocessed: {err:?}");
|
||||||
|
};
|
||||||
connection_tracker
|
connection_tracker
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
@ -204,18 +217,33 @@ async fn watch_new(
|
|||||||
conn_tracker: Arc<Mutex<ConnectionTracker>>,
|
conn_tracker: Arc<Mutex<ConnectionTracker>>,
|
||||||
poll_time: Duration,
|
poll_time: Duration,
|
||||||
) -> Result<(), async_graphql::Error> {
|
) -> Result<(), async_graphql::Error> {
|
||||||
let mut old_ids = Vec::new();
|
async fn watch_new_iteration(
|
||||||
loop {
|
nm: &Notmuch,
|
||||||
|
pool: &PgPool,
|
||||||
|
conn_tracker: Arc<Mutex<ConnectionTracker>>,
|
||||||
|
old_ids: &[String],
|
||||||
|
) -> Result<Vec<String>, async_graphql::Error> {
|
||||||
let ids = compute_catchup_ids(&nm, &pool, "is:unread").await?;
|
let ids = compute_catchup_ids(&nm, &pool, "is:unread").await?;
|
||||||
|
info!("old_ids: {} ids: {}", old_ids.len(), ids.len());
|
||||||
if old_ids != ids {
|
if old_ids != ids {
|
||||||
info!("old_ids: {old_ids:?}\n ids: {ids:?}");
|
label_unprocessed(&nm, &pool, false, Some(100), "tag:unprocessed").await?;
|
||||||
conn_tracker
|
conn_tracker
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
.send_message_all(WebsocketMessage::RefreshMessages)
|
.send_message_all(WebsocketMessage::RefreshMessages)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
old_ids = ids;
|
Ok(ids)
|
||||||
|
}
|
||||||
|
let mut old_ids = Vec::new();
|
||||||
|
loop {
|
||||||
|
old_ids = match watch_new_iteration(&nm, &pool, conn_tracker.clone(), &old_ids).await {
|
||||||
|
Ok(old_ids) => old_ids,
|
||||||
|
Err(err) => {
|
||||||
|
error!("watch_new_iteration failed: {err:?}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
tokio::time::sleep(poll_time).await;
|
tokio::time::sleep(poll_time).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -223,6 +251,7 @@ async fn watch_new(
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
nm: Notmuch,
|
nm: Notmuch,
|
||||||
|
pool: PgPool,
|
||||||
connection_tracker: Arc<Mutex<ConnectionTracker>>,
|
connection_tracker: Arc<Mutex<ConnectionTracker>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,7 +292,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let connection_tracker = Arc::new(Mutex::new(ConnectionTracker::default()));
|
let connection_tracker = Arc::new(Mutex::new(ConnectionTracker::default()));
|
||||||
let ct = Arc::clone(&connection_tracker);
|
let ct = Arc::clone(&connection_tracker);
|
||||||
let poll_time = Duration::from_secs(60);
|
let poll_time = Duration::from_secs(60);
|
||||||
let _h = tokio::spawn(watch_new(nm.clone(), pool, ct, poll_time));
|
let _h = tokio::spawn(watch_new(nm.clone(), pool.clone(), ct, poll_time));
|
||||||
|
|
||||||
let api_routes = Router::new()
|
let api_routes = Router::new()
|
||||||
.route(
|
.route(
|
||||||
@ -288,6 +317,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
.nest("/notification", notification_routes)
|
.nest("/notification", notification_routes)
|
||||||
.with_state(AppState {
|
.with_state(AppState {
|
||||||
nm,
|
nm,
|
||||||
|
pool,
|
||||||
connection_tracker,
|
connection_tracker,
|
||||||
})
|
})
|
||||||
.layer(
|
.layer(
|
||||||
|
|||||||
@ -671,7 +671,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
|
|||||||
}
|
}
|
||||||
Msg::WebsocketMessage(msg) => {
|
Msg::WebsocketMessage(msg) => {
|
||||||
match msg {
|
match msg {
|
||||||
WebsocketMessage::RefreshMessages => orders.send_msg(Msg::RefreshStart),
|
WebsocketMessage::RefreshMessages => orders.send_msg(Msg::Refresh),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user