KI-AGENT: Asterisk-Statusprüfung robuster machen
This commit is contained in:
@@ -11,6 +11,17 @@ const telephonyEnabled = () =>
|
||||
const asteriskHttpStatusUrl = () =>
|
||||
process.env.TELEPHONY_ASTERISK_HTTP_URL || "http://asterisk-dev:8088/ws"
|
||||
|
||||
const asteriskHttpStatusUrls = () => {
|
||||
const configuredUrl = asteriskHttpStatusUrl()
|
||||
return Array.from(new Set([
|
||||
configuredUrl,
|
||||
"http://asterisk-dev:8088/ws",
|
||||
"http://host.docker.internal:8088/ws",
|
||||
`http://127.0.0.1:${process.env.TELEPHONY_DEV_WS_PORT || "8088"}/ws`,
|
||||
`http://localhost:${process.env.TELEPHONY_DEV_WS_PORT || "8088"}/ws`,
|
||||
]))
|
||||
}
|
||||
|
||||
const publicAsteriskWsUrl = () =>
|
||||
process.env.TELEPHONY_ASTERISK_WS_URL || `ws://localhost:${process.env.TELEPHONY_DEV_WS_PORT || "8088"}/ws`
|
||||
|
||||
@@ -54,40 +65,52 @@ export default async function telephonyRoutes(server: FastifyInstance) {
|
||||
|
||||
server.get("/telephony/status", async () => {
|
||||
const enabled = telephonyEnabled()
|
||||
const url = asteriskHttpStatusUrl()
|
||||
const urls = asteriskHttpStatusUrls()
|
||||
|
||||
if (!enabled) {
|
||||
return {
|
||||
enabled,
|
||||
provider: "asterisk",
|
||||
reachable: false,
|
||||
statusUrl: url,
|
||||
message: "Telefonie ist nicht aktiviert.",
|
||||
let lastError: any = null
|
||||
const attempts = []
|
||||
|
||||
for (const url of urls) {
|
||||
try {
|
||||
const response = await fetchWithTimeout(url)
|
||||
attempts.push({
|
||||
url,
|
||||
reachable: true,
|
||||
statusCode: response.status,
|
||||
})
|
||||
|
||||
return {
|
||||
enabled,
|
||||
provider: "asterisk",
|
||||
reachable: true,
|
||||
statusCode: response.status,
|
||||
statusUrl: url,
|
||||
attempts,
|
||||
message: enabled
|
||||
? (response.ok ? "Asterisk ist erreichbar." : `Asterisk-HTTP ist erreichbar (HTTP ${response.status}).`)
|
||||
: "Asterisk ist erreichbar, Telefonie ist aber noch nicht aktiviert.",
|
||||
}
|
||||
} catch (error: any) {
|
||||
lastError = error
|
||||
attempts.push({
|
||||
url,
|
||||
reachable: false,
|
||||
message: error?.name === "AbortError"
|
||||
? "Abgelaufen"
|
||||
: (error?.message || "Nicht erreichbar"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetchWithTimeout(url)
|
||||
return {
|
||||
enabled,
|
||||
provider: "asterisk",
|
||||
reachable: true,
|
||||
statusCode: response.status,
|
||||
statusUrl: url,
|
||||
message: response.ok
|
||||
? "Asterisk ist erreichbar."
|
||||
: `Asterisk-HTTP ist erreichbar (HTTP ${response.status}).`,
|
||||
}
|
||||
} catch (error: any) {
|
||||
return {
|
||||
enabled,
|
||||
provider: "asterisk",
|
||||
reachable: false,
|
||||
statusUrl: url,
|
||||
message: error?.name === "AbortError"
|
||||
? "Asterisk-Statusabfrage ist abgelaufen."
|
||||
: (error?.message || "Asterisk ist nicht erreichbar."),
|
||||
}
|
||||
return {
|
||||
enabled,
|
||||
provider: "asterisk",
|
||||
reachable: false,
|
||||
statusUrl: urls[0],
|
||||
attempts,
|
||||
message: lastError?.name === "AbortError"
|
||||
? "Asterisk-Statusabfrage ist abgelaufen."
|
||||
: (lastError?.message || "Asterisk ist nicht erreichbar."),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user