Skip to content

Commit 1e969cf

Browse files
committed
Cleanup on SIGINT/SIGTERM
1 parent e3a0f47 commit 1e969cf

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,7 @@ async function start () {
106106

107107
app.post('/close', async (req, res) => {
108108
try {
109-
await unmountAll(hypermount, db)
110-
console.log('unmounted them all')
111-
await store.close()
112-
await db.close()
113-
server.close()
114-
109+
await cleanup()
115110
res.sendStatus(200)
116111
} catch (err) {
117112
console.error('Close error:', err)
@@ -126,7 +121,17 @@ async function start () {
126121
await store.ready()
127122
await refreshMounts(hypermount, db)
128123

124+
process.once('SIGINT', cleanup)
125+
process.once('SIGTERM', cleanup)
126+
129127
var server = app.listen(argv.port || 3005)
128+
129+
async function cleanup () {
130+
await unmountAll(hypermount, db)
131+
await store.close()
132+
await db.close()
133+
server.close()
134+
}
130135
}
131136

132137
async function mount (hypermount, db, key, mnt, opts) {
@@ -180,7 +185,6 @@ function refreshMounts (hypermount, db) {
180185
db.createReadStream(),
181186
through.obj(({ key, value: record }, enc, cb) => {
182187
if (record.status === Status.UNMOUNTED) {
183-
console.log(`Refreshing mount for ${JSON.stringify(record)}`)
184188
const mountPromise = mount(hypermount, db, record.key, key, record)
185189
mountPromise.then(() => cb(null))
186190
mountPromise.catch(cb)

0 commit comments

Comments
 (0)