Add ports tree updating.
This commit is contained in:
parent
f1db62190a
commit
613065de85
4
src/action/mod.rs
Normal file
4
src/action/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
mod ports_tree;
|
||||
|
||||
pub(crate) use ports_tree::update_ports_tree;
|
||||
pub(crate) use ports_tree::ACTION_UPDATE_PORTS_TREE;
|
25
src/action/ports_tree.rs
Normal file
25
src/action/ports_tree.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use crate::db::DbHandle;
|
||||
|
||||
pub(crate) const ACTION_UPDATE_PORTS_TREE: &str = "update_ports_tree";
|
||||
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()?;
|
||||
let next_run = (SystemTime::now() + Duration::from_secs(ACTION_UPDATE_PORTS_TREE_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_UPDATE_PORTS_TREE),
|
||||
)?;
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
@ -11,7 +11,7 @@ use super::DbLocalAction;
|
||||
static DB_INIT_QUERY: &str = include_str!("../../migrations/01_init.sql");
|
||||
|
||||
pub(crate) struct DbHandle {
|
||||
conn: Connection,
|
||||
pub(crate) conn: Connection,
|
||||
}
|
||||
|
||||
impl DbHandle {
|
||||
|
16
src/main.rs
16
src/main.rs
@ -1,5 +1,8 @@
|
||||
use std::{thread, time};
|
||||
|
||||
use crate::action::update_ports_tree;
|
||||
|
||||
mod action;
|
||||
mod db;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -9,6 +12,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut db_conn: db::DbHandle = db::DbHandle::new(db_path)?;
|
||||
|
||||
loop {
|
||||
for pending_global_action in db_conn.get_pending_global_actions()? {
|
||||
match pending_global_action.name.as_str() {
|
||||
ACTION_UPDATE_PORTS_TREE => {
|
||||
update_ports_tree(&mut db_conn)?;
|
||||
}
|
||||
_ => {
|
||||
panic!("Unknown global action: {}", pending_global_action.name);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let pending_local_actions = db_conn.get_pending_local_actions()?;
|
||||
|
||||
let time_until_next_action = db_conn.get_time_until_next_action()?;
|
||||
let sleep_duration = std::cmp::min(
|
||||
time::Duration::from_secs(60),
|
||||
|
Loading…
x
Reference in New Issue
Block a user