thread::sleep blocking fix + better print system
This commit is contained in:
parent
19c41eff6b
commit
a17de20b2b
2 changed files with 26 additions and 20 deletions
|
@ -3,7 +3,6 @@ use std::{
|
|||
fs::{self, OpenOptions},
|
||||
io::Write,
|
||||
path::Path,
|
||||
thread::sleep,
|
||||
time,
|
||||
};
|
||||
|
||||
|
@ -17,6 +16,7 @@ use lemmy_db_schema::{
|
|||
ListingType,
|
||||
};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use tokio::time::sleep;
|
||||
use url::Url;
|
||||
|
||||
use crate::CLIENT;
|
||||
|
@ -161,7 +161,7 @@ impl Config {
|
|||
|
||||
post_queue.push((new_post, prev_data));
|
||||
}
|
||||
sleep(time::Duration::from_millis(100)); // Should prevent dos-ing J-Novel servers
|
||||
sleep(time::Duration::from_millis(100)).await; // Should prevent dos-ing J-Novel servers
|
||||
i += 1;
|
||||
}
|
||||
|
||||
|
|
42
src/main.rs
42
src/main.rs
|
@ -16,10 +16,11 @@ use std::{
|
|||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread::{self, sleep},
|
||||
time::{self, Duration},
|
||||
thread::{self},
|
||||
time::{self, Duration}, vec,
|
||||
};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::sleep;
|
||||
|
||||
mod config;
|
||||
|
||||
|
@ -40,6 +41,7 @@ struct Bot {
|
|||
community_ids: CommunitiesVector,
|
||||
auth: Sensitive<String>,
|
||||
start_time: NaiveDateTime,
|
||||
message_queue: Vec<String>,
|
||||
}
|
||||
|
||||
impl Bot {
|
||||
|
@ -51,6 +53,7 @@ impl Bot {
|
|||
community_ids: CommunitiesVector::new(),
|
||||
auth: Sensitive::new("".to_string()),
|
||||
start_time: Utc::now().naive_local(),
|
||||
message_queue: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,7 +80,7 @@ impl Bot {
|
|||
self.auth = jwt.clone();
|
||||
return Ok(jwt);
|
||||
} else {
|
||||
println!("Error Code: {:?}", res.status());
|
||||
self.message_queue.push(format!("Error Code: {:?}", res.status()));
|
||||
return Err(Box::new(res.error_for_status().unwrap_err()));
|
||||
}
|
||||
}
|
||||
|
@ -119,7 +122,7 @@ impl Bot {
|
|||
let _ = self.pin(remove_community_pin);
|
||||
}
|
||||
None => {
|
||||
println!("Unable to unpin old post, please do so manually");
|
||||
self.message_queue.push(format!("Unable to unpin old post, please do so manually"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -157,7 +160,7 @@ impl Bot {
|
|||
|
||||
match self.pin(remove_local_pin).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => println!("Error Unpinning Post: {:#?}", e),
|
||||
Err(e) => self.message_queue.push(format!("Error Unpinning Post: {:#?}", e)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -180,7 +183,7 @@ impl Bot {
|
|||
|
||||
match self.pin(pin_new_local).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => println!("Error Pinning Post: {:#?}", e),
|
||||
Err(e) => self.message_queue.push(format!("Error Pinning Post: {:#?}", e)),
|
||||
};
|
||||
|
||||
return Ok(());
|
||||
|
@ -201,32 +204,32 @@ impl Bot {
|
|||
|
||||
#[warn(unused_results)]
|
||||
pub(crate) async fn run_once(&mut self, prev_time: &mut NaiveDateTime) -> Result<(), Box<dyn Error>> {
|
||||
println!("{:#<1$}", "", 30);
|
||||
self.message_queue.push(format!("{:#<1$}", "", 30));
|
||||
self.start_time = Utc::now().naive_local();
|
||||
|
||||
if self.start_time - *prev_time > chrono::Duration::seconds(6) {
|
||||
// Prod should use hours, add command line switch later and read duration from config
|
||||
println!("Reloading Config");
|
||||
self.message_queue.push(format!("Reloading Config"));
|
||||
*prev_time = self.start_time;
|
||||
self.config.load();
|
||||
self.community_ids.load(&self.auth, &self.config.instance).await?;
|
||||
println!("Done!");
|
||||
self.message_queue.push(format!("Done!"));
|
||||
}
|
||||
|
||||
// Start the polling process
|
||||
// Get all feed URLs (use cache)
|
||||
println!("Checking Feeds");
|
||||
self.message_queue.push(format!("Checking Feeds"));
|
||||
let post_queue: Vec<(CreatePost, (Option<usize>, usize, String))> = self
|
||||
.config
|
||||
.check_feeds(&mut self.post_history, &self.community_ids, &self.auth).await?;
|
||||
println!("Done!");
|
||||
self.message_queue.push(format!("Done!"));
|
||||
|
||||
|
||||
let mut i = 0;
|
||||
while i < post_queue.len() {
|
||||
let (post, (prev_idx, feed_id, feed_title)) = &post_queue[i];
|
||||
|
||||
println!("Posting: {}", post.name);
|
||||
self.message_queue.push(format!("Posting: {}", post.name));
|
||||
|
||||
let post_data = self.post(post.clone()).await?;
|
||||
|
||||
|
@ -256,21 +259,21 @@ impl Bot {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
pub(crate) async fn idle(&self) {
|
||||
pub(crate) async fn idle(&mut self) {
|
||||
let mut sleep_duration = chrono::Duration::seconds(30);
|
||||
if Utc::now().naive_local() - self.start_time > sleep_duration {
|
||||
sleep_duration = chrono::Duration::seconds(60);
|
||||
}
|
||||
|
||||
while Utc::now().naive_local() - self.start_time < sleep_duration {
|
||||
sleep(time::Duration::from_secs(1));
|
||||
sleep(time::Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
match reqwest::get(
|
||||
"https://status.neshweb.net/api/push/7s1CjPPzrV?status=up&msg=OK&ping=",
|
||||
).await {
|
||||
Ok(_) => {}
|
||||
Err(err) => println!("{}", err),
|
||||
Err(err) => self.message_queue.push(format!("{}", err)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -312,7 +315,7 @@ async fn run_bot(bot: Arc<Mutex<Bot>>) {
|
|||
|
||||
loop {
|
||||
this.idle().await;
|
||||
println!("Debug A"); // DEBUG
|
||||
this.message_queue = vec![];
|
||||
match this.run_once(&mut old).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => panic!("Crashed due to Error: {:#?}", e),
|
||||
|
@ -326,7 +329,7 @@ async fn run_bot(bot: Arc<Mutex<Bot>>) {
|
|||
async fn print_info(shutdown: Arc<AtomicBool>, bot: Arc<Mutex<Bot>>) {
|
||||
while !shutdown.load(Ordering::Relaxed) {
|
||||
let bot: tokio::sync::MutexGuard<'_, Bot> = bot.lock().await;
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
|
||||
print!("\x1B[2J\x1B[1;1H");
|
||||
println!(
|
||||
|
@ -341,6 +344,9 @@ async fn print_info(shutdown: Arc<AtomicBool>, bot: Arc<Mutex<Bot>>) {
|
|||
print!("{} ", post.title);
|
||||
print!("{:<1$}| ", "", 60 - post.title.len());
|
||||
println!("{}", post.last_post_url);
|
||||
});
|
||||
bot.message_queue.iter().for_each(|message| {
|
||||
println!("{}", message);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -367,7 +373,7 @@ async fn main() {
|
|||
|
||||
println!("Bot crashed due to unknown Error, restarting thread after wait...");
|
||||
|
||||
sleep(Duration::from_secs(30));
|
||||
sleep(Duration::from_secs(30)).await;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue