-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds maintenance task to clean up unused and empty lists
- Loading branch information
Showing
15 changed files
with
208 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP INDEX IF EXISTS idx_list_is_empty; | ||
ALTER TABLE list DROP COLUMN is_empty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ALTER TABLE list ADD COLUMN is_empty BOOLEAN NOT NULL DEFAULT TRUE; | ||
UPDATE list SET is_empty = (items = '{}'); | ||
CREATE INDEX idx_list_is_empty ON list(is_empty); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
use std::time::Duration; | ||
|
||
use anyhow::{Context, Result}; | ||
use bot_na_lista::{db, telegram, MIGRATIONS}; | ||
use bot_na_lista::{db, maintenance, telegram, MIGRATIONS}; | ||
use diesel_migrations::{HarnessWithOutput, MigrationHarness}; | ||
use tokio::{spawn, time::sleep}; | ||
|
||
const ONE_DAY: Duration = Duration::from_secs(60 * 60 * 24); | ||
|
||
#[tokio::main(flavor = "multi_thread")] | ||
async fn main() -> Result<()> { | ||
pretty_env_logger::init(); | ||
log::info!("Starting Bot na Lista"); | ||
let pool = db::from_env().context("Could not get a database connection pool")?; | ||
let pool = db::pool_from_env().context("Could not get a database connection pool")?; | ||
let mut conn = pool.get().context("Could not get a database connection")?; | ||
let mut harness = HarnessWithOutput::write_to_stdout(&mut conn); | ||
harness | ||
.run_pending_migrations(MIGRATIONS) | ||
.map_err(|e| anyhow::anyhow!("Failed to run pending migrations: {}", e))?; | ||
spawn(async { | ||
loop { | ||
if let Err(e) = maintenance::clean_up() { | ||
log::error!("Error cleaning up: {:?}", e); | ||
} | ||
sleep(ONE_DAY).await; | ||
} | ||
}); | ||
telegram::run(pool).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use anyhow::Result; | ||
use chrono::{Duration, Utc}; | ||
use diesel::{delete, BoolExpressionMethods, ExpressionMethods, RunQueryDsl}; | ||
|
||
use crate::{db, schema::list}; | ||
|
||
pub fn clean_up() -> Result<()> { | ||
let mut conn = db::from_env()?; | ||
let one_year_ago = Utc::now() - Duration::days(366); // considering leap years | ||
delete(list::table) | ||
.filter( | ||
list::dsl::updated_at | ||
.lt(one_year_ago) | ||
.or(list::dsl::is_empty.eq(true)), | ||
) | ||
.execute(&mut conn)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.