Add an action to update jails automatically.
I probably will only use this for end-user systems like a laptop but not for servers so I can pick when I need to reboot my servers.
This commit is contained in:
@@ -6,6 +6,7 @@ use rusqlite::params;
|
||||
use rusqlite::Connection;
|
||||
|
||||
use super::DbGlobalAction;
|
||||
use super::DbJailAction;
|
||||
use super::DbLocalAction;
|
||||
|
||||
static DB_INIT_QUERY: &str = include_str!("../../migrations/01_init.sql");
|
||||
@@ -43,6 +44,27 @@ impl DbHandle {
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
pub(crate) fn get_pending_jail_actions(
|
||||
&mut self,
|
||||
) -> Result<Vec<DbJailAction>, Box<dyn std::error::Error>> {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)?
|
||||
.as_secs();
|
||||
let mut stmt = self
|
||||
.conn
|
||||
.prepare("SELECT name, jail_name, next_run FROM jail_action WHERE next_run <= $1")?;
|
||||
let rows = stmt
|
||||
.query_map(params![now], |row| {
|
||||
Ok(DbJailAction {
|
||||
name: row.get(0)?,
|
||||
jail_name: row.get(1)?,
|
||||
next_run: row.get(2)?,
|
||||
})
|
||||
})?
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
pub(crate) fn get_pending_local_actions(
|
||||
&mut self,
|
||||
) -> Result<Vec<DbLocalAction>, Box<dyn std::error::Error>> {
|
||||
@@ -71,7 +93,7 @@ impl DbHandle {
|
||||
) -> Result<Duration, Box<dyn std::error::Error>> {
|
||||
let now = SystemTime::now();
|
||||
let mut stmt = self.conn.prepare(
|
||||
"WITH next_runs AS (SELECT next_run FROM global_action UNION SELECT next_run FROM local_action) SELECT next_run FROM next_runs ORDER BY next_run ASC",
|
||||
"WITH next_runs AS (SELECT next_run FROM global_action UNION SELECT next_run FROM jail_action UNION SELECT next_run FROM local_action) SELECT min(next_run) FROM next_runs;",
|
||||
)?;
|
||||
let rows = stmt
|
||||
.query_map(params![], |row| row.get(0))?
|
||||
|
||||
@@ -3,4 +3,5 @@ mod types;
|
||||
|
||||
pub(crate) use init::DbHandle;
|
||||
pub(crate) use types::DbGlobalAction;
|
||||
pub(crate) use types::DbJailAction;
|
||||
pub(crate) use types::DbLocalAction;
|
||||
|
||||
@@ -4,6 +4,13 @@ pub(crate) struct DbGlobalAction {
|
||||
pub(crate) next_run: i64,
|
||||
}
|
||||
|
||||
pub(crate) struct DbJailAction {
|
||||
pub(crate) name: String,
|
||||
pub(crate) jail_name: String,
|
||||
#[allow(dead_code)]
|
||||
pub(crate) next_run: i64,
|
||||
}
|
||||
|
||||
pub(crate) struct DbLocalAction {
|
||||
pub(crate) name: String,
|
||||
pub(crate) jail_name: String,
|
||||
|
||||
Reference in New Issue
Block a user