Files
mcp-memory/inject_gc.py
T

46 lines
1.6 KiB
Python

import sys
main_path = r"C:\Users\reazul.ashraf\workspace\rust\mcp-memory\server\src\main.rs"
with open(main_path, "r", encoding="utf-8") as f:
content = f.read()
idx = content.find('eprintln!("MCP Memory Server running on http://127.0.0.1:3000/sse");')
if idx == -1:
print("Could not find start point")
sys.exit(1)
gc_code = """
// Background Garbage Collection for old tasks
let state_gc = state_clone.clone();
tokio::spawn(async move {
loop {
// Run every 24 hours
tokio::time::sleep(tokio::time::Duration::from_secs(24 * 3600)).await;
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
let fourteen_days = 14 * 24 * 3600;
let cutoff = now.saturating_sub(fourteen_days);
state_gc.tasks.modify(|tasks| {
let initial_len = tasks.len();
tasks.retain(|task| {
if task.status.to_lowercase() == "completed" && task.created_at < cutoff {
false // remove
} else {
true // keep
}
});
if tasks.len() < initial_len {
eprintln!("GC: Removed {} old completed tasks", initial_len - tasks.len());
}
});
}
});
"""
content = content[:idx] + gc_code + content[idx:]
with open(main_path, "w", encoding="utf-8") as f:
f.write(content)
print("Injected GC task!")