mirror of
https://github.com/pockethost/pockethost.git
synced 2025-11-24 14:35:49 +00:00
fix: improve instance shutdown handling
This commit is contained in:
parent
3b6cf119e7
commit
564f65047b
@ -92,6 +92,9 @@ export const instanceService = mkSingleton(
|
|||||||
let _shutdownReason: Error | undefined
|
let _shutdownReason: Error | undefined
|
||||||
let internalUrl: string | undefined
|
let internalUrl: string | undefined
|
||||||
|
|
||||||
|
// Declare api variable early to avoid temporal dead zone
|
||||||
|
let api: InstanceApi
|
||||||
|
|
||||||
const clientLimiter = new Bottleneck({ maxConcurrent: 1 })
|
const clientLimiter = new Bottleneck({ maxConcurrent: 1 })
|
||||||
const updateInstance = clientLimiter.wrap(
|
const updateInstance = clientLimiter.wrap(
|
||||||
(id: InstanceId, fields: Partial<InstanceFields>) => {
|
(id: InstanceId, fields: Partial<InstanceFields>) => {
|
||||||
@ -112,6 +115,12 @@ export const instanceService = mkSingleton(
|
|||||||
let openRequestCount = 0
|
let openRequestCount = 0
|
||||||
let lastRequest = now()
|
let lastRequest = now()
|
||||||
|
|
||||||
|
// Create shutdown function that can be referenced early
|
||||||
|
const shutdown = () => {
|
||||||
|
dbg(`Shutting down`)
|
||||||
|
shutdownManager.forEach((fn) => fn())
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
/** Mark the instance as starting */
|
/** Mark the instance as starting */
|
||||||
dbg(`Starting instance`)
|
dbg(`Starting instance`)
|
||||||
@ -180,7 +189,7 @@ export const instanceService = mkSingleton(
|
|||||||
const { exitCode, stopped, started, url: internalUrl } = childProcess
|
const { exitCode, stopped, started, url: internalUrl } = childProcess
|
||||||
exitCode.then((code) => {
|
exitCode.then((code) => {
|
||||||
dbg(`Instance exited with code ${code}`)
|
dbg(`Instance exited with code ${code}`)
|
||||||
api?.shutdown()
|
shutdown()
|
||||||
})
|
})
|
||||||
|
|
||||||
shutdownManager.push(() => {
|
shutdownManager.push(() => {
|
||||||
@ -215,7 +224,7 @@ export const instanceService = mkSingleton(
|
|||||||
userInstanceLogger.info(
|
userInstanceLogger.info(
|
||||||
`Instance has been idle for ${DAEMON_PB_IDLE_TTL()}ms. Hibernating to conserve resources.`,
|
`Instance has been idle for ${DAEMON_PB_IDLE_TTL()}ms. Hibernating to conserve resources.`,
|
||||||
)
|
)
|
||||||
api.shutdown()
|
shutdown()
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
dbg(`${openRequestCount} requests remain open`)
|
dbg(`${openRequestCount} requests remain open`)
|
||||||
@ -224,7 +233,8 @@ export const instanceService = mkSingleton(
|
|||||||
}, 1000)
|
}, 1000)
|
||||||
shutdownManager.push(() => clearInterval(idleTid))
|
shutdownManager.push(() => clearInterval(idleTid))
|
||||||
|
|
||||||
const api: InstanceApi = {
|
// Now assign the api object
|
||||||
|
api = {
|
||||||
internalUrl,
|
internalUrl,
|
||||||
startRequest: () => {
|
startRequest: () => {
|
||||||
lastRequest = now()
|
lastRequest = now()
|
||||||
@ -235,10 +245,7 @@ export const instanceService = mkSingleton(
|
|||||||
trace(`ended request (${openRequestCount} still open)`)
|
trace(`ended request (${openRequestCount} still open)`)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
shutdown: () => {
|
shutdown,
|
||||||
dbg(`Shutting down`)
|
|
||||||
shutdownManager.forEach((fn) => fn())
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dbg(`${internalUrl} is running`)
|
dbg(`${internalUrl} is running`)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user