Skip to content

Commit

Permalink
add busy flag
Browse files Browse the repository at this point in the history
  • Loading branch information
esbenp committed Oct 17, 2017
1 parent 41b3f38 commit eb6b753
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
24 changes: 19 additions & 5 deletions bin/pdf-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ program
.action(function (url) {
openConfig()

var isBusy = queue.isBusy()
if (isBusy) {
return
}

var maxTries = configuration.queue.generationMaxTries
var retryStrategy = configuration.queue.generationRetryStrategy
var parallelism = configuration.queue.parallelism
Expand All @@ -319,25 +324,34 @@ program
if (jobs.length > 0) {
var chunks = chunk(jobs, parallelism)

function runNextChunk(i = 1) {
function runNextChunk(k = 1) {
if (chunks.length === 0) {
queue.setIsBusy(false)
process.exit(0)
} else {
var chunk = chunks.shift()
console.log('Running chunk %s, %s chunks left', i, chunks.length)
console.log('Running chunk %s, %s chunks left', k, chunks.length)

var promises = []
for(var i in chunk) {
promises.push(processJob(chunk[i], configuration, false))
}

Promise.all(promises).then(function(){
runNextChunk(i + 1)
})
Promise.all(promises)
.then(function(){
runNextChunk(k + 1)
})
.catch(function(){
queue.setIsBusy(false)
process.exit(1)
})
}
}

console.log('Found %s jobs, divided into %s chunks', jobs.length, chunks.length)

queue.setIsBusy(true)

runNextChunk()
}
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pdf-bot",
"version": "0.4.1",
"version": "0.4.2",
"author": "Esben Petersen <esbenspetersen@gmail.com>",
"homepage": "https://github.com/esbenp/pdf-bot",
"license": "MIT",
Expand Down
12 changes: 11 additions & 1 deletion src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ function createQueue (path, options = {}, initialValue = []) {
getNext: createQueueMethod(getNext),
getAllUnfinished: createQueueMethod(getAllUnfinished),
getNextWithoutSuccessfulPing: createQueueMethod(getNextWithoutSuccessfulPing),
isBusy: createQueueMethod(isBusy),
processJob: createQueueMethod(processJob),
purge: createQueueMethod(purge)
purge: createQueueMethod(purge),
setIsBusy: createQueueMethod(setIsBusy)
}
}

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

function isBusy (db) {
return db.get('is_busy').value() || false
}

function purge (db, failed = false, pristine = false, maxTries = 5) {
var query = db.get('queue').slice(0)

Expand Down Expand Up @@ -199,6 +205,10 @@ function purge (db, failed = false, pristine = false, maxTries = 5) {
}
}

function setIsBusy(db, isBusy) {
return db.set('is_busy', isBusy).write()
}

// ==========
// PROCESSING
// ==========
Expand Down

0 comments on commit eb6b753

Please sign in to comment.