@@ -55,25 +55,46 @@ export const runCommand = async (scheduleId: string) => {
5555 description : "Schedule" ,
5656 } ) ;
5757
58- if ( scheduleType === "application" || scheduleType === "compose" ) {
59- let containerId = "" ;
60- let serverId = "" ;
61- if ( scheduleType === "application" && application ) {
62- const container = await getServiceContainer (
63- application . appName ,
64- application . serverId ,
65- ) ;
66- containerId = container ?. Id || "" ;
67- serverId = application . serverId || "" ;
68- }
69- if ( scheduleType === "compose" && compose ) {
70- const container = await getComposeContainer ( compose , serviceName || "" ) ;
71- containerId = container ?. Id || "" ;
72- serverId = compose . serverId || "" ;
73- }
58+ try {
59+ if ( scheduleType === "application" || scheduleType === "compose" ) {
60+ let containerId = "" ;
61+ let serverId = "" ;
62+ if ( scheduleType === "application" && application ) {
63+ const container = await getServiceContainer (
64+ application . appName ,
65+ application . serverId ,
66+ ) ;
67+ containerId = container ?. Id || "" ;
68+ serverId = application . serverId || "" ;
69+ }
70+ if ( scheduleType === "compose" && compose ) {
71+ const container = await getComposeContainer ( compose , serviceName || "" ) ;
72+ containerId = container ?. Id || "" ;
73+ serverId = compose . serverId || "" ;
74+ }
75+
76+ if ( ! containerId ) {
77+ const target =
78+ scheduleType === "compose"
79+ ? `service '${ serviceName } ' of compose '${ compose ?. name } '`
80+ : `application '${ application ?. appName } '` ;
81+ const message = `Container not found for ${ target } , make sure the service is running` ;
82+ if ( serverId ) {
83+ await execAsyncRemote (
84+ serverId ,
85+ `echo ${ quote ( [ `❌ ${ message } ` ] ) } >> ${ quote ( [ deployment . logPath ] ) } ` ,
86+ ) ;
87+ } else {
88+ const writeStream = createWriteStream ( deployment . logPath , {
89+ flags : "a" ,
90+ } ) ;
91+ writeStream . write ( `❌ ${ message } \n` ) ;
92+ writeStream . end ( ) ;
93+ }
94+ throw new Error ( message ) ;
95+ }
7496
75- if ( serverId ) {
76- try {
97+ if ( serverId ) {
7798 await execAsyncRemote (
7899 serverId ,
79100 `
@@ -86,47 +107,44 @@ export const runCommand = async (scheduleId: string) => {
86107 echo "✅ Command executed successfully" >> ${ quote ( [ deployment . logPath ] ) } ;
87108 ` ,
88109 ) ;
89- } catch ( error ) {
90- await updateDeploymentStatus ( deployment . deploymentId , "error" ) ;
91- throw error ;
92- }
93- } else {
94- const writeStream = createWriteStream ( deployment . logPath , { flags : "a" } ) ;
110+ } else {
111+ const writeStream = createWriteStream ( deployment . logPath , {
112+ flags : "a" ,
113+ } ) ;
95114
96- try {
97- if ( IS_CLOUD ) {
115+ try {
116+ if ( IS_CLOUD ) {
117+ writeStream . write (
118+ "This feature is not available in the cloud version." ,
119+ ) ;
120+ writeStream . end ( ) ;
121+ return { ...deployment , status : "running" as const } ;
122+ }
98123 writeStream . write (
99- "This feature is not available in the cloud version." ,
124+ `docker exec ${ containerId } ${ shellType } -c ${ command } \n` ,
125+ ) ;
126+ await spawnAsync (
127+ "docker" ,
128+ [ "exec" , containerId , shellType , "-c" , command ] ,
129+ ( data ) => {
130+ if ( writeStream . writable ) {
131+ writeStream . write ( data ) ;
132+ }
133+ } ,
100134 ) ;
135+
136+ writeStream . write ( "✅ Command executed successfully\n" ) ;
101137 writeStream . end ( ) ;
102- return ;
138+ } catch ( error ) {
139+ writeStream . write ( "❌ Command failed\n" ) ;
140+ writeStream . write (
141+ error instanceof Error ? error . message : "Unknown error" ,
142+ ) ;
143+ writeStream . end ( ) ;
144+ throw error ;
103145 }
104- writeStream . write (
105- `docker exec ${ containerId } ${ shellType } -c ${ command } \n` ,
106- ) ;
107- await spawnAsync (
108- "docker" ,
109- [ "exec" , containerId , shellType , "-c" , command ] ,
110- ( data ) => {
111- if ( writeStream . writable ) {
112- writeStream . write ( data ) ;
113- }
114- } ,
115- ) ;
116-
117- writeStream . write ( "✅ Command executed successfully\n" ) ;
118- } catch ( error ) {
119- writeStream . write ( "❌ Command failed\n" ) ;
120- writeStream . write (
121- error instanceof Error ? error . message : "Unknown error" ,
122- ) ;
123- writeStream . end ( ) ;
124- await updateDeploymentStatus ( deployment . deploymentId , "error" ) ;
125- throw error ;
126146 }
127- }
128- } else if ( scheduleType === "dokploy-server" ) {
129- try {
147+ } else if ( scheduleType === "dokploy-server" ) {
130148 const writeStream = createWriteStream ( deployment . logPath , { flags : "a" } ) ;
131149 const { SCHEDULES_PATH } = paths ( ) ;
132150 const fullPath = path . join ( SCHEDULES_PATH , appName || "" ) ;
@@ -151,18 +169,13 @@ export const runCommand = async (scheduleId: string) => {
151169 cwd : fullPath ,
152170 } ,
153171 ) ;
154- } catch ( error ) {
155- await updateDeploymentStatus ( deployment . deploymentId , "error" ) ;
156- throw error ;
157- }
158- } else if ( scheduleType === "server" ) {
159- try {
172+ } else if ( scheduleType === "server" ) {
160173 const { SCHEDULES_PATH } = paths ( true ) ;
161174 const fullPath = path . join ( SCHEDULES_PATH , appName || "" ) ;
162175 const command = `
163176 set -e
164177 echo "Running script" >> ${ deployment . logPath } ;
165- bash -c ${ fullPath } /script.sh 2>&1 | tee -a ${ deployment . logPath } || {
178+ bash -c ${ fullPath } /script.sh 2>&1 | tee -a ${ deployment . logPath } || {
166179 echo "❌ Command failed" >> ${ deployment . logPath } ;
167180 exit 1;
168181 }
@@ -177,10 +190,11 @@ export const runCommand = async (scheduleId: string) => {
177190 } ) ;
178191 }
179192 } ) ;
180- } catch ( error ) {
181- await updateDeploymentStatus ( deployment . deploymentId , "error" ) ;
182- throw error ;
183193 }
194+ await updateDeploymentStatus ( deployment . deploymentId , "done" ) ;
195+ return { ...deployment , status : "done" as const } ;
196+ } catch {
197+ await updateDeploymentStatus ( deployment . deploymentId , "error" ) ;
198+ return { ...deployment , status : "error" as const } ;
184199 }
185- await updateDeploymentStatus ( deployment . deploymentId , "done" ) ;
186200} ;
0 commit comments