From 43e00cc4197d63e48750d903e7d9e74f54126501 Mon Sep 17 00:00:00 2001 From: Esben Petersen Date: Mon, 13 Nov 2017 14:02:45 +0100 Subject: [PATCH] make sure to close connections --- bin/pdf-bot.js | 23 +++++++++++++++++++++++ package.json | 2 +- src/db/lowdb.js | 5 +++++ src/db/pgsql.js | 5 +++++ src/queue.js | 5 +++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/bin/pdf-bot.js b/bin/pdf-bot.js index b020407..a3b3e4c 100755 --- a/bin/pdf-bot.js +++ b/bin/pdf-bot.js @@ -176,6 +176,7 @@ program return db.migrate() .then(function () { console.log('The database was migrated') + db.close() process.exit(0) }) .catch(handleDbError) @@ -204,6 +205,7 @@ program db.destroy() .then(function() { console.log('The database has been destroyed.') + db.close() process.exit(0) }) .catch(handleDbError) @@ -221,6 +223,7 @@ program .then(function (job) { if (!job) { console.error('Job not found') + queue.close() process.exit(1) } @@ -240,6 +243,7 @@ program return listJobs(queue, options.failed, options.completed, options.limit) .then(function() { + queue.close() process.exit(0) }) .catch(handleDbError) @@ -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 { @@ -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 { @@ -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) } @@ -327,6 +338,7 @@ program } console.log(table.toString()) + queue.close() process.exit(0) }) .catch(handleDbError) @@ -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) }) @@ -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) @@ -384,6 +399,7 @@ program return queue.getNext(retryStrategy, maxTries) .then(function (next) { if (!next) { + queue.close() process.exit(0) } @@ -401,6 +417,7 @@ program return queue.isBusy() .then(function (isBusy) { if (isBusy) { + queue.close() process.exit(0) } @@ -411,6 +428,7 @@ program return queue.getAllUnfinished(retryStrategy, maxTries) .then(function (jobs) { if (jobs.length === 0) { + queue.close() process.exit(0) } @@ -419,6 +437,7 @@ program function runNextChunk(k = 1) { if (chunks.length === 0) { queue.setIsBusy(false).then(function() { + queue.close() process.exit(0) }) } else { @@ -436,6 +455,7 @@ program }) .catch(function(){ return queue.setIsBusy(false).then(function() { + queue.close() process.exit(1) }) }) @@ -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) } } @@ -575,5 +597,6 @@ function formatDate(input) { function handleDbError(e) { console.error(e) + queue.close() process.exit(1) } diff --git a/package.json b/package.json index 25e47d1..151cb22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pdf-bot", - "version": "0.5.0", + "version": "0.5.1", "author": "Esben Petersen ", "homepage": "https://github.com/esbenp/pdf-bot", "license": "MIT", diff --git a/src/db/lowdb.js b/src/db/lowdb.js index 5788e50..3cb1473 100644 --- a/src/db/lowdb.js +++ b/src/db/lowdb.js @@ -24,6 +24,7 @@ function createLowDb(options = {}) { } return { + close: createDbMethod(close), getAllUnfinished: createDbMethod(getAllUnfinished), getById: createDbMethod(getById), getList: createDbMethod(getList), @@ -51,6 +52,10 @@ function pushToQueue (db, data) { return data } +function close() { + return true +} + function getAllUnfinished (db, shouldWait, maxTries = 5) { return db .get('queue') diff --git a/src/db/pgsql.js b/src/db/pgsql.js index 164cf19..feee93e 100644 --- a/src/db/pgsql.js +++ b/src/db/pgsql.js @@ -23,6 +23,7 @@ function createPostgresDb(options = {}) { } return { + close: createDbMethod(close), destroy: createDbMethod(destroy), getAllUnfinished: createDbMethod(getAllUnfinished), getById: createDbMethod(getById), @@ -43,6 +44,10 @@ function createPostgresDb(options = {}) { module.exports = createPostgresDb +function close (db) { + db.end() +} + function pushToQueue (db, data) { return db .query( diff --git a/src/queue.js b/src/queue.js index 4cedb59..d88347c 100644 --- a/src/queue.js +++ b/src/queue.js @@ -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), @@ -57,6 +58,10 @@ function addToQueue (db, data) { return db.pushToQueue(data) } +function close(db) { + return db.close() +} + // ========= // RETRIEVAL // =========