From c96ff09128605b442d71a7aebae5c4fb779c0e38 Mon Sep 17 00:00:00 2001 From: Emile Date: Fri, 25 Jun 2021 20:48:06 +0100 Subject: [PATCH] Added posting loop on main thread --- src/config.rs | 28 +++++++++++++++++++++++++++- src/main.rs | 17 +++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 024ad3e..214d4bb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,23 @@ pub struct Config { top_k: String, gpt_code_path: String, fediverse_base_url: String, + interval_seconds: MinMax, +} + +#[derive(Serialize, Deserialize)] +pub struct MinMax { + min: u64, + max: u64, +} + +impl MinMax { + pub fn min(&self) -> u64 { + self.min + } + + pub fn max(&self) -> u64 { + self.max + } } impl Config { @@ -19,8 +36,12 @@ impl Config { model_name: String::from("117M"), temperature: String::from("1"), top_k: String::from("40"), - gpt_code_path: String::from("."), + gpt_code_path: String::from("./gpt/"), fediverse_base_url: String::from("https://lain.com"), + interval_seconds: MinMax { + min: 60 * 30, + max: 60 * 90, + }, } } @@ -72,4 +93,9 @@ impl Config { pub fn fediverse_base_url(&self) -> String { self.fediverse_base_url.clone() } + + /// Get a reference to the config's inverval seconds. + pub fn interval_seconds(&self) -> &MinMax { + &self.interval_seconds + } } diff --git a/src/main.rs b/src/main.rs index 20a2688..e08cf12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use std::{error::Error, process}; +use std::{error::Error, process, thread, time::Duration}; + +use rand::Rng; mod bot; mod config; @@ -43,8 +45,15 @@ fn main() -> Result<(), Box> { let gen = generator::Generator::new(gpt_model); let mut bot = bot::IzzilisBot::new(gen, publisher); - match bot.publish() { - Some(err) => Err(err), - None => Ok(()), + let cfg_interval = cfg.interval_seconds(); + loop { + let wait_seconds = rand::thread_rng().gen_range(cfg_interval.min()..cfg_interval.max()); + let wait_time = Duration::from_secs(wait_seconds); + println!("Next post is in {} seconds", wait_seconds); + thread::sleep(wait_time); + match bot.publish() { + Some(err) => println!("Got error from publish: {}; continuing", err), + None => println!("publish() call successful"), + } } }