Compare commits
4 Commits
v0.1.2
...
bb88e68f8f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb88e68f8f | ||
|
|
004f0eb900 | ||
|
|
22fa677d8a | ||
|
|
2faf8038fe |
1
emeline.json
Normal file
1
emeline.json
Normal file
File diff suppressed because one or more lines are too long
@@ -51,6 +51,8 @@ pub struct CompState {
|
|||||||
pub rank: DanceRank,
|
pub rank: DanceRank,
|
||||||
pub points: u16,
|
pub points: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct DanceInfo {
|
pub struct DanceInfo {
|
||||||
pub firstname: String,
|
pub firstname: String,
|
||||||
pub lastname: String,
|
pub lastname: String,
|
||||||
|
|||||||
46
src/main.rs
46
src/main.rs
@@ -13,8 +13,10 @@
|
|||||||
reason = "Disable this for most of the time, enable this for cleanup later"
|
reason = "Disable this for most of the time, enable this for cleanup later"
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
#![feature(never_type)]
|
||||||
|
|
||||||
use mwbot::{
|
use mwbot::{
|
||||||
Bot,
|
Bot, ConfigError,
|
||||||
generators::{Generator, SortDirection, categories::CategoryMemberSort},
|
generators::{Generator, SortDirection, categories::CategoryMemberSort},
|
||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@@ -46,30 +48,32 @@ pub fn app_signature() -> String {
|
|||||||
format!("{} [{}]", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
|
format!("{} [{}]", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
pub enum AppError {
|
||||||
|
#[error("Runtime error: {0}")]
|
||||||
|
RuntimeError(#[from] std::io::Error),
|
||||||
|
#[error("Bot initialization error: {0}")]
|
||||||
|
BotError(#[from] ConfigError),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), AppError> {
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
.with_level(true)
|
.with_level(true)
|
||||||
.with_max_level(tracing::Level::INFO)
|
.with_max_level(tracing::Level::INFO)
|
||||||
.init();
|
.init();
|
||||||
tracing::info!("Starting {}", app_signature());
|
tracing::info!("Starting {}", app_signature());
|
||||||
let rt = match tokio::runtime::Builder::new_current_thread()
|
|
||||||
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.build()
|
.build()?;
|
||||||
{
|
|
||||||
Ok(o) => o,
|
let bot = rt.block_on(Bot::from_path(Path::new("./mwbot.toml")))?;
|
||||||
Err(e) => {
|
|
||||||
tracing::error!("Could not start runtime: {e}");
|
#[allow(
|
||||||
return;
|
unreachable_code,
|
||||||
}
|
reason = "This is a false positive I think, I just want to loop infinitely on two futures"
|
||||||
};
|
)]
|
||||||
rt.block_on(async {
|
rt.block_on(async { futures::join!(watch_wanted(bot.clone()), updater::update_wsdc(bot)) });
|
||||||
let bot = match Bot::from_path(Path::new("./mwbot.toml")).await {
|
|
||||||
Ok(x) => x,
|
Ok(())
|
||||||
Err(e) => {
|
|
||||||
dbg!(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
futures::join!(watch_wanted(bot.clone()), updater::update_wsdc(bot));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,38 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use mwbot::Bot;
|
use mwbot::Bot;
|
||||||
use rand::seq::SliceRandom as _;
|
use rand::seq::SliceRandom as _;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
use crate::{watchdog::generate_page, wikiinfo::index_wsdc_ids};
|
use crate::{watchdog::generate_page, wikiinfo::index_wsdc_ids};
|
||||||
|
|
||||||
pub async fn update_wsdc(bot: Bot) -> ! {
|
pub async fn update_wsdc(bot: Bot) -> ! {
|
||||||
loop {
|
loop {
|
||||||
let mut l = index_wsdc_ids(&bot).await;
|
update_all_teachers(&bot).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates all teachers once
|
||||||
|
async fn update_all_teachers(bot: &Bot) {
|
||||||
|
let mut l = index_wsdc_ids(bot).await;
|
||||||
l.shuffle(&mut rand::rng());
|
l.shuffle(&mut rand::rng());
|
||||||
tracing::info!("We have to update {} pages", l.len());
|
tracing::info!("We have to update {} pages", l.len());
|
||||||
let wait_duration = Duration::from_secs(6 * 3600);
|
let wait_duration = Duration::from_hours(6);
|
||||||
|
|
||||||
for (index, page) in l {
|
for (index, page) in l {
|
||||||
tracing::info!("Next up: #{index}");
|
process_page(wait_duration, index, page).await;
|
||||||
tokio::time::sleep(wait_duration).await;
|
}
|
||||||
if generate_page(index, page).await {
|
tracing::info!("Updates all pages");
|
||||||
tracing::info!("Updated {index}");
|
}
|
||||||
} else {
|
|
||||||
tracing::error!("Error updating {index}");
|
#[tracing::instrument(skip_all, fields(index))]
|
||||||
}
|
async fn process_page(wait_duration: Duration, index: u32, page: mwbot::Page) {
|
||||||
|
tracing::info!("Next up");
|
||||||
|
sleep(wait_duration).await;
|
||||||
|
|
||||||
|
match generate_page(index, page).await {
|
||||||
|
Ok(()) => (),
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("Error updating: {err}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
117
src/watchdog.rs
117
src/watchdog.rs
@@ -1,61 +1,83 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::app_signature;
|
use crate::app_signature;
|
||||||
|
use crate::wikipage::InfoCompileError;
|
||||||
|
use crate::worldsdc::DanceInfoError;
|
||||||
use crate::{wikiinfo::wanted_ids, wikipage::page_from_info, worldsdc::fetch_wsdc_info};
|
use crate::{wikiinfo::wanted_ids, wikipage::page_from_info, worldsdc::fetch_wsdc_info};
|
||||||
use mwbot::Bot;
|
|
||||||
use mwbot::SaveOptions;
|
use mwbot::SaveOptions;
|
||||||
use tracing::Level;
|
use mwbot::{Bot, Page};
|
||||||
|
|
||||||
pub async fn watch_wanted(bot: Bot) -> ! {
|
pub struct Ticker {
|
||||||
let span = tracing::span!(Level::INFO, "wanted_watchdog");
|
count: usize,
|
||||||
let _enter = span.enter();
|
max: usize,
|
||||||
|
}
|
||||||
|
|
||||||
let mut ignored_ids = vec![];
|
impl Ticker {
|
||||||
let mut watch_count = 0;
|
pub const fn new(max: usize) -> Self {
|
||||||
loop {
|
Self { count: 0, max }
|
||||||
watch_count += 1;
|
}
|
||||||
if watch_count >= 120 {
|
|
||||||
watch_count = 0;
|
/// Returns `true` if the ticker has "ticked" (i.e., reached `max` and reset).
|
||||||
let failed_id_info = if ignored_ids.is_empty() {
|
pub const fn tick(&mut self) -> bool {
|
||||||
String::new()
|
self.count += 1;
|
||||||
|
if self.count >= self.max {
|
||||||
|
self.count = 0;
|
||||||
|
true
|
||||||
} else {
|
} else {
|
||||||
format!("[{} failed ids]", ignored_ids.len())
|
false
|
||||||
};
|
}
|
||||||
tracing::info!("Watchdog check{failed_id_info}...");
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Continuously monitors teacher IDs that do not have a corresponding teacher WSDC page, ignoring those that fail processing.
|
||||||
|
#[tracing::instrument(skip_all)]
|
||||||
|
pub async fn watch_wanted(bot: Bot) -> ! {
|
||||||
|
let mut ignored_ids = vec![];
|
||||||
|
let mut heartbeat_ticker = Ticker::new(120);
|
||||||
|
loop {
|
||||||
|
if heartbeat_ticker.tick() {
|
||||||
|
tracing::info!(failed_id_count = ignored_ids.len(), "Watchdog check...");
|
||||||
}
|
}
|
||||||
let wanted = wanted_ids(bot.clone()).await;
|
let wanted = wanted_ids(bot.clone()).await;
|
||||||
let mut new_ignored = vec![];
|
let new_ignored = update_wanted_ids(&wanted, &ignored_ids).await;
|
||||||
for (id, page) in wanted.into_iter().filter(|(x, _)| ignored_ids.contains(x)) {
|
|
||||||
if !generate_page(id, page).await {
|
|
||||||
new_ignored.push(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !new_ignored.is_empty() {
|
|
||||||
ignored_ids.extend(new_ignored);
|
ignored_ids.extend(new_ignored);
|
||||||
}
|
|
||||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
tokio::time::sleep(Duration::from_secs(30)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn generate_page(id: u32, page: mwbot::Page) -> bool {
|
async fn update_wanted_ids(wanted: &[(u32, Page)], ignored_ids: &[u32]) -> Vec<u32> {
|
||||||
tracing::info!("Generating page for {id}");
|
let mut new_ignored = vec![];
|
||||||
let info = match fetch_wsdc_info(id).await {
|
|
||||||
Ok(o) => o,
|
|
||||||
Err(e) => {
|
|
||||||
tracing::error!("Error fetching wsdc info for {id}: {e}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let code = match page_from_info(info) {
|
|
||||||
Ok(o) => o,
|
|
||||||
Err(e) => {
|
|
||||||
tracing::error!("Creating wikicode for {id}: {e}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
match page
|
for (id, page) in wanted.iter().filter(|(x, _)| ignored_ids.contains(x)) {
|
||||||
.save(
|
let span = tracing::info_span!("update", id);
|
||||||
|
let _enter = span.enter();
|
||||||
|
if let Err(e) = generate_page(*id, page.clone()).await {
|
||||||
|
tracing::error!("{e}");
|
||||||
|
new_ignored.push(*id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum GeneratePageError {
|
||||||
|
#[error("Error fetching WSDC info for {0}")]
|
||||||
|
Fetch(#[from] DanceInfoError),
|
||||||
|
#[error("Error creating wikicode for {0}")]
|
||||||
|
Wikicode(#[from] InfoCompileError),
|
||||||
|
#[error("Error saving page for {0}")]
|
||||||
|
Save(#[from] mwbot::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn generate_page(id: u32, page: mwbot::Page) -> Result<(), GeneratePageError> {
|
||||||
|
tracing::info!("Generating page for {id}");
|
||||||
|
let info = fetch_wsdc_info(id).await?;
|
||||||
|
|
||||||
|
let code = page_from_info(info)?;
|
||||||
|
|
||||||
|
page.save(
|
||||||
code,
|
code,
|
||||||
&SaveOptions::summary(&format!(
|
&SaveOptions::summary(&format!(
|
||||||
"Created WSDC info from worldsdc.com -- {}",
|
"Created WSDC info from worldsdc.com -- {}",
|
||||||
@@ -64,12 +86,7 @@ pub async fn generate_page(id: u32, page: mwbot::Page) -> bool {
|
|||||||
.mark_as_bot(true)
|
.mark_as_bot(true)
|
||||||
.mark_as_minor(false),
|
.mark_as_minor(false),
|
||||||
)
|
)
|
||||||
.await
|
.await?;
|
||||||
{
|
|
||||||
Ok(_) => true,
|
Ok(())
|
||||||
Err(e) => {
|
|
||||||
tracing::error!("Could not save page for {id}: {e}");
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,21 +2,29 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use reqwest::ClientBuilder;
|
use reqwest::ClientBuilder;
|
||||||
|
|
||||||
use crate::dance_info::{CompState, DanceInfo, DanceRank, DanceRole};
|
use crate::{
|
||||||
|
app_signature,
|
||||||
|
dance_info::{CompState, DanceInfo, DanceRank, DanceRole},
|
||||||
|
};
|
||||||
|
|
||||||
// mod caching;
|
// mod caching;
|
||||||
pub async fn fetch_wsdc_info(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
pub async fn fetch_wsdc_info(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
||||||
let client = ClientBuilder::new()
|
let client = ClientBuilder::new()
|
||||||
|
.user_agent(app_signature())
|
||||||
.build()
|
.build()
|
||||||
.map_err(DanceInfoError::ClientBuild)?;
|
.map_err(DanceInfoError::ClientBuild)?;
|
||||||
|
|
||||||
let mut params = HashMap::new();
|
let mut params = HashMap::new();
|
||||||
|
|
||||||
|
let url = if cfg!(test) {
|
||||||
|
// "https://o5grQU3Y.free.beeceptor.com/lookup2020/find"
|
||||||
|
"http://localhost:8000"
|
||||||
|
} else {
|
||||||
|
"https://points.worldsdc.com/lookup2020/find"
|
||||||
|
};
|
||||||
params.insert("num", id.to_string());
|
params.insert("num", id.to_string());
|
||||||
let request = client
|
let request = client
|
||||||
.request(
|
.request(reqwest::Method::POST, url)
|
||||||
reqwest::Method::POST,
|
|
||||||
"https://points.worldsdc.com/lookup2020/find",
|
|
||||||
)
|
|
||||||
.form(¶ms)
|
.form(¶ms)
|
||||||
.build()
|
.build()
|
||||||
.map_err(DanceInfoError::RequestBuild)?;
|
.map_err(DanceInfoError::RequestBuild)?;
|
||||||
@@ -29,6 +37,30 @@ pub async fn fetch_wsdc_info(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
|||||||
Ok(x.into())
|
Ok(x.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#![allow(clippy::unwrap_used, reason = "Allow unwrap in tests")]
|
||||||
|
use crate::worldsdc::fetch_wsdc_info;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore = "Only run when the mock api is setup"]
|
||||||
|
fn test_fetch_wsdc() {
|
||||||
|
let rt = match tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
{
|
||||||
|
Ok(o) => o,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Could not start runtime: {e}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let x = rt.block_on(fetch_wsdc_info(7));
|
||||||
|
dbg!(&x);
|
||||||
|
x.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum DanceInfoError {
|
pub enum DanceInfoError {
|
||||||
#[error("Failed to build client: {0}")]
|
#[error("Failed to build client: {0}")]
|
||||||
|
|||||||
Reference in New Issue
Block a user