mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-20 01:08:55 +00:00
Various rewrite of the announcer
This commit is contained in:
parent
822f07954f
commit
67a7eb9aec
4 changed files with 31 additions and 25 deletions
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -1698,13 +1698,13 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serenity"
|
name = "serenity"
|
||||||
version = "0.11.6"
|
version = "0.11.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d007dc45584ecc47e791f2a9a7cf17bf98ac386728106f111159c846d624be3f"
|
checksum = "7a7a89cef23483fc9d4caf2df41e6d3928e18aada84c56abd237439d929622c6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"async-tungstenite",
|
"async-tungstenite",
|
||||||
"base64 0.13.1",
|
"base64 0.21.4",
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"bytes",
|
"bytes",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
|
|
|
@ -77,9 +77,10 @@ impl youmubot_prelude::Announcer for Announcer {
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
println!("scanning {} done", osu_user.id);
|
|
||||||
osu_user.last_update = now;
|
osu_user.last_update = now;
|
||||||
osu_user.pp = v.try_into().unwrap();
|
osu_user.pp = v.try_into().unwrap();
|
||||||
|
let id = osu_user.id;
|
||||||
|
println!("scanning {} done", id);
|
||||||
ctx.data
|
ctx.data
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
|
@ -88,6 +89,7 @@ impl youmubot_prelude::Announcer for Announcer {
|
||||||
.save(osu_user)
|
.save(osu_user)
|
||||||
.await
|
.await
|
||||||
.pls_ok();
|
.pls_ok();
|
||||||
|
println!("updating {} done", id);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("osu: Cannot update {}: {}", osu_user.id, e);
|
eprintln!("osu: Cannot update {}: {}", osu_user.id, e);
|
||||||
|
|
|
@ -19,6 +19,7 @@ use serenity::{
|
||||||
CacheAndHttp,
|
CacheAndHttp,
|
||||||
};
|
};
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
use tokio::time::{interval, MissedTickBehavior};
|
||||||
use youmubot_db::DB;
|
use youmubot_db::DB;
|
||||||
|
|
||||||
/// A list of assigned channels for an announcer.
|
/// A list of assigned channels for an announcer.
|
||||||
|
@ -149,30 +150,33 @@ impl AnnouncerHandler {
|
||||||
|
|
||||||
/// Start the AnnouncerHandler, looping forever.
|
/// Start the AnnouncerHandler, looping forever.
|
||||||
///
|
///
|
||||||
/// It will run all the announcers in sequence every *cooldown* seconds.
|
/// It will run all the announcers every *cooldown* seconds.
|
||||||
pub async fn scan(self, cooldown: std::time::Duration) {
|
pub async fn scan(self, cooldown: std::time::Duration) {
|
||||||
// First we store all the keys inside the database.
|
// First we store all the keys inside the database.
|
||||||
let keys = self.announcers.keys().cloned().collect::<Vec<_>>();
|
let keys = self.announcers.keys().cloned().collect::<Vec<_>>();
|
||||||
self.data.write().await.insert::<Self>(keys.clone());
|
self.data.write().await.insert::<Self>(keys.clone());
|
||||||
loop {
|
|
||||||
eprintln!("{}: announcer started scanning", chrono::Utc::now());
|
|
||||||
let after = tokio::time::sleep_until(tokio::time::Instant::now() + cooldown);
|
|
||||||
join_all(self.announcers.iter().map(|(key, announcer)| {
|
join_all(self.announcers.iter().map(|(key, announcer)| {
|
||||||
|
let data = self.data.clone();
|
||||||
|
let cache = self.cache_http.clone();
|
||||||
|
let mut looper = interval(cooldown);
|
||||||
|
looper.set_missed_tick_behavior(MissedTickBehavior::Delay);
|
||||||
|
async move {
|
||||||
|
loop {
|
||||||
eprintln!(" - scanning key `{}`", key);
|
eprintln!(" - scanning key `{}`", key);
|
||||||
Self::announce(self.data.clone(), self.cache_http.clone(), key, announcer).map(
|
Self::announce(data.clone(), cache.clone(), key, announcer)
|
||||||
move |v| {
|
.map(move |v| {
|
||||||
if let Err(e) = v {
|
if let Err(e) = v {
|
||||||
eprintln!(" - key `{}`: {:?}", *key, e)
|
eprintln!(" - key `{}`: {:?}", *key, e)
|
||||||
} else {
|
} else {
|
||||||
eprintln!(" - key `{}`: complete", *key)
|
eprintln!(" - key `{}`: complete", *key)
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
)
|
.await;
|
||||||
|
looper.tick().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
.await;
|
.await;
|
||||||
eprintln!("{}: announcer finished scanning", chrono::Utc::now());
|
|
||||||
after.await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -148,7 +148,7 @@ async fn main() {
|
||||||
#[cfg(feature = "codeforces")]
|
#[cfg(feature = "codeforces")]
|
||||||
println!("codeforces enabled.");
|
println!("codeforces enabled.");
|
||||||
|
|
||||||
tokio::spawn(announcers.scan(std::time::Duration::from_secs(120)));
|
tokio::spawn(announcers.scan(std::time::Duration::from_secs(300)));
|
||||||
|
|
||||||
println!("Starting...");
|
println!("Starting...");
|
||||||
if let Err(v) = client.start().await {
|
if let Err(v) = client.start().await {
|
||||||
|
|
Loading…
Add table
Reference in a new issue