Skip to content

Commit

Permalink
feat: trap SIGINT, SIGTERM, SIGQUIT
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Aug 6, 2024
1 parent 9da79ce commit 214be4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 4 additions & 4 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import WikibaseRepo from './../lib/repo.js'
import { pickLanguages, pickKeys } from './../lib/util.js'
import { execCmd } from './../lib/exec.js'
import { listen } from './../lib/signals.js'

;(async () => {
Promise.race([listen('SIGINT', 'SIGTERM', 'SIGQUIT'), (async () => {
const args = process.argv.slice(2)
if (args.length < 3) {
throw new Error(
Expand Down Expand Up @@ -37,10 +38,9 @@ import { execCmd } from './../lib/exec.js'

await targetRepo.createEntities(...data)

return `Sucessfully transferred ${entities.length} entities from ${source} to ${target}.`
})()
console.log(`Sucessfully transferred ${entities.length} entities from ${source} to ${target}.`)
})()])
.then((result) => {
console.log(result)
process.exitCode = 0
})
.catch((err) => {
Expand Down
9 changes: 9 additions & 0 deletions lib/signals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const listen = (...signals) => {
return Promise.race(signals.map((s) => {
return new Promise((resolve, reject) => {
process.on(s, () => {
reject(new Error(`Received ${s}, program will exit before finishing.`))
})
})
}))
}

0 comments on commit 214be4d

Please sign in to comment.