Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 082bfa8

Browse files
committed
I don't even know anymore man
1 parent 6ef8769 commit 082bfa8

File tree

7 files changed

+202
-123
lines changed

7 files changed

+202
-123
lines changed

src/core/stacks/controller.ts

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { rm } from "node:fs/promises";
22
import DockerCompose from "docker-compose";
33
import { dbFunctions } from "~/core/database";
44
import { logger } from "~/core/utils/logger";
5-
import { postToClient } from "~/handlers/modules/live-stacks";
65
import type { stacks_config } from "~/typings/database";
76
import type { Stack } from "~/typings/docker-compose";
87
import type { ComposeSpec } from "~/typings/docker-compose";
8+
import { broadcast } from "../../handlers/modules/docker-socket";
99
import { checkStacks } from "./checker";
1010
import { runStackCommand } from "./operations/runStackCommand";
1111
import { wrapProgressCallback } from "./operations/runStackCommand";
@@ -33,13 +33,17 @@ export async function deployStack(stack_config: stacks_config): Promise<void> {
3333
throw new Error("Failed to add stack to database");
3434
}
3535

36-
postToClient({
37-
type: "stack-status",
38-
timestamp: new Date(),
36+
// Broadcast pending status
37+
broadcast({
38+
topic: "stack",
3939
data: {
40-
stack_id: stackId,
41-
status: "pending",
42-
message: "Creating stack configuration",
40+
timestamp: new Date(),
41+
type: "stack-status",
42+
data: {
43+
stack_id: stackId,
44+
status: "pending",
45+
message: "Creating stack configuration",
46+
},
4347
},
4448
});
4549

@@ -65,13 +69,17 @@ export async function deployStack(stack_config: stacks_config): Promise<void> {
6569
"deploying",
6670
);
6771

68-
postToClient({
69-
type: "stack-status",
70-
timestamp: new Date(),
72+
// Broadcast deployed status
73+
broadcast({
74+
topic: "stack",
7175
data: {
72-
stack_id: stackId,
73-
status: "deployed",
74-
message: "Stack deployed successfully",
76+
timestamp: new Date(),
77+
type: "stack-status",
78+
data: {
79+
stack_id: stackId,
80+
status: "deployed",
81+
message: "Stack deployed successfully",
82+
},
7583
},
7684
});
7785

@@ -109,14 +117,17 @@ export async function deployStack(stack_config: stacks_config): Promise<void> {
109117
}
110118
}
111119

112-
postToClient({
113-
type: "stack-error",
114-
timestamp: new Date(),
120+
// Broadcast deployment error
121+
broadcast({
122+
topic: "stack",
115123
data: {
116-
stack_id: stackId ?? 0,
117-
action: "deploying",
118-
message: errorMsg,
119-
timestamp: new Date().toISOString(),
124+
timestamp: new Date(),
125+
type: "stack-error",
126+
data: {
127+
stack_id: stackId ?? 0,
128+
action: "deploying",
129+
message: errorMsg,
130+
},
120131
},
121132
});
122133
throw new Error(errorMsg);
@@ -211,40 +222,50 @@ export async function removeStack(stack_id: number): Promise<void> {
211222
} catch (error) {
212223
const errorMsg = error instanceof Error ? error.message : String(error);
213224
logger.error(errorMsg);
214-
postToClient({
215-
type: "stack-error",
216-
timestamp: new Date(),
225+
// Broadcast removal error
226+
broadcast({
227+
topic: "stack",
217228
data: {
218-
stack_id,
219-
action: "removing",
220-
message: `Directory removal failed: ${errorMsg}`,
221-
timestamp: new Date().toISOString(),
229+
timestamp: new Date(),
230+
type: "stack-error",
231+
data: {
232+
stack_id,
233+
action: "removing",
234+
message: `Directory removal failed: ${errorMsg}`,
235+
},
222236
},
223237
});
224238
throw new Error(errorMsg);
225239
}
226240

227241
dbFunctions.deleteStack(stack_id);
228242

229-
postToClient({
230-
type: "stack-removed",
231-
timestamp: new Date(),
243+
// Broadcast successful removal
244+
broadcast({
245+
topic: "stack",
232246
data: {
233-
stack_id,
234-
message: "Stack removed successfully",
247+
timestamp: new Date(),
248+
type: "stack-removed",
249+
data: {
250+
stack_id,
251+
message: "Stack removed successfully",
252+
},
235253
},
236254
});
237255
} catch (error: unknown) {
238256
const errorMsg = error instanceof Error ? error.message : String(error);
239257
logger.error(errorMsg);
240-
postToClient({
241-
type: "stack-error",
242-
timestamp: new Date(),
258+
// Broadcast removal error
259+
broadcast({
260+
topic: "stack",
243261
data: {
244-
stack_id,
245-
action: "removing",
246-
message: errorMsg,
247-
timestamp: new Date().toISOString(),
262+
timestamp: new Date(),
263+
type: "stack-error",
264+
data: {
265+
stack_id,
266+
action: "removing",
267+
message: errorMsg,
268+
},
248269
},
249270
});
250271
throw new Error(errorMsg);

src/core/stacks/operations/runStackCommand.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from "~/core/utils/logger";
2-
import { postToClient } from "~/handlers/modules/live-stacks";
32
import type { Stack } from "~/typings/docker-compose";
3+
import { broadcast } from "../../../handlers/modules/docker-socket";
44
import { getStackName, getStackPath } from "./stackHelpers";
55

66
export function wrapProgressCallback(progressCallback?: (log: string) => void) {
@@ -49,14 +49,17 @@ export async function runStackCommand<T>(
4949
}
5050
}
5151

52-
postToClient({
53-
type: "stack-progress",
54-
timestamp: new Date(),
52+
// Broadcast progress
53+
broadcast({
54+
topic: "stack",
5555
data: {
56-
stack_id,
57-
action,
58-
message,
59-
timestamp: new Date().toISOString(),
56+
timestamp: new Date(),
57+
type: "stack-progress",
58+
data: {
59+
stack_id,
60+
message,
61+
action,
62+
},
6063
},
6164
});
6265
};
@@ -69,23 +72,43 @@ export async function runStackCommand<T>(
6972
`Successfully completed command for stack_id=${stack_id}, action="${action}"`,
7073
);
7174

75+
// Optionally broadcast status on completion
76+
broadcast({
77+
topic: "stack",
78+
data: {
79+
timestamp: new Date(),
80+
type: "stack-status",
81+
data: {
82+
stack_id,
83+
status: "completed",
84+
message: `Completed ${action}`,
85+
action,
86+
},
87+
},
88+
});
89+
7290
return result;
7391
} catch (error: unknown) {
7492
const errorMsg =
7593
error instanceof Error ? error.message : JSON.stringify(error);
7694
logger.debug(
7795
`Error occurred for stack_id=${stack_id}, action="${action}": ${errorMsg}`,
7896
);
79-
postToClient({
80-
type: "stack-error",
81-
timestamp: new Date(),
97+
98+
// Broadcast error
99+
broadcast({
100+
topic: "stack",
82101
data: {
83-
stack_id,
84-
action,
85-
message: errorMsg,
86-
timestamp: new Date().toISOString(),
102+
timestamp: new Date(),
103+
type: "stack-error",
104+
data: {
105+
stack_id,
106+
action,
107+
message: errorMsg,
108+
},
87109
},
88110
});
111+
89112
throw new Error(`Error while ${action} stack "${stack_id}": ${errorMsg}`);
90113
}
91114
}

src/core/utils/logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "node:path";
2-
import chalk, { type ChalkFunction } from "chalk";
2+
import chalk from "chalk";
3+
import type { ChalkInstance } from "chalk";
34
import type { TransformableInfo } from "logform";
45
import { createLogger, format, transports } from "winston";
56
import wrapAnsi from "wrap-ansi";
@@ -53,7 +54,7 @@ const formatTerminalMessage = (message: string, prefix: string): string => {
5354
}
5455
};
5556

56-
const levelColors: Record<LogLevel | string, ChalkFunction> = {
57+
const levelColors: Record<LogLevel | string, ChalkInstance> = {
5758
error: chalk.red.bold,
5859
warn: chalk.yellow.bold,
5960
info: chalk.green.bold,

src/handlers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const handlers = {
1818
StackHandler,
1919
LogHandler,
2020
CheckHealth,
21-
Sockets: Sockets,
21+
Socket: "ws://localhost:4837/ws",
2222
};
2323

2424
Starter.startAll();

0 commit comments

Comments
 (0)