sausage core part 1: a process per call, routing, tree, stop and kill, locks, journal, boot
Checkpoint. Removes the old host. A quality rework follows (modules, journal writer thread, hash cache, finer tests).
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
//! SQLite journal entries and filters.
|
||||
use rusqlite::{Connection, params};
|
||||
use serde_json::{Value, json};
|
||||
use std::{collections::HashSet, path::Path, sync::Mutex};
|
||||
pub struct Journal {
|
||||
db: Mutex<Connection>,
|
||||
warned: Mutex<HashSet<String>>,
|
||||
}
|
||||
impl Journal {
|
||||
pub fn open(path: &Path) -> anyhow::Result<Self> {
|
||||
std::fs::create_dir_all(path.parent().unwrap())?;
|
||||
let db = Connection::open(path)?;
|
||||
db.execute_batch("CREATE TABLE IF NOT EXISTS journal (id TEXT PRIMARY KEY, pid TEXT, plugin TEXT, stream TEXT, line TEXT)")?;
|
||||
Ok(Self {
|
||||
db: Mutex::new(db),
|
||||
warned: Mutex::new(HashSet::new()),
|
||||
})
|
||||
}
|
||||
pub fn write(&self, pid: &str, plugin: &str, stream: &str, line: &str) {
|
||||
self.db
|
||||
.lock()
|
||||
.unwrap()
|
||||
.execute(
|
||||
"INSERT INTO journal VALUES (?1,?2,?3,?4,?5)",
|
||||
params![uuid::Uuid::now_v7().to_string(), pid, plugin, stream, line],
|
||||
)
|
||||
.expect("write journal");
|
||||
}
|
||||
pub fn warning(&self, pid: &str, plugin: &str) {
|
||||
if self.warned.lock().unwrap().insert(pid.into()) {
|
||||
self.write(pid, plugin, "core", "warning: discarded stdout");
|
||||
}
|
||||
}
|
||||
pub fn query(&self, p: &Value) -> Value {
|
||||
let db = self.db.lock().unwrap();
|
||||
let mut stmt=db.prepare("SELECT id,pid,plugin,stream,line FROM journal WHERE (?1 IS NULL OR plugin=?1) AND (?2 IS NULL OR pid=?2) AND (?3 IS NULL OR id>=?3) AND (?4 IS NULL OR id<=?4) AND (?5 IS NULL OR instr(line,?5)>0) ORDER BY id DESC LIMIT ?6").unwrap();
|
||||
let rows=stmt.query_map(params![p["plugin"].as_str(),p["pid"].as_str(),p["since"].as_str(),p["until"].as_str(),p["grep"].as_str(),p["limit"].as_i64().unwrap_or(100)],|r|Ok(json!({"id":r.get::<_,String>(0)?,"pid":r.get::<_,String>(1)?,"plugin":r.get::<_,String>(2)?,"stream":r.get::<_,String>(3)?,"line":r.get::<_,String>(4)?}))).unwrap();
|
||||
let mut entries: Vec<_> = rows.map(Result::unwrap).collect();
|
||||
entries.reverse();
|
||||
json!(entries)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user