mirror of
https://github.com/mii443/rs-docker-bot.git
synced 2025-08-22 16:15:40 +00:00
fix timeout and log sending
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
/target
|
/target
|
||||||
/code
|
/code
|
||||||
config.yaml
|
config.yaml
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
*.swp
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
io::Write,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
@ -9,11 +10,11 @@ use regex::Regex;
|
|||||||
use serenity::{
|
use serenity::{
|
||||||
async_trait,
|
async_trait,
|
||||||
builder::{
|
builder::{
|
||||||
CreateApplicationCommand, CreateApplicationCommandOption, CreateInteractionResponse,
|
CreateApplicationCommand, CreateApplicationCommandOption, CreateAttachment,
|
||||||
CreateInteractionResponseMessage, EditMessage,
|
CreateInteractionResponse, CreateInteractionResponseMessage, EditMessage,
|
||||||
},
|
},
|
||||||
model::prelude::{
|
model::prelude::{
|
||||||
command::{CommandOptionType, CommandType, Command},
|
command::{Command, CommandOptionType, CommandType},
|
||||||
Interaction, Message, Ready,
|
Interaction, Message, Ready,
|
||||||
},
|
},
|
||||||
prelude::{Context, EventHandler},
|
prelude::{Context, EventHandler},
|
||||||
@ -40,7 +41,10 @@ impl EventHandler for Handler {
|
|||||||
|
|
||||||
let config = {
|
let config = {
|
||||||
let data_read = context.data.read().await;
|
let data_read = context.data.read().await;
|
||||||
data_read.get::<ConfigStorage>().expect("Cannot get ConfigStorage.").clone()
|
data_read
|
||||||
|
.get::<ConfigStorage>()
|
||||||
|
.expect("Cannot get ConfigStorage.")
|
||||||
|
.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let language = {
|
let language = {
|
||||||
@ -50,7 +54,10 @@ impl EventHandler for Handler {
|
|||||||
|
|
||||||
if let Some(language) = language {
|
if let Some(language) = language {
|
||||||
let mut message = message
|
let mut message = message
|
||||||
.reply(&context.http, format!("Creating {:?} container.", language.name))
|
.reply(
|
||||||
|
&context.http,
|
||||||
|
format!("Creating {} container.", language.name),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -67,10 +74,14 @@ impl EventHandler for Handler {
|
|||||||
|
|
||||||
container.upload_file(code, file_name.clone()).await;
|
container.upload_file(code, file_name.clone()).await;
|
||||||
|
|
||||||
|
let compile_buf = Arc::new(Mutex::new(String::default()));
|
||||||
|
let b = Arc::clone(&compile_buf);
|
||||||
|
|
||||||
if let Some((compile_handle, compile_rx)) = container.compile().await {
|
if let Some((compile_handle, compile_rx)) = container.compile().await {
|
||||||
let rx_handle = tokio::spawn(async move {
|
let rx_handle = tokio::spawn(async move {
|
||||||
while let Ok(Some(msg)) = compile_rx.recv() {
|
while let Ok(Some(msg)) = compile_rx.recv() {
|
||||||
print!("{}", msg);
|
print!("{}", msg);
|
||||||
|
*b.lock().unwrap() += &msg.to_string();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -88,23 +99,80 @@ impl EventHandler for Handler {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let timeout = Arc::new(Mutex::new(false));
|
||||||
|
let t = Arc::clone(&timeout);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
sleep_until(Instant::now() + Duration::from_secs(10)).await;
|
sleep_until(Instant::now() + Duration::from_secs(10)).await;
|
||||||
end_tx.send(()).unwrap();
|
end_tx.send(()).unwrap();
|
||||||
|
*t.lock().unwrap() = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
let (_, _) = tokio::join!(run_handle, rx_handle);
|
let (_, _) = tokio::join!(run_handle, rx_handle);
|
||||||
|
|
||||||
message
|
let mut edit_message = EditMessage::new();
|
||||||
.edit(
|
let mut content = if *timeout.lock().unwrap() {
|
||||||
&context.http,
|
"Timeout".to_string()
|
||||||
EditMessage::new().content(format!(
|
} else if compile_buf.lock().unwrap().is_empty() {
|
||||||
"Result\n```{}\n```",
|
format!(
|
||||||
buf.lock().unwrap().replace("@", "\\@")
|
"Result\n```\n{}\n```",
|
||||||
)),
|
buf.lock().unwrap().replace("@", "\\@")
|
||||||
)
|
)
|
||||||
.await
|
} else {
|
||||||
.unwrap();
|
format!(
|
||||||
|
"Result\nCompilation log\n```\n{}\n```\nExecution log\n```{}\n```",
|
||||||
|
compile_buf.lock().unwrap().replace("@", "\\@"),
|
||||||
|
buf.lock().unwrap().replace("@", "\\@")
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
if content.len() >= 1000 {
|
||||||
|
content = "Result log out of length.".to_string();
|
||||||
|
{
|
||||||
|
let mut result_log =
|
||||||
|
std::fs::File::create(format!("./log/result-{}.txt", container.name))
|
||||||
|
.unwrap();
|
||||||
|
result_log
|
||||||
|
.write_all(buf.lock().unwrap().as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
result_log.flush().unwrap();
|
||||||
|
}
|
||||||
|
let result_log =
|
||||||
|
tokio::fs::File::open(format!("./log/result-{}.txt", container.name))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
edit_message = edit_message.attachment(
|
||||||
|
CreateAttachment::file(&result_log, "result_log.txt")
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if !((*compile_buf.lock().unwrap()).is_empty()) {
|
||||||
|
{
|
||||||
|
let mut compile_log = std::fs::File::create(format!(
|
||||||
|
"./log/compile-{}.txt",
|
||||||
|
container.name
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
compile_log
|
||||||
|
.write_all(compile_buf.lock().unwrap().as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
compile_log.flush().unwrap();
|
||||||
|
}
|
||||||
|
let result_log =
|
||||||
|
tokio::fs::File::open(format!("./log/compile-{}.txt", container.name))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
edit_message = edit_message.attachment(
|
||||||
|
CreateAttachment::file(&result_log, "compile_log.txt")
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
edit_message = edit_message.content(content);
|
||||||
|
|
||||||
|
message.edit(&context.http, edit_message).await.unwrap();
|
||||||
|
|
||||||
container.stop().await;
|
container.stop().await;
|
||||||
}
|
}
|
||||||
@ -137,6 +205,8 @@ impl EventHandler for Handler {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if command_interaction.data.options()[0].name == "help" {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -157,6 +227,8 @@ impl EventHandler for Handler {
|
|||||||
"docker help command",
|
"docker help command",
|
||||||
));
|
));
|
||||||
|
|
||||||
Command::create_global_application_command(&context.http, docker_command).await.unwrap();
|
Command::create_global_application_command(&context.http, docker_command)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user