From 03f221f515e137cef84c118037bf8d4db21e8a08 Mon Sep 17 00:00:00 2001 From: Esben Petersen Date: Sun, 13 Aug 2017 19:33:22 +0200 Subject: [PATCH] fix removal bug --- bin/pdf-bot.js | 21 ++++++++++++++++----- src/api.js | 3 ++- test/api.test.js | 38 +++++++++++++++++++++++--------------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/bin/pdf-bot.js b/bin/pdf-bot.js index ec75671..5dd2727 100755 --- a/bin/pdf-bot.js +++ b/bin/pdf-bot.js @@ -75,12 +75,15 @@ program .command('api') .description('Start the API') .action(function (options) { - openConfig() + // We delay initiation of queue. This is because the API will load the DB in memory as + // copy A. When we make changes through the CLI this creates copy B. But next time the + // user pushes to the queue using the API copy A will be persisted again. + var initiateQueue = openConfig(true) var apiOptions = configuration.api var port = apiOptions.port - createApi(queue, { + createApi(initiateQueue, { port: port, token: apiOptions.token }).listen(port, function() { @@ -322,7 +325,7 @@ function processJob(job, configuration) { }) } -function openConfig() { +function openConfig(delayQueueCreation = false) { configuration = defaultConfig if (!program.config) { @@ -354,8 +357,16 @@ function openConfig() { throw new Error('There is no pdf folder in the storage folder. Create it: storage/pdf') } - var queueOptions = configuration.queue - queue = createQueue(path.join(configuration.storagePath, 'db/db.json'), queueOptions.lowDbOptions) + function initiateQueue() { + var queueOptions = configuration.queue + return createQueue(path.join(configuration.storagePath, 'db/db.json'), queueOptions.lowDbOptions) + } + + if (delayQueueCreation) { + return initiateQueue + } else { + queue = initiateQueue() + } } function listJobs(queue, failed = false, limit) { diff --git a/src/api.js b/src/api.js index f62329c..d514f05 100644 --- a/src/api.js +++ b/src/api.js @@ -4,7 +4,7 @@ var bodyParser = require('body-parser') var debug = require('debug')('pdf:api') var error = require('./error') -function createApi(queue, options = {}) { +function createApi(createQueue, options = {}) { var api = express() api.use(bodyParser.json()) @@ -15,6 +15,7 @@ function createApi(queue, options = {}) { } api.post('/', function(req, res) { + var queue = createQueue() var authHeader = req.get('Authorization') if (token && (!authHeader || authHeader.replace(/Bearer (.*)$/i, '$1') !== token)) { diff --git a/test/api.test.js b/test/api.test.js index c298387..5cda931 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -4,12 +4,9 @@ var createApi = require('../src/api') var error = require('../src/error') describe('api: POST /', function () { - var api, queue - beforeEach(function() { - queue = { - addToQueue: sinon.spy() - } - api = createApi(queue, { + var api + beforeEach(function(){ + api = createApi(function(){}, { token: '1234' }) }) @@ -28,15 +25,17 @@ describe('api: POST /', function () { }) it('should return 422 on errorneous responses', function(done) { - queue = { - addToQueue: function() { - return { - code: '001', - error: true + queue = function () { + return { + addToQueue: function() { + return { + code: '001', + error: true + } } } } - api = createApi(queue, { + var api = createApi(queue, { token: '1234' }) @@ -50,8 +49,17 @@ describe('api: POST /', function () { it('should run the queue with the correct params', function (done) { var meta = {id: 1} - queue.addToQueue = sinon.stub() - queue.addToQueue.onCall(0).returns({ id: '1234' }) + var addToQueue = sinon.stub() + addToQueue.onCall(0).returns({ id: '1234' }) + + var queue = function() { + return { + addToQueue: addToQueue + } + } + var api = createApi(queue, { + token: '1234' + }) request(api) .post('/') @@ -61,7 +69,7 @@ describe('api: POST /', function () { .end(function (err, res) { if (err) return done(err) - if (!queue.addToQueue.calledWith({ url: 'https://google.com', meta: meta })) { + if (!addToQueue.calledWith({ url: 'https://google.com', meta: meta })) { throw new Error('Queue was not called with correct url') }