Skip to content

Commit 03f221f

Browse files
committed
fix removal bug
1 parent 4771ebf commit 03f221f

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

bin/pdf-bot.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,15 @@ program
7575
.command('api')
7676
.description('Start the API')
7777
.action(function (options) {
78-
openConfig()
78+
// We delay initiation of queue. This is because the API will load the DB in memory as
79+
// copy A. When we make changes through the CLI this creates copy B. But next time the
80+
// user pushes to the queue using the API copy A will be persisted again.
81+
var initiateQueue = openConfig(true)
7982

8083
var apiOptions = configuration.api
8184
var port = apiOptions.port
8285

83-
createApi(queue, {
86+
createApi(initiateQueue, {
8487
port: port,
8588
token: apiOptions.token
8689
}).listen(port, function() {
@@ -322,7 +325,7 @@ function processJob(job, configuration) {
322325
})
323326
}
324327

325-
function openConfig() {
328+
function openConfig(delayQueueCreation = false) {
326329
configuration = defaultConfig
327330

328331
if (!program.config) {
@@ -354,8 +357,16 @@ function openConfig() {
354357
throw new Error('There is no pdf folder in the storage folder. Create it: storage/pdf')
355358
}
356359

357-
var queueOptions = configuration.queue
358-
queue = createQueue(path.join(configuration.storagePath, 'db/db.json'), queueOptions.lowDbOptions)
360+
function initiateQueue() {
361+
var queueOptions = configuration.queue
362+
return createQueue(path.join(configuration.storagePath, 'db/db.json'), queueOptions.lowDbOptions)
363+
}
364+
365+
if (delayQueueCreation) {
366+
return initiateQueue
367+
} else {
368+
queue = initiateQueue()
369+
}
359370
}
360371

361372
function listJobs(queue, failed = false, limit) {

src/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var bodyParser = require('body-parser')
44
var debug = require('debug')('pdf:api')
55
var error = require('./error')
66

7-
function createApi(queue, options = {}) {
7+
function createApi(createQueue, options = {}) {
88
var api = express()
99
api.use(bodyParser.json())
1010

@@ -15,6 +15,7 @@ function createApi(queue, options = {}) {
1515
}
1616

1717
api.post('/', function(req, res) {
18+
var queue = createQueue()
1819
var authHeader = req.get('Authorization')
1920

2021
if (token && (!authHeader || authHeader.replace(/Bearer (.*)$/i, '$1') !== token)) {

test/api.test.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ var createApi = require('../src/api')
44
var error = require('../src/error')
55

66
describe('api: POST /', function () {
7-
var api, queue
8-
beforeEach(function() {
9-
queue = {
10-
addToQueue: sinon.spy()
11-
}
12-
api = createApi(queue, {
7+
var api
8+
beforeEach(function(){
9+
api = createApi(function(){}, {
1310
token: '1234'
1411
})
1512
})
@@ -28,15 +25,17 @@ describe('api: POST /', function () {
2825
})
2926

3027
it('should return 422 on errorneous responses', function(done) {
31-
queue = {
32-
addToQueue: function() {
33-
return {
34-
code: '001',
35-
error: true
28+
queue = function () {
29+
return {
30+
addToQueue: function() {
31+
return {
32+
code: '001',
33+
error: true
34+
}
3635
}
3736
}
3837
}
39-
api = createApi(queue, {
38+
var api = createApi(queue, {
4039
token: '1234'
4140
})
4241

@@ -50,8 +49,17 @@ describe('api: POST /', function () {
5049
it('should run the queue with the correct params', function (done) {
5150
var meta = {id: 1}
5251

53-
queue.addToQueue = sinon.stub()
54-
queue.addToQueue.onCall(0).returns({ id: '1234' })
52+
var addToQueue = sinon.stub()
53+
addToQueue.onCall(0).returns({ id: '1234' })
54+
55+
var queue = function() {
56+
return {
57+
addToQueue: addToQueue
58+
}
59+
}
60+
var api = createApi(queue, {
61+
token: '1234'
62+
})
5563

5664
request(api)
5765
.post('/')
@@ -61,7 +69,7 @@ describe('api: POST /', function () {
6169
.end(function (err, res) {
6270
if (err) return done(err)
6371

64-
if (!queue.addToQueue.calledWith({ url: 'https://google.com', meta: meta })) {
72+
if (!addToQueue.calledWith({ url: 'https://google.com', meta: meta })) {
6573
throw new Error('Queue was not called with correct url')
6674
}
6775

0 commit comments

Comments
 (0)