@inetafrica/open-claudia 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bot.js +39 -0
- package/package.json +1 -1
package/bot.js
CHANGED
|
@@ -7,6 +7,45 @@ const cron = require("node-cron");
|
|
|
7
7
|
const Vault = require("./vault");
|
|
8
8
|
const CONFIG_DIR = require("./config-dir");
|
|
9
9
|
|
|
10
|
+
// ── Single instance lock ───────────────────────────────────────────
|
|
11
|
+
const LOCK_FILE = path.join(CONFIG_DIR, "bot.lock");
|
|
12
|
+
|
|
13
|
+
function acquireLock() {
|
|
14
|
+
// Check if another instance is running
|
|
15
|
+
if (fs.existsSync(LOCK_FILE)) {
|
|
16
|
+
try {
|
|
17
|
+
const pid = parseInt(fs.readFileSync(LOCK_FILE, "utf-8").trim(), 10);
|
|
18
|
+
// Check if that PID is still alive
|
|
19
|
+
process.kill(pid, 0); // throws if process doesn't exist
|
|
20
|
+
console.error(`Another instance is running (PID ${pid}). Exiting.`);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (e.code === "ESRCH") {
|
|
24
|
+
// Process is dead, stale lock — take over
|
|
25
|
+
} else if (e.code === "EPERM") {
|
|
26
|
+
// Process exists but we can't signal it
|
|
27
|
+
console.error("Another instance is running. Exiting.");
|
|
28
|
+
process.exit(1);
|
|
29
|
+
} else {
|
|
30
|
+
// Lock file is corrupt or unreadable — take over
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
fs.writeFileSync(LOCK_FILE, String(process.pid));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function releaseLock() {
|
|
38
|
+
try {
|
|
39
|
+
const pid = fs.readFileSync(LOCK_FILE, "utf-8").trim();
|
|
40
|
+
if (pid === String(process.pid)) fs.unlinkSync(LOCK_FILE);
|
|
41
|
+
} catch (e) {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
acquireLock();
|
|
45
|
+
process.on("exit", releaseLock);
|
|
46
|
+
process.on("SIGINT", () => { releaseLock(); process.exit(0); });
|
|
47
|
+
process.on("SIGTERM", () => { releaseLock(); process.exit(0); });
|
|
48
|
+
|
|
10
49
|
// ── Load Config from .env ───────────────────────────────────────────
|
|
11
50
|
function loadEnv() {
|
|
12
51
|
const envPath = path.join(CONFIG_DIR, ".env");
|