mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-03-25 06:54:14 -04:00
record boot uuid on open
This can be used to match up with eg `journalctl --list-boots` for debugging.
This commit is contained in:
parent
f86f03cf59
commit
f7aa71d2af
@ -39,6 +39,7 @@ use failure::{bail, format_err, Error, ResultExt};
|
|||||||
use fnv::{FnvHashMap, FnvHashSet};
|
use fnv::{FnvHashMap, FnvHashSet};
|
||||||
use hashlink::LinkedHashMap;
|
use hashlink::LinkedHashMap;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use log::warn;
|
||||||
use log::{error, info, trace};
|
use log::{error, info, trace};
|
||||||
use parking_lot::{Mutex, MutexGuard};
|
use parking_lot::{Mutex, MutexGuard};
|
||||||
use rusqlite::{named_params, params};
|
use rusqlite::{named_params, params};
|
||||||
@ -112,6 +113,12 @@ impl rusqlite::types::FromSql for FromSqlUuid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl rusqlite::types::ToSql for FromSqlUuid {
|
||||||
|
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
|
||||||
|
Ok(self.0.as_bytes()[..].into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct VideoIndex(Box<[u8]>);
|
struct VideoIndex(Box<[u8]>);
|
||||||
|
|
||||||
impl rusqlite::types::FromSql for VideoIndex {
|
impl rusqlite::types::FromSql for VideoIndex {
|
||||||
@ -2151,6 +2158,16 @@ pub fn get_schema_version(conn: &rusqlite::Connection) -> Result<Option<i32>, Er
|
|||||||
)?))
|
)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the UUID associated with the current system boot, if available.
|
||||||
|
fn get_boot_uuid() -> Result<Option<Uuid>, Error> {
|
||||||
|
if cfg!(target_os = "linux") {
|
||||||
|
let boot_id = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?;
|
||||||
|
Ok(Some(Uuid::parse_str(boot_id.trim_end())?))
|
||||||
|
} else {
|
||||||
|
Ok(None) // don't complain about lack of platform support; just return None.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks that the schema version in the given database is as expected.
|
/// Checks that the schema version in the given database is as expected.
|
||||||
pub(crate) fn check_schema_version(conn: &rusqlite::Connection) -> Result<(), Error> {
|
pub(crate) fn check_schema_version(conn: &rusqlite::Connection) -> Result<(), Error> {
|
||||||
let ver = get_schema_version(conn)?.ok_or_else(|| {
|
let ver = get_schema_version(conn)?.ok_or_else(|| {
|
||||||
@ -2229,15 +2246,19 @@ impl<C: Clocks + Clone> Database<C> {
|
|||||||
let open_monotonic = recording::Time::new(clocks.monotonic());
|
let open_monotonic = recording::Time::new(clocks.monotonic());
|
||||||
let open = if read_write {
|
let open = if read_write {
|
||||||
let real = recording::Time::new(clocks.realtime());
|
let real = recording::Time::new(clocks.realtime());
|
||||||
let mut stmt =
|
let mut stmt = conn
|
||||||
conn.prepare(" insert into open (uuid, start_time_90k) values (?, ?)")?;
|
.prepare(" insert into open (uuid, start_time_90k, boot_uuid) values (?, ?, ?)")?;
|
||||||
let uuid = Uuid::new_v4();
|
let open_uuid = FromSqlUuid(Uuid::new_v4());
|
||||||
let uuid_bytes = &uuid.as_bytes()[..];
|
let boot_uuid = match get_boot_uuid() {
|
||||||
stmt.execute(params![uuid_bytes, real.0])?;
|
Err(e) => {
|
||||||
Some(Open {
|
warn!("Unable to get boot uuid: {}", e);
|
||||||
id: conn.last_insert_rowid() as u32,
|
None
|
||||||
uuid,
|
}
|
||||||
})
|
Ok(id) => id.map(FromSqlUuid),
|
||||||
|
};
|
||||||
|
stmt.execute(params![open_uuid, real.0, boot_uuid])?;
|
||||||
|
let id = conn.last_insert_rowid() as u32;
|
||||||
|
Some(Open { id, uuid })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -50,7 +50,9 @@ create table open (
|
|||||||
|
|
||||||
-- How long the database was open. This is end_time_90k - start_time_90k if
|
-- How long the database was open. This is end_time_90k - start_time_90k if
|
||||||
-- there were no time steps or leap seconds during this time.
|
-- there were no time steps or leap seconds during this time.
|
||||||
duration_90k integer
|
duration_90k integer,
|
||||||
|
|
||||||
|
boot_uuid check (length(boot_uuid) = 16)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table sample_file_dir (
|
create table sample_file_dir (
|
||||||
|
@ -130,7 +130,7 @@ fn copy_streams(tx: &rusqlite::Transaction) -> Result<(), Error> {
|
|||||||
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
|
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
|
||||||
tx.execute_batch(
|
tx.execute_batch(
|
||||||
r#"
|
r#"
|
||||||
|
alter table open add boot_uuid check (length(boot_uuid) = 16);
|
||||||
alter table user add preferences text;
|
alter table user add preferences text;
|
||||||
alter table camera rename to old_camera;
|
alter table camera rename to old_camera;
|
||||||
alter table stream rename to old_stream;
|
alter table stream rename to old_stream;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user