Added posting loop on main thread

This commit is contained in:
Emile 2021-06-25 20:48:06 +01:00
parent 7e61db4f3c
commit c96ff09128
2 changed files with 40 additions and 5 deletions

View File

@ -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
}
}

View File

@ -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<dyn Error>> {
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"),
}
}
}