botserver/src/core/bootstrap/instance.rs
Rodrigo Rodriguez (Pragmatismo) 7d8f141fc2
Some checks failed
BotServer CI/CD / build (push) Failing after 1m28s
refactor: Replace all hardcoded ./botserver-stack paths with get_stack_path()/get_work_path()
- Adds get_stack_path() helper: returns /opt/gbo in production (.env without botserver-stack), ./botserver-stack in dev
- Adds get_work_path() helper: returns /opt/gbo/work in production, ./botserver-stack/data/system/work in dev
- Updated 35+ files to use dynamic path resolution
- Production system container no longer needs botserver-stack directory
- Work files go to /opt/gbo/work instead of /opt/gbo/bin/botserver-stack
2026-04-04 09:24:44 -03:00

51 lines
1.6 KiB
Rust

//! Instance locking functions for bootstrap
//!
//! Extracted from mod.rs
use crate::core::shared::utils::get_stack_path;
use crate::security::command_guard::SafeCommand;
use log::warn;
use std::fs;
use std::path::PathBuf;
/// Check if another instance is already running
pub fn check_single_instance() -> Result<bool, Box<dyn std::error::Error>> {
let stack_path = get_stack_path();
let lock_file = PathBuf::from(&stack_path).join(".lock");
if lock_file.exists() {
if let Ok(pid_str) = fs::read_to_string(&lock_file) {
if let Ok(pid) = pid_str.trim().parse::<i32>() {
let pid_str = pid.to_string();
if let Some(output) = SafeCommand::new("kill")
.and_then(|c| c.args(&["-0", &pid_str]))
.ok()
.and_then(|cmd| cmd.execute().ok())
{
if output.status.success() {
warn!(
"Another botserver process (PID {}) is already running on this stack",
pid
);
return Ok(false);
}
}
}
}
}
let pid = std::process::id();
if let Some(parent) = lock_file.parent() {
fs::create_dir_all(parent).ok();
}
fs::write(&lock_file, pid.to_string()).ok();
Ok(true)
}
/// Release the instance lock
pub fn release_instance_lock() {
let stack_path = get_stack_path();
let lock_file = PathBuf::from(&stack_path).join(".lock");
if lock_file.exists() {
fs::remove_file(&lock_file).ok();
}
}