Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 70 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"@livingdata/tabular-data-helpers": "^0.0.13",
"addok-cluster": "^0.9.0",
"addok-cluster": "^0.10.0",
"addok-geocode-stream": "^0.26.0",
"content-disposition": "^0.5.4",
"cors": "^2.8.5",
Expand Down
31 changes: 29 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import routes from './lib/routes.js'
const PORT = process.env.PORT || 5000

const app = express()
const cluster = await createCluster()

const cluster = await createCluster({
onTerminate(reason) {
console.log(`Cluster terminated: ${reason}`)
process.exit(0)
}
})

app.disable('x-powered-by')

Expand All @@ -25,6 +31,27 @@ app.use(cors({origin: true}))

app.use('/', routes(cluster))

app.listen(PORT, () => {
const httpServer = app.listen(PORT, () => {
console.log(`Start listening on port ${PORT}`)
})

// Graceful shutdown on SIGTERM and SIGINT
async function handleShutdown(signal) {
console.log(`Received ${signal}, gracefully shutting down...`)

// Close HTTP server first
httpServer.close(() => {
console.log('Server closed')
})

// Then terminate the cluster
try {
await cluster.end()
} catch (error) {
console.error('Error during cluster shutdown:', error)
process.exit(1)
}
}

process.on('SIGTERM', () => handleShutdown('SIGTERM'))
process.on('SIGINT', () => handleShutdown('SIGINT'))
46 changes: 46 additions & 0 deletions test/cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import test from 'ava'

test('cluster mock / should support end method', async t => {
const cluster = {
geocode() {
return []
},
reverse() {
return []
},
end() {
return Promise.resolve()
}
}

t.is(typeof cluster.end, 'function')
await t.notThrowsAsync(async () => {
await cluster.end()
})
})

test('cluster mock / end should be callable when cluster is terminated', async t => {
let terminated = false

const cluster = {
geocode() {
if (terminated) {
throw new Error('Cluster terminated')
}

return []
},
end() {
terminated = true
return Promise.resolve()
}
}

await cluster.end()
t.true(terminated)

await t.throwsAsync(
async () => cluster.geocode({}),
{message: 'Cluster terminated'}
)
})