Compare commits

..

1 Commits

Author SHA1 Message Date
2a6a1b093a Fix: dockge cannot be self-managed 2023-11-25 00:59:36 +08:00
6 changed files with 13 additions and 81 deletions

View File

@ -30,7 +30,6 @@ import { Cron } from "croner";
import gracefulShutdown from "http-graceful-shutdown"; import gracefulShutdown from "http-graceful-shutdown";
import User from "./models/user"; import User from "./models/user";
import childProcess from "child_process"; import childProcess from "child_process";
import { Terminal } from "./terminal";
export class DockgeServer { export class DockgeServer {
app : Express; app : Express;
@ -231,11 +230,6 @@ export class DockgeServer {
}); });
if (isDev) {
setInterval(() => {
log.debug("terminal", "Terminal count: " + Terminal.getTerminalCount());
}, 5000);
}
} }
async afterLogin(socket : DockgeSocket, user : User) { async afterLogin(socket : DockgeSocket, user : User) {
@ -298,11 +292,11 @@ export class DockgeServer {
log.info("server", `Listening on ${this.config.port}`); log.info("server", `Listening on ${this.config.port}`);
} }
// Run every 10 seconds // Run every 5 seconds
Cron("*/10 * * * * *", { Cron("*/2 * * * * *", {
protect: true, // Enabled over-run protection. protect: true, // Enabled over-run protection.
}, () => { }, () => {
//log.debug("server", "Cron job running"); log.debug("server", "Cron job running");
this.sendStackList(true); this.sendStackList(true);
}); });

View File

@ -75,9 +75,7 @@ export class DockerSocketHandler extends SocketHandler {
const stack = Stack.getStack(server, stackName); const stack = Stack.getStack(server, stackName);
if (stack.isManagedByDockge) { stack.joinCombinedTerminal(socket);
stack.joinCombinedTerminal(socket);
}
callback({ callback({
ok: true, ok: true,

View File

@ -140,26 +140,9 @@ export class TerminalSocketHandler extends SocketHandler {
} }
}); });
// Leave Combined Terminal // Close Terminal
socket.on("leaveCombinedTerminal", async (stackName : unknown, callback) => { socket.on("terminalClose", async (terminalName : unknown, callback : unknown) => {
try {
checkLogin(socket);
log.debug("leaveCombinedTerminal", "Stack name: " + stackName);
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string.");
}
const stack = Stack.getStack(server, stackName);
await stack.leaveCombinedTerminal(socket);
callback({
ok: true,
});
} catch (e) {
callbackError(e, callback);
}
}); });
// TODO: Resize Terminal // TODO: Resize Terminal

View File

