Add global cleanup.

main
Tom Alexander 2 weeks ago
parent 613065de85
commit e15b0fd95e
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -0,0 +1,38 @@
use std::process::Command;
use std::time::Duration;
use std::time::SystemTime;
use crate::db::DbHandle;
pub(crate) const ACTION_CLEANUP: &str = "cleanup";
const ACTION_CLEANUP_INTERVAL: u64 = 1209600;
const ACTION_CLEANUP_LOG_RETENTION_DAYS: u64 = 30;
pub(crate) fn cleanup(db_conn: &mut DbHandle) -> Result<(), Box<dyn std::error::Error>> {
println!("Performing global cleanup.");
Command::new("poudriere")
.arg("distclean")
.arg("-v")
.arg("-y")
.arg("-a")
.status()?
.exit_ok()?;
Command::new("poudriere")
.arg("logclean")
.arg("-v")
.arg("-y")
.arg(ACTION_CLEANUP_LOG_RETENTION_DAYS.to_string())
.status()?
.exit_ok()?;
let next_run = (SystemTime::now() + Duration::from_secs(ACTION_CLEANUP_INTERVAL))
.duration_since(SystemTime::UNIX_EPOCH)?
.as_secs();
let tx = db_conn.conn.transaction()?;
tx.execute(
"UPDATE global_action SET next_run=$1 WHERE name=$2",
(next_run, ACTION_CLEANUP),
)?;
tx.commit()?;
Ok(())
}

@ -1,4 +1,5 @@
mod cleanup;
mod ports_tree;
pub(crate) use cleanup::cleanup;
pub(crate) use ports_tree::update_ports_tree;
pub(crate) use ports_tree::ACTION_UPDATE_PORTS_TREE;

@ -9,8 +9,8 @@ const ACTION_UPDATE_PORTS_TREE_INTERVAL: u64 = 86400;
pub(crate) fn update_ports_tree(db_conn: &mut DbHandle) -> Result<(), Box<dyn std::error::Error>> {
println!("Updating ports tree.");
Command::new("portshaker").arg("-U").status()?;
Command::new("portshaker").arg("-M").status()?;
Command::new("portshaker").arg("-U").status()?.exit_ok()?;
Command::new("portshaker").arg("-M").status()?.exit_ok()?;
let next_run = (SystemTime::now() + Duration::from_secs(ACTION_UPDATE_PORTS_TREE_INTERVAL))
.duration_since(SystemTime::UNIX_EPOCH)?
.as_secs();

@ -1,5 +1,7 @@
#![feature(exit_status_error)]
use std::{thread, time};
use crate::action::cleanup;
use crate::action::update_ports_tree;
mod action;
@ -17,6 +19,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
ACTION_UPDATE_PORTS_TREE => {
update_ports_tree(&mut db_conn)?;
}
ACTION_CLEANUP => {
cleanup(&mut db_conn)?;
}
_ => {
panic!("Unknown global action: {}", pending_global_action.name);
}

Loading…
Cancel
Save