graceful shutdown
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
@@ -3,6 +3,7 @@ import http from 'node:http'
|
|||||||
import {SparkWallet} from '@buildonspark/spark-sdk'
|
import {SparkWallet} from '@buildonspark/spark-sdk'
|
||||||
|
|
||||||
const PORT = parseInt(process.env.SPARK_SIDECAR_PORT || '8765', 10)
|
const PORT = parseInt(process.env.SPARK_SIDECAR_PORT || '8765', 10)
|
||||||
|
const HOST = process.env.SPARK_SIDECAR_HOST || '127.0.0.1'
|
||||||
const API_KEY = process.env.SPARK_SIDECAR_API_KEY || ''
|
const API_KEY = process.env.SPARK_SIDECAR_API_KEY || ''
|
||||||
const MNEMONIC = process.env.SPARK_MNEMONIC || ''
|
const MNEMONIC = process.env.SPARK_MNEMONIC || ''
|
||||||
const NETWORK = process.env.SPARK_NETWORK || 'MAINNET'
|
const NETWORK = process.env.SPARK_NETWORK || 'MAINNET'
|
||||||
@@ -27,6 +28,26 @@ async function getWallet() {
|
|||||||
return walletPromise
|
return walletPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function shutdown() {
|
||||||
|
try {
|
||||||
|
if (walletPromise) {
|
||||||
|
const wallet = await walletPromise
|
||||||
|
if (wallet && typeof wallet.cleanupConnections === 'function') {
|
||||||
|
await wallet.cleanupConnections()
|
||||||
|
} else if (wallet && typeof wallet.cleanup === 'function') {
|
||||||
|
wallet.cleanup()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error during Spark sidecar shutdown:', error)
|
||||||
|
} finally {
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
process.on('SIGINT', shutdown)
|
||||||
|
process.on('SIGTERM', shutdown)
|
||||||
|
|
||||||
function sendJson(res, statusCode, payload) {
|
function sendJson(res, statusCode, payload) {
|
||||||
res.writeHead(statusCode, {'content-type': 'application/json'})
|
res.writeHead(statusCode, {'content-type': 'application/json'})
|
||||||
res.end(JSON.stringify(payload))
|
res.end(JSON.stringify(payload))
|
||||||
@@ -170,6 +191,14 @@ const server = http.createServer(async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, HOST, () => {
|
||||||
console.log(`Spark sidecar listening on :${PORT}`)
|
console.log(`Spark sidecar listening on ${HOST}:${PORT}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
server.on('error', err => {
|
||||||
|
if (err && err.code === 'EADDRINUSE') {
|
||||||
|
console.error(`Spark sidecar port ${HOST}:${PORT} already in use.`)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
throw err
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user