@@ -2,10 +2,10 @@ import { rm } from "node:fs/promises";
22import DockerCompose from "docker-compose" ;
33import { dbFunctions } from "~/core/database" ;
44import { logger } from "~/core/utils/logger" ;
5- import { postToClient } from "~/handlers/modules/live-stacks" ;
65import type { stacks_config } from "~/typings/database" ;
76import type { Stack } from "~/typings/docker-compose" ;
87import type { ComposeSpec } from "~/typings/docker-compose" ;
8+ import { broadcast } from "../../handlers/modules/docker-socket" ;
99import { checkStacks } from "./checker" ;
1010import { runStackCommand } from "./operations/runStackCommand" ;
1111import { 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 ) ;
0 commit comments