Skip to content

Commit eb6b753

Browse files
committed
add busy flag
1 parent 41b3f38 commit eb6b753

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

bin/pdf-bot.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ program
310310
.action(function (url) {
311311
openConfig()
312312

313+
var isBusy = queue.isBusy()
314+
if (isBusy) {
315+
return
316+
}
317+
313318
var maxTries = configuration.queue.generationMaxTries
314319
var retryStrategy = configuration.queue.generationRetryStrategy
315320
var parallelism = configuration.queue.parallelism
@@ -319,25 +324,34 @@ program
319324
if (jobs.length > 0) {
320325
var chunks = chunk(jobs, parallelism)
321326

322-
function runNextChunk(i = 1) {
327+
function runNextChunk(k = 1) {
323328
if (chunks.length === 0) {
329+
queue.setIsBusy(false)
324330
process.exit(0)
325331
} else {
326332
var chunk = chunks.shift()
327-
console.log('Running chunk %s, %s chunks left', i, chunks.length)
333+
console.log('Running chunk %s, %s chunks left', k, chunks.length)
328334

329335
var promises = []
330336
for(var i in chunk) {
331337
promises.push(processJob(chunk[i], configuration, false))
332338
}
333339

334-
Promise.all(promises).then(function(){
335-
runNextChunk(i + 1)
336-
})
340+
Promise.all(promises)
341+
.then(function(){
342+
runNextChunk(k + 1)
343+
})
344+
.catch(function(){
345+
queue.setIsBusy(false)
346+
process.exit(1)
347+
})
337348
}
338349
}
339350

340351
console.log('Found %s jobs, divided into %s chunks', jobs.length, chunks.length)
352+
353+
queue.setIsBusy(true)
354+
341355
runNextChunk()
342356
}
343357
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pdf-bot",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"author": "Esben Petersen <[email protected]>",
55
"homepage": "https://github.com/esbenp/pdf-bot",
66
"license": "MIT",

src/queue.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ function createQueue (path, options = {}, initialValue = []) {
2828
getNext: createQueueMethod(getNext),
2929
getAllUnfinished: createQueueMethod(getAllUnfinished),
3030
getNextWithoutSuccessfulPing: createQueueMethod(getNextWithoutSuccessfulPing),
31+
isBusy: createQueueMethod(isBusy),
3132
processJob: createQueueMethod(processJob),
32-
purge: createQueueMethod(purge)
33+
purge: createQueueMethod(purge),
34+
setIsBusy: createQueueMethod(setIsBusy)
3335
}
3436
}
3537

@@ -170,6 +172,10 @@ function getNextWithoutSuccessfulPing (db, shouldWait, maxTries = 5) {
170172
.value()[0]
171173
}
172174

175+
function isBusy (db) {
176+
return db.get('is_busy').value() || false
177+
}
178+
173179
function purge (db, failed = false, pristine = false, maxTries = 5) {
174180
var query = db.get('queue').slice(0)
175181

@@ -199,6 +205,10 @@ function purge (db, failed = false, pristine = false, maxTries = 5) {
199205
}
200206
}
201207

208+
function setIsBusy(db, isBusy) {
209+
return db.set('is_busy', isBusy).write()
210+
}
211+
202212
// ==========
203213
// PROCESSING
204214
// ==========

0 commit comments

Comments
 (0)