Skip to content

Commit 726779b

Browse files
authored
wait for server (#1588)
1 parent b3c77e6 commit 726779b

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src-electron/ide-integration/studio-rest-api.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,31 @@ function wsApiUrl(api, path) {
136136
return wsLocalhost + studioHttpPort + api + path
137137
}
138138

139+
/**
140+
* Retry connecting to the Studio server until it is ready.
141+
* @param {string} url - The URL to connect to.
142+
* @param {number} retries - Number of retry attempts.
143+
* @param {number} delay - Delay between retries in milliseconds.
144+
* @returns {Promise} - Resolves when the connection is successful, rejects if it fails.
145+
*/
146+
async function waitForServer(url, retries = 5, delay = 2000) {
147+
for (let attempt = 1; attempt <= retries; attempt++) {
148+
try {
149+
let response = await axios.get(url)
150+
if (response.status === 200) {
151+
return response // Server is ready
152+
}
153+
} catch (err) {
154+
env.logWarning(`Attempt ${attempt} failed: ${err.message}`)
155+
if (attempt === retries) {
156+
throw new Error(
157+
`Failed to connect to Studio server at ${url} after ${retries} attempts.`
158+
)
159+
}
160+
}
161+
await new Promise((resolve) => setTimeout(resolve, delay)) // Wait before retrying
162+
}
163+
}
139164
/**
140165
* Send HTTP GET request to Studio Jetty server for project information.
141166
* @param {} db
@@ -151,16 +176,14 @@ async function getProjectInfo(db, sessionId) {
151176
if (studioIntegration && !isUserDisabled) {
152177
let path = restApiUrl(StudioRestAPI.GetProjectInfo, project)
153178
env.logDebug(`StudioUC(${name}): GET: ${path}`)
154-
return axios
155-
.get(path)
156-
.then((resp) => {
157-
env.logDebug(`StudioUC(${name}): RESP: ${resp.status}`)
158-
return resp
159-
})
160-
.catch((err) => {
161-
env.logWarning(`StudioUC(${name}): ERR: ${err.message}`)
162-
return { data: [] }
163-
})
179+
try {
180+
let response = await waitForServer(path) // Wait for the server to be ready
181+
env.logDebug(`StudioUC(${name}): RESP: ${response.status}`)
182+
return response
183+
} catch (err) {
184+
env.logWarning(`StudioUC(${name}): ERR: ${err.message}`)
185+
return { data: [] }
186+
}
164187
} else {
165188
if (!isUserDisabled)
166189
env.logWarning(`StudioUC(${name}): Studio integration is now enabled!`)

0 commit comments

Comments
 (0)