@inetafrica/open-claudia 1.2.3 → 1.2.5
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 +10 -41
- package/package.json +1 -1
package/bot.js
CHANGED
|
@@ -7,44 +7,9 @@ const cron = require("node-cron");
|
|
|
7
7
|
const Vault = require("./vault");
|
|
8
8
|
const CONFIG_DIR = require("./config-dir");
|
|
9
9
|
|
|
10
|
-
// ──
|
|
11
|
-
|
|
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); });
|
|
10
|
+
// ── Graceful shutdown ──────────────────────────────────────────────
|
|
11
|
+
process.on("SIGINT", () => process.exit(0));
|
|
12
|
+
process.on("SIGTERM", () => process.exit(0));
|
|
48
13
|
|
|
49
14
|
// ── Load Config from .env ───────────────────────────────────────────
|
|
50
15
|
function loadEnv() {
|
|
@@ -112,6 +77,11 @@ let reconnectTimer = null;
|
|
|
112
77
|
bot.on("polling_error", (err) => {
|
|
113
78
|
const msg = err.message || "";
|
|
114
79
|
console.error("Polling error:", msg);
|
|
80
|
+
// Another instance is already running — exit immediately
|
|
81
|
+
if (msg.includes("409 Conflict")) {
|
|
82
|
+
console.error("Another instance is polling. Exiting.");
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
115
85
|
if (msg.includes("ETIMEDOUT") || msg.includes("ECONNRESET") || msg.includes("ENOTFOUND") || msg.includes("EFATAL")) {
|
|
116
86
|
if (reconnectTimer) return; // Already scheduled
|
|
117
87
|
console.log("Network lost. Reconnecting in 10s...");
|
|
@@ -830,8 +800,7 @@ bot.onText(/\/version$/, (msg) => {
|
|
|
830
800
|
bot.onText(/\/restart$/, async (msg) => {
|
|
831
801
|
if (!isOwner(msg)) return;
|
|
832
802
|
await send("Going offline for a quick restart — back in a moment.");
|
|
833
|
-
|
|
834
|
-
setTimeout(() => process.exit(0), 500);
|
|
803
|
+
setTimeout(() => process.exit(0), 1000);
|
|
835
804
|
});
|
|
836
805
|
|
|
837
806
|
bot.onText(/\/upgrade$/, async (msg) => {
|
|
@@ -845,7 +814,7 @@ bot.onText(/\/upgrade$/, async (msg) => {
|
|
|
845
814
|
// Read version from newly installed package
|
|
846
815
|
const root = execSync("npm root -g", { encoding: "utf-8", env: { ...process.env, PATH: FULL_PATH } }).trim();
|
|
847
816
|
const newPkg = JSON.parse(fs.readFileSync(path.join(root, "@inetafrica", "open-claudia", "package.json"), "utf-8"));
|
|
848
|
-
await send(`
|
|
817
|
+
await send(`Installed v${newPkg.version}. Going offline to restart...`);
|
|
849
818
|
} catch (e) {
|
|
850
819
|
await send(`Upgrade failed: ${e.message}`);
|
|
851
820
|
return;
|