Skip to content

Instantly share code, notes, and snippets.

@cggaurav
Created January 15, 2018 07:44
Show Gist options
  • Save cggaurav/570d2722a6a5aa7d317aa5203610dc82 to your computer and use it in GitHub Desktop.
Save cggaurav/570d2722a6a5aa7d317aa5203610dc82 to your computer and use it in GitHub Desktop.
A simple worker
function worker (key, options = {}, work) {
global.queues = global.queues || {}
global.queues[key] = global.queues[key] || []
global.queues[`${key}:active`] = global.queues[`${key}:active`] || []
global.queues[`${key}:completed`] = global.queues[`${key}:completed`] || []
global.queues[`${key}:failed`] = global.queues[`${key}:failed`] || []
const queue = global.queues[key]
this.jobs = this.jobs || 0
const MAX_JOBS = options.concurrency || 1
const status = (job, status) => {
const queue = (!status || status === 'pending') ? global.queues['process'] : global.queues[`process:${status}`]
const isStatus = ((queue || []).indexOf(job) > -1)
return isStatus
}
const active = (job, status) => {
const queue = global.queues['process:active']
if (status === true) {
if (status(job, 'active')) {
queue.push(job)
}
} else if (status === false) {
delete queue[queue.indexOf(job)]
}
}
const done = (error, job) => {
this.jobs--
if (!job) return
active(job, false)
const queue = error ? global.queues['process:failed'] : global.queues['process:successful']
queue.push({...job, error})
}
setInterval(() => {
if (this.jobs <= MAX_JOBS) {
this.jobs++
const job = queue.pop()
if (job) {
active(job, true)
work({queue, job, active, done})
} else {
done()
}
}
}, 1000)
return {
push: (job) => {
const exist = this.isComplete(job) || this.isPending(job)
if (!exist) {
queue.push(job)
}
return exist
},
isPending: (job) => {
return status(job, 'pending')
},
isActive: (job) => {
return status(job, 'active')
},
isSuccessful: (job) => {
return status(job, 'successful')
},
isFailed: (job) => {
return status(job, 'failed')
},
isComplete: (job) => {
return this.isSuccessful(job) || this.isFailed(job)
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment