Skip to content

Commit

Permalink
make sure to close connections
Browse files Browse the repository at this point in the history
  • Loading branch information
esbenp committed Nov 13, 2017
1 parent 8b050b6 commit 43e00cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
23 changes: 23 additions & 0 deletions bin/pdf-bot.js
Expand Up @@ -176,6 +176,7 @@ program
return db.migrate()
.then(function () {
console.log('The database was migrated')
db.close()
process.exit(0)
})
.catch(handleDbError)
Expand Down Expand Up @@ -204,6 +205,7 @@ program
db.destroy()
.then(function() {
console.log('The database has been destroyed.')
db.close()
process.exit(0)
})
.catch(handleDbError)
Expand All @@ -221,6 +223,7 @@ program
.then(function (job) {
if (!job) {
console.error('Job not found')
queue.close()
process.exit(1)
}

Expand All @@ -240,6 +243,7 @@ program

return listJobs(queue, options.failed, options.completed, options.limit)
.then(function() {
queue.close()
process.exit(0)
})
.catch(handleDbError)
Expand All @@ -254,11 +258,14 @@ program
return queue.getById(jobId)
.then(function (job) {
if (!job) {
queue.close()
console.log('Job not found.')
return;
}

return ping(job, configuration.webhook).then(response => {
queue.close()

if (response.error) {
process.exit(1)
} else {
Expand All @@ -280,10 +287,13 @@ program
queue.getNextWithoutSuccessfulPing(retryStrategy, maxTries)
.then(function (next) {
if (!next) {
queue.close()
process.exit(0)
}

return ping(next, configuration.webhook).then(function (response) {
queue.close()

if (response.error) {
process.exit(1)
} else {
Expand All @@ -303,6 +313,7 @@ program
var job = queue.getById(jobId)
.then(function (job) {
if (!job) {
queue.close()
console.log('Job not found')
process.exit(1)
}
Expand All @@ -327,6 +338,7 @@ program
}

console.log(table.toString())
queue.close()
process.exit(0)
})
.catch(handleDbError)
Expand All @@ -342,6 +354,7 @@ program

return queue.purge(options.failed, options.new)
.then(function () {
queue.close()
console.log('The queue was purged.')
process.exit(0)
})
Expand All @@ -361,6 +374,8 @@ program
meta: JSON.parse(options.meta || '{}')
})
.then(function (response) {
queue.close()

if (error.isError(response)) {
console.error('Could not push to queue: %s', response.message)
process.exit(1)
Expand All @@ -384,6 +399,7 @@ program
return queue.getNext(retryStrategy, maxTries)
.then(function (next) {
if (!next) {
queue.close()
process.exit(0)
}

Expand All @@ -401,6 +417,7 @@ program
return queue.isBusy()
.then(function (isBusy) {
if (isBusy) {
queue.close()
process.exit(0)
}

Expand All @@ -411,6 +428,7 @@ program
return queue.getAllUnfinished(retryStrategy, maxTries)
.then(function (jobs) {
if (jobs.length === 0) {
queue.close()
process.exit(0)
}

Expand All @@ -419,6 +437,7 @@ program
function runNextChunk(k = 1) {
if (chunks.length === 0) {
queue.setIsBusy(false).then(function() {
queue.close()
process.exit(0)
})
} else {
Expand All @@ -436,6 +455,7 @@ program
})
.catch(function(){
return queue.setIsBusy(false).then(function() {
queue.close()
process.exit(1)
})
})
Expand Down Expand Up @@ -468,11 +488,13 @@ function processJob(job, configuration, exitProcess = true) {
if (error.isError(response)) {
console.error(response.message)
if (exitProcess) {
queue.close()
process.exit(1)
}
} else {
console.log('Job ID ' + job.id + ' was processed.')
if (exitProcess) {
queue.close()
process.exit(0)
}
}
Expand Down Expand Up @@ -575,5 +597,6 @@ function formatDate(input) {

function handleDbError(e) {
console.error(e)
queue.close()
process.exit(1)
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pdf-bot",
"version": "0.5.0",
"version": "0.5.1",
"author": "Esben Petersen <esbenspetersen@gmail.com>",
"homepage": "https://github.com/esbenp/pdf-bot",
"license": "MIT",
Expand Down
5 changes: 5 additions & 0 deletions src/db/lowdb.js
Expand Up @@ -24,6 +24,7 @@ function createLowDb(options = {}) {
}

return {
close: createDbMethod(close),
getAllUnfinished: createDbMethod(getAllUnfinished),
getById: createDbMethod(getById),
getList: createDbMethod(getList),
Expand Down Expand Up @@ -51,6 +52,10 @@ function pushToQueue (db, data) {
return data
}

function close() {
return true
}

function getAllUnfinished (db, shouldWait, maxTries = 5) {
return db
.get('queue')
Expand Down
5 changes: 5 additions & 0 deletions src/db/pgsql.js
Expand Up @@ -23,6 +23,7 @@ function createPostgresDb(options = {}) {
}

return {
close: createDbMethod(close),
destroy: createDbMethod(destroy),
getAllUnfinished: createDbMethod(getAllUnfinished),
getById: createDbMethod(getById),
Expand All @@ -43,6 +44,10 @@ function createPostgresDb(options = {}) {

module.exports = createPostgresDb

function close (db) {
db.end()
}

function pushToQueue (db, data) {
return db
.query(
Expand Down
5 changes: 5 additions & 0 deletions src/queue.js
Expand Up @@ -15,6 +15,7 @@ function createQueue (db, options = {}) {
return {
addToQueue: createQueueMethod(addToQueue),
attemptPing: createQueueMethod(attemptPing),
close: createQueueMethod(close),
getById: createQueueMethod(getById),
getList: createQueueMethod(getList),
getNext: createQueueMethod(getNext),
Expand Down Expand Up @@ -57,6 +58,10 @@ function addToQueue (db, data) {
return db.pushToQueue(data)
}

function close(db) {
return db.close()
}

// =========
// RETRIEVAL
// =========
Expand Down

0 comments on commit 43e00cc

Please sign in to comment.