Adapt module changes to main.rs
This commit is contained in:
parent
1db4cb19bd
commit
c355bc141f
1 changed files with 38 additions and 46 deletions
84
src/main.rs
84
src/main.rs
|
@ -1,11 +1,12 @@
|
|||
use chrono::{DateTime, Duration, Utc};
|
||||
use once_cell::sync::Lazy;
|
||||
use reqwest::{Client};
|
||||
use std::{collections::HashMap, vec};
|
||||
use std::{collections::HashMap};
|
||||
use std::fmt::Debug;
|
||||
use std::sync::{Arc};
|
||||
use log::{error, warn, info, LevelFilter};
|
||||
use tokio::sync::{RwLock};
|
||||
use strum_macros::Display;
|
||||
use systemd_journal_logger::{connected_to_journal, JournalLog};
|
||||
use tokio::time::sleep;
|
||||
use crate::config::Config;
|
||||
use crate::post_history::{SeriesHistory};
|
||||
|
@ -14,9 +15,29 @@ mod config;
|
|||
mod jnovel;
|
||||
mod bot;
|
||||
mod lemmy;
|
||||
mod tui;
|
||||
mod post_history;
|
||||
|
||||
pub (crate) fn write_error(err_msg: String) {
|
||||
match connected_to_journal() {
|
||||
true => error!("[ERROR] {err_msg}"),
|
||||
false => println!("[ERROR] {err_msg}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub (crate) fn write_warn(warn_msg: String) {
|
||||
match connected_to_journal() {
|
||||
true => warn!("[WARN] {warn_msg}"),
|
||||
false => println!("[WARN] {warn_msg}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub (crate) fn write_info(info_msg: String) {
|
||||
match connected_to_journal() {
|
||||
true => info!("[INFO] {info_msg}"),
|
||||
false => println!("[INFO] {info_msg}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
|
||||
Client::builder()
|
||||
.timeout(Duration::seconds(30).to_std().unwrap())
|
||||
|
@ -27,7 +48,6 @@ pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct SharedData {
|
||||
messages: Vec<Message>,
|
||||
config: Config,
|
||||
post_history: SeriesHistory,
|
||||
start: DateTime<Utc>,
|
||||
|
@ -36,69 +56,41 @@ pub(crate) struct SharedData {
|
|||
impl SharedData {
|
||||
pub(crate) fn new() -> Self {
|
||||
SharedData {
|
||||
messages: vec![],
|
||||
config: Config {
|
||||
instance: "".to_owned(),
|
||||
username: "".to_owned(),
|
||||
password: "".to_owned(),
|
||||
status_post_url: None,
|
||||
config_reload_seconds: 0,
|
||||
protected_communities: vec![],
|
||||
series: vec![],
|
||||
},
|
||||
config: Config::default(),
|
||||
post_history: SeriesHistory {
|
||||
series: HashMap::new(),
|
||||
},
|
||||
start: Utc::now(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_messages(&self, errors: bool, warnings: bool, infos: bool) -> Vec<Message> {
|
||||
self.messages.iter().filter(|msg| {
|
||||
match msg {
|
||||
Message::Error(_) => errors,
|
||||
Message::Warning(_) => warnings,
|
||||
Message::Info(_) => infos,
|
||||
}
|
||||
}).cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Display, PartialEq)]
|
||||
pub(crate) enum Message {
|
||||
Info(String),
|
||||
Warning(String),
|
||||
Error(String),
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub(crate) fn content(&self) -> String {
|
||||
match self {
|
||||
Message::Info(msg) => msg.clone(),
|
||||
Message::Warning(msg) => msg.clone(),
|
||||
Message::Error(msg) => msg.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let mut data = SharedData::new();
|
||||
JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error");
|
||||
log::set_max_level(LevelFilter::Info);
|
||||
|
||||
let mut data = SharedData::new();
|
||||
|
||||
loop {
|
||||
let write_data = Arc::new(RwLock::new(data.clone()));
|
||||
let read_data = write_data.clone();
|
||||
//let read_data = write_data.clone();
|
||||
let persistent_data = write_data.clone();
|
||||
|
||||
let tui_thread = tokio::spawn(async move { tui::run(read_data).await });
|
||||
let bot_thread = tokio::spawn(async move { bot::run(write_data).await });
|
||||
|
||||
let _ = bot_thread.await;
|
||||
tui_thread.abort();
|
||||
|
||||
data = persistent_data.read().await.clone();
|
||||
data.messages.push(Message::Error("Bot crashed due to unknown Error, restarting thread after wait...".to_owned()));
|
||||
|
||||
{
|
||||
let err_msg = "Bot crashed due to unknown Error, restarting thread after wait...";
|
||||
match connected_to_journal() {
|
||||
true => error!("[ERROR] {err_msg}"),
|
||||
false => println!("[ERROR] {err_msg}")
|
||||
}
|
||||
}
|
||||
|
||||
sleep(Duration::seconds(5).to_std().expect("Conversion should always work since static")).await;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue