Skip to content

Commit

Permalink
feat: set chat action as typing to wait task done
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed Jun 12, 2024
1 parent 03a0fc0 commit e2edff8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
teloxide = { version = "0.12.2", features = ["macros"] }
timeago = { version = "0.4.2", features = ["chrono"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "process", "sync"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "process", "sync", "time"] }
console = "0.15.8"
buildit-utils = { path = "../buildit-utils" }
jsonwebtoken = "9.2.0"
Expand Down
44 changes: 35 additions & 9 deletions server/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ use rand::prelude::SliceRandom;
use rand::thread_rng;
use reqwest::ClientBuilder;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, fmt::Display};
use tokio::time::sleep;
use std::{
borrow::Cow,
fmt::Display,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
}, time::Duration,
};
use teloxide::{
prelude::*,
types::{ChatAction, ParseMode},
Expand Down Expand Up @@ -299,10 +307,6 @@ async fn create_pipeline_from_pr(

#[tracing::instrument(skip(bot, msg, pool))]
pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> ResponseResult<()> {
bot.send_chat_action(msg.chat.id, ChatAction::Typing)
.send()
.instrument(tracing::info_span!("send_chat_action"))
.await?;
match cmd {
Command::Help => {
bot.send_message(msg.chat.id, Command::descriptions().to_string())
Expand Down Expand Up @@ -472,7 +476,7 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
}
};

match buildit_utils::github::open_pr(
let task = buildit_utils::github::open_pr(
app_private_key,
&token,
id,
Expand All @@ -484,15 +488,37 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
tags: tags.clone(),
archs: archs.clone(),
},
)
.await
{
);

let is_done = Arc::new(AtomicBool::new(false));
let is_done_shared = is_done.clone();
let bot_shared = bot.clone();
tokio::spawn(async move {
loop {
if is_done_shared.load(Ordering::Relaxed) {
break;
}

bot_shared
.send_chat_action(msg.chat.id, ChatAction::Typing)
.send()
.instrument(tracing::info_span!("send_chat_action"))
.await
.ok();

sleep(Duration::from_secs(5)).await;
}
});

match task.await {
Ok((_id, url)) => {
is_done.store(true, Ordering::Relaxed);
bot.send_message(msg.chat.id, format!("Successfully opened PR: {url}"))
.await?;
return Ok(());
}
Err(e) => {
is_done.store(true, Ordering::Relaxed);
bot.send_message(msg.chat.id, truncate(&format!("Failed to open pr: {e}")))
.await?;
return Ok(());
Expand Down

0 comments on commit e2edff8

Please sign in to comment.