Skip to content

Commit b3cb6af

Browse files
committed
refactor(threads): add timeout for thread's status updates
1 parent 0dce6e8 commit b3cb6af

7 files changed

Lines changed: 41 additions & 15 deletions

File tree

rustmail/src/commands/anonreply/text_command/anonreply.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use chrono::Utc;
88
use serenity::all::{Context, GuildId, Message, UserId};
99
use std::collections::HashMap;
1010
use std::sync::Arc;
11+
use crate::modules::update_thread_status_ui;
1112

1213
pub async fn anonreply(
1314
ctx: Context,
@@ -77,7 +78,8 @@ pub async fn anonreply(
7778

7879
ticket_status.last_message_by = TicketAuthor::Staff;
7980
ticket_status.last_message_at = Utc::now().timestamp();
80-
update_thread_status(&thread.id, &ticket_status, db_pool).await?;
81+
update_thread_status_db(&thread.id, &ticket_status, db_pool).await?;
82+
update_thread_status_ui(&ctx, &ticket_status).await?;
8183

8284
let _ = msg.delete(&ctx.http).await;
8385

rustmail/src/commands/reply/slash_command/reply.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use serenity::all::{
1414
};
1515
use std::collections::HashMap;
1616
use std::sync::Arc;
17+
use crate::modules::update_thread_status_ui;
1718

1819
pub struct ReplyCommand;
1920

@@ -166,7 +167,8 @@ impl RegistrableCommand for ReplyCommand {
166167

167168
ticket_status.last_message_by = TicketAuthor::Staff;
168169
ticket_status.last_message_at = Utc::now().timestamp();
169-
update_thread_status(&thread.id, &ticket_status, db_pool).await?;
170+
update_thread_status_db(&thread.id, &ticket_status, db_pool).await?;
171+
update_thread_status_ui(&ctx, &ticket_status).await?;
170172

171173
let mut sr = MessageBuilder::begin_staff_reply(
172174
&ctx,

rustmail/src/commands/reply/text_command/reply.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use chrono::Utc;
88
use serenity::all::{Context, GuildId, Message, UserId};
99
use std::collections::HashMap;
1010
use std::sync::Arc;
11+
use crate::modules::update_thread_status_ui;
1112

1213
pub async fn reply(
1314
ctx: Context,
@@ -49,7 +50,8 @@ pub async fn reply(
4950

5051
ticket_status.last_message_by = TicketAuthor::Staff;
5152
ticket_status.last_message_at = Utc::now().timestamp();
52-
update_thread_status(&thread.id, &ticket_status, db_pool).await?;
53+
update_thread_status_db(&thread.id, &ticket_status, db_pool).await?;
54+
update_thread_status_ui(&ctx, &ticket_status).await?;
5355

5456
let _ = msg.delete(&ctx.http).await;
5557

rustmail/src/db/operations/threads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ pub async fn get_thread_status(thread_id: &str, pool: &SqlitePool) -> Option<Tic
479479
}
480480
}
481481

482-
pub async fn update_thread_status(
482+
pub async fn update_thread_status_db(
483483
thread_id: &str,
484484
ticket: &TicketState,
485485
pool: &SqlitePool,

rustmail/src/handlers/ready_handler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ fn update_threads_status(ctx: &Context, pool: &SqlitePool) {
101101
async move {
102102
let mut interval = interval(Duration::from_secs(60 * 10));
103103

104+
interval.tick().await;
105+
104106
loop {
105107
let tickets_status = get_all_thread_status(&pool).await;
106108

@@ -110,7 +112,7 @@ fn update_threads_status(ctx: &Context, pool: &SqlitePool) {
110112
let ticket = ticket.clone();
111113
let handle = tokio::spawn(async move {
112114
println!("Update");
113-
if let Err(e) = update_thread_status(&ctx, &ticket).await {
115+
if let Err(e) = update_thread_status_ui(&ctx, &ticket).await {
114116
eprintln!(
115117
"Failed to update thread status for channel {}: {:?}",
116118
ticket.channel_id, e

rustmail/src/modules/threads_status.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use std::time::Duration;
12
use crate::prelude::errors::*;
23
use crate::prelude::types::*;
34
use chrono::Utc;
45
use serenity::all::{ChannelId, UserId};
56
use serenity::builder::EditChannel;
67
use serenity::client::Context;
8+
use tokio::time::timeout;
79

8-
pub async fn update_thread_status(ctx: &Context, ticket: &TicketState) -> ModmailResult<()> {
10+
pub async fn update_thread_status_ui(ctx: &Context, ticket: &TicketState) -> ModmailResult<()> {
911
let channel = ChannelId::new(ticket.channel_id as u64);
1012

1113
let color = match ticket.last_message_by {
@@ -47,14 +49,28 @@ pub async fn update_thread_status(ctx: &Context, ticket: &TicketState) -> Modmai
4749

4850
name.push_str(&format!("・{}", time_str));
4951

50-
tokio::spawn({
51-
let ctx = ctx.clone();
52-
async move {
53-
let _ = channel
54-
.edit(&ctx.http, EditChannel::new().name(&name))
55-
.await;
52+
let result = timeout(
53+
Duration::from_secs(2),
54+
channel.edit(&ctx.http, EditChannel::new().name(&name))
55+
).await;
56+
57+
match result {
58+
Ok(Ok(_)) => Ok(()),
59+
60+
Ok(Err(e)) => {
61+
eprintln!(
62+
"Failed to edit channel {}: {:?}",
63+
ticket.channel_id, e
64+
);
65+
Err(e.into())
5666
}
57-
});
5867

59-
Ok(())
68+
Err(_) => {
69+
eprintln!(
70+
"Timeout editing channel {} (skipping)",
71+
ticket.channel_id
72+
);
73+
Ok(())
74+
}
75+
}
6076
}

rustmail/src/utils/thread/send_to_thread.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::types::TicketAuthor;
77
use chrono::Utc;
88
use serenity::all::{ChannelId, Context, CreateAttachment, GuildId, Message, UserId};
99
use std::collections::HashMap;
10+
use crate::modules::update_thread_status_ui;
1011

1112
fn extract_message_content_with_media(msg: &Message) -> (String, Vec<String>) {
1213
let content = msg.content.clone();
@@ -137,7 +138,8 @@ pub async fn send_to_thread(
137138

138139
ticket_status.last_message_by = TicketAuthor::User;
139140
ticket_status.last_message_at = Utc::now().timestamp();
140-
update_thread_status(&thread_id.clone(), &ticket_status, &pool.clone()).await?;
141+
update_thread_status_db(&thread_id.clone(), &ticket_status, &pool.clone()).await?;
142+
update_thread_status_ui(&ctx, &ticket_status).await?;
141143

142144
let builder = MessageBuilder::begin_user_incoming(ctx, config, thread_id.clone(), msg)
143145
.to_thread(channel_id)

0 commit comments

Comments
 (0)