@ -298,7 +298,7 @@ export class Stack {
} }
} }
} else { } else {
//log.debug("getStack", "Skip FS operations"); log.debug("getStack", "Skip FS operations");
} }
let stack : Stack; let stack : Stack;
@ -374,21 +374,12 @@ export class Stack {
async joinCombinedTerminal(socket: DockgeSocket) { async joinCombinedTerminal(socket: DockgeSocket) {
const terminalName = getCombinedTerminalName(this.name); const terminalName = getCombinedTerminalName(this.name);
const terminal = Terminal.getOrCreateTerminal(this.server, terminalName, "docker", [ "compose", "logs", "-f", "--tail", "100" ], this.path); const terminal = Terminal.getOrCreateTerminal(this.server, terminalName, "docker", [ "compose", "logs", "-f", "--tail", "100" ], this.path);
terminal.enableKeepAlive = true;
terminal.rows = COMBINED_TERMINAL_ROWS; terminal.rows = COMBINED_TERMINAL_ROWS;
terminal.cols = COMBINED_TERMINAL_COLS; terminal.cols = COMBINED_TERMINAL_COLS;
terminal.join(socket); terminal.join(socket);
terminal.start(); terminal.start();
} }
async leaveCombinedTerminal(socket: DockgeSocket) {
const terminalName = getCombinedTerminalName(this.name);
const terminal = Terminal.getTerminal(terminalName);
if (terminal) {
terminal.leave(socket);
}
}
async joinContainerTerminal(socket: DockgeSocket, serviceName: string, shell : string = "sh", index: number = 0) { async joinContainerTerminal(socket: DockgeSocket, serviceName: string, shell : string = "sh", index: number = 0) {
const terminalName = getContainerExecTerminalName(this.name, serviceName, index); const terminalName = getContainerExecTerminalName(this.name, serviceName, index);
let terminal = Terminal.getTerminal(terminalName); let terminal = Terminal.getTerminal(terminalName);

View File

@ -34,9 +34,6 @@ export class Terminal {
protected _rows : number = TERMINAL_ROWS; protected _rows : number = TERMINAL_ROWS;
protected _cols : number = TERMINAL_COLS; protected _cols : number = TERMINAL_COLS;
public enableKeepAlive : boolean = false;
protected keepAliveInterval? : NodeJS.Timeout;
constructor(server : DockgeServer, name : string, file : string, args : string | string[], cwd : string) { constructor(server : DockgeServer, name : string, file : string, args : string | string[], cwd : string) {
this.server = server; this.server = server;
this._name = name; this._name = name;
@ -83,25 +80,6 @@ export class Terminal {
return; return;
} }
if (this.enableKeepAlive) {
log.debug("Terminal", "Keep alive enabled for terminal " + this.name);
// Close if there is no clients
this.keepAliveInterval = setInterval(() => {
const clients = this.server.io.sockets.adapter.rooms.get(this.name);
const numClients = clients ? clients.size : 0;
if (numClients === 0) {
log.debug("Terminal", "Terminal " + this.name + " has no client, closing...");
this.close();
} else {
log.debug("Terminal", "Terminal " + this.name + " has " + numClients + " client(s)");
}
}, 60 * 1000);
} else {
log.debug("Terminal", "Keep alive disabled for terminal " + this.name);
}
try { try {
this._ptyProcess = pty.spawn(this.file, this.args, { this._ptyProcess = pty.spawn(this.file, this.args, {
name: this.name, name: this.name,
@ -122,8 +100,6 @@ export class Terminal {
this._ptyProcess.onExit(this.exit); this._ptyProcess.onExit(this.exit);
} catch (error) { } catch (error) {
if (error instanceof Error) { if (error instanceof Error) {
clearInterval(this.keepAliveInterval);
log.error("Terminal", "Failed to start terminal: " + error.message); log.error("Terminal", "Failed to start terminal: " + error.message);
const exitCode = Number(error.message.split(" ").pop()); const exitCode = Number(error.message.split(" ").pop());
this.exit({ this.exit({
@ -146,8 +122,6 @@ export class Terminal {
Terminal.terminalMap.delete(this.name); Terminal.terminalMap.delete(this.name);
log.debug("Terminal", "Terminal " + this.name + " exited with code " + res.exitCode); log.debug("Terminal", "Terminal " + this.name + " exited with code " + res.exitCode);
clearInterval(this.keepAliveInterval);
if (this.callback) { if (this.callback) {
this.callback(res.exitCode); this.callback(res.exitCode);
} }
@ -184,9 +158,7 @@ export class Terminal {
} }
close() { close() {
clearInterval(this.keepAliveInterval); this._ptyProcess?.kill();
// Send Ctrl+C to the terminal
this.ptyProcess?.write("\x03");
} }
/** /**
@ -221,10 +193,6 @@ export class Terminal {
terminal.start(); terminal.start();
}); });
} }
public static getTerminalCount() {
return Terminal.terminalMap.size;
}
} }
/** /**

View File

@ -319,12 +319,6 @@ export default {
}, },
deep: true, deep: true,
}, },
$route(to, from) {
// Leave Combined Terminal
console.debug("leaveCombinedTerminal", from.params.stackName);
this.$root.getSocket().emit("leaveCombinedTerminal", this.stack.name, () => {});
}
}, },
mounted() { mounted() {
if (this.isAdd) { if (this.isAdd) {
@ -367,7 +361,7 @@ export default {
clearTimeout(serviceStatusTimeout); clearTimeout(serviceStatusTimeout);
serviceStatusTimeout = setTimeout(async () => { serviceStatusTimeout = setTimeout(async () => {
this.requestServiceStatus(); this.requestServiceStatus();
}, 5000); }, 2000);
}, },
requestServiceStatus() { requestServiceStatus() {
@ -550,6 +544,10 @@ export default {
throw new Error("Services must be an object"); throw new Error("Services must be an object");
} }
if (!config.version) {
config.version = "3.8";
}
this.yamlDoc = doc; this.yamlDoc = doc;
this.jsonConfig = config; this.jsonConfig = config;