This commit is contained in:
jude 2024-10-13 19:01:37 +01:00
parent 6340f5a20f
commit eb5c851d97
4 changed files with 226 additions and 158 deletions

View File

@ -59,7 +59,6 @@ assets = [
["reminder-dashboard/dist/index.html", "lib/reminder-rs/static/index.html", "644"],
["conf/default.env", "etc/reminder-rs/config.env", "600"],
["conf/Rocket.toml", "etc/reminder-rs/Rocket.toml", "600"],
["bin/reminder-rs-clean-old", "usr/bin/reminder-rs-clean-old", "755"],
# ["nginx/reminder-rs", "etc/nginx/sites-available/reminder-rs", "755"]
]
conf-files = [

View File

@ -29,7 +29,6 @@ use std::{
};
use chrono_tz::Tz;
use clap::Subcommand;
use log::warn;
use poise::serenity_prelude::{
model::{
@ -121,7 +120,51 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
let _ = dotenv::dotenv();
}
let discord_token = env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment");
let args = env::args().collect::<Vec<_>>();
let cmd_word = args.last().map(|w| w.as_str());
let database =
Pool::connect(&env::var("DATABASE_URL").expect("No database URL provided")).await.unwrap();
match cmd_word {
Some("clean") => {
let sent_clean_age = env::var("SENT_CLEAN_AGE")?;
if sent_clean_age.is_empty() {
panic!("No SENT_CLEAN_AGE")
}
sqlx::query!(
"
DELETE FROM reminders
WHERE `utc_time` < NOW() - INTERVAL ? DAY
AND status != 'pending'
ORDER BY `utc_time`
LIMIT 1000
",
sent_clean_age
)
.execute(&database)
.await?;
let total_clean_age = env::var("TOTAL_CLEAN_AGE");
if let Ok(total_clean_age) = total_clean_age {
sqlx::query!(
"
DELETE FROM reminders
WHERE `utc_time` < NOW() - INTERVAL ? DAY
ORDER BY `utc_time`
LIMIT 1000
",
total_clean_age
)
.execute(&database)
.await?;
}
Ok(())
}
_ => {
let discord_token =
env::var("DISCORD_TOKEN").expect("Missing DISCORD_TOKEN from environment");
let options = poise::FrameworkOptions {
commands: vec![
@ -175,11 +218,17 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
poise::Command {
subcommands: vec![
poise::Command {
subcommands: vec![todo::guild::add::add(), todo::guild::view::view()],
subcommands: vec![
todo::guild::add::add(),
todo::guild::view::view(),
],
..todo::guild::guild()
},
poise::Command {
subcommands: vec![todo::channel::add::add(), todo::channel::view::view()],
subcommands: vec![
todo::channel::add::add(),
todo::channel::view::view(),
],
..todo::channel::channel()
},
poise::Command {
@ -213,9 +262,6 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
// Start metrics
init_metrics();
let database =
Pool::connect(&env::var("DATABASE_URL").expect("No database URL provided")).await.unwrap();
sqlx::migrate!().run(&database).await?;
let popular_timezones = sqlx::query!(
@ -238,7 +284,8 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
let framework = poise::Framework::builder()
.setup(move |ctx, _bot, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
poise::builtins::register_globally(ctx, &framework.options().commands)
.await?;
let kill_tx = tx.clone();
let kill_recv = tx.subscribe();
@ -291,4 +338,6 @@ async fn _main(tx: Sender<()>) -> Result<(), Box<dyn StdError + Send + Sync>> {
client.start_autosharded().await?;
Ok(())
}
}
}

View File

@ -0,0 +1,11 @@
[Unit]
Description=Clean old data from Reminder Bot
[Service]
User=reminder
Type=simple
ExecStart=/usr/bin/reminder-rs clean
WorkingDirectory=/etc/reminder-rs
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,9 @@
[Unit]
Description=Clean reminder data twice daily
[Timer]
OnCalendar=*-*-* 0/8
Unit=reminder-rs-clean-old.service
[Install]
WantedBy=timers.target