Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uteke-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ dirs = "5"
toml = "0.8"
serde = { version = "1", features = ["derive"] }
clap_complete = "4"
ctrlc = "3"
tracing-appender = "0.2"
22 changes: 22 additions & 0 deletions crates/uteke-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Shell};
use config::Config;
use std::io::{self, Read};
use std::sync::atomic::{AtomicBool, Ordering};
use tracing::Level;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
use uteke_core::Uteke;

/// Global flag set by SIGINT handler.
static SHUTDOWN_REQUESTED: AtomicBool = AtomicBool::new(false);

// ── Config is in config.rs ─────────────────────────────────────────────────

// ── JSON output helpers ─────────────────────────────────────────────────────
Expand Down Expand Up @@ -526,6 +530,7 @@ fn main() {

tracing::debug!("Opening store at: {store_path}");

// Install SIGINT handler for graceful shutdown
let uteke = match Uteke::open(&store_path) {
Ok(u) => u,
Err(e) => {
Expand All @@ -534,7 +539,24 @@ fn main() {
}
};

ctrlc::set_handler(|| {
SHUTDOWN_REQUESTED.store(true, Ordering::SeqCst);
eprintln!("\nInterrupt received, shutting down gracefully...");
})
.expect("Failed to set SIGINT handler");

let result = run_command(&cli, &uteke);

// Graceful shutdown: save dirty index if needed
if let Err(e) = uteke.shutdown() {
tracing::warn!("Shutdown flush failed: {e}");
}

if SHUTDOWN_REQUESTED.load(Ordering::SeqCst) {
eprintln!("Shutdown complete.");
std::process::exit(130); // 128 + SIGINT(2)
}

if let Err(e) = result {
eprintln!("Error: {e}");
std::process::exit(1);
Expand Down
12 changes: 12 additions & 0 deletions crates/uteke-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,18 @@ impl Uteke {
Ok(lines.join("\n"))
}

/// Graceful shutdown — save dirty index to disk.
pub fn shutdown(&self) -> Result<(), Error> {
let mut index = self
.index
.lock()
.map_err(|_| Error::Database("lock".into()))?;
if index.is_dirty() {
index.save()?;
}
Ok(())
}

/// Import memories from JSONL format.
///
/// Each line should be a valid JSON object with `content`, `tags`, `metadata`, `created_at`.
Expand Down
Loading