2017-05-15 15:22:03 -05:00
|
|
|
import { forever, queue } from 'async'
|
2017-06-10 15:15:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
JOBS_FETCHING_INTERVAL,
|
|
|
|
JOBS_FETCH_LIMIT_PER_CYCLE,
|
|
|
|
JOB_STATES
|
|
|
|
} from '../../initializers'
|
|
|
|
import { logger } from '../../helpers'
|
2017-06-10 15:15:25 -05:00
|
|
|
import { JobInstance } from '../../models'
|
|
|
|
import { JobHandler, jobHandlers } from './handlers'
|
|
|
|
|
|
|
|
type JobQueueCallback = (err: Error) => void
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
class JobScheduler {
|
|
|
|
|
|
|
|
private static instance: JobScheduler
|
|
|
|
|
|
|
|
private constructor () { }
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
|
|
|
|
activate () {
|
|
|
|
const limit = JOBS_FETCH_LIMIT_PER_CYCLE
|
|
|
|
|
|
|
|
logger.info('Jobs scheduler activated.')
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
const jobsQueue = queue<JobInstance, JobQueueCallback>(this.processJob.bind(this))
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
// Finish processing jobs from a previous start
|
|
|
|
const state = JOB_STATES.PROCESSING
|
2017-07-05 06:26:25 -05:00
|
|
|
db.Job.listWithLimit(limit, state)
|
|
|
|
.then(jobs => {
|
|
|
|
this.enqueueJobs(jobsQueue, jobs)
|
|
|
|
|
|
|
|
forever(
|
|
|
|
next => {
|
|
|
|
if (jobsQueue.length() !== 0) {
|
|
|
|
// Finish processing the queue first
|
|
|
|
return setTimeout(next, JOBS_FETCHING_INTERVAL)
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
const state = JOB_STATES.PENDING
|
|
|
|
db.Job.listWithLimit(limit, state)
|
|
|
|
.then(jobs => {
|
|
|
|
this.enqueueJobs(jobsQueue, jobs)
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
// Optimization: we could use "drain" from queue object
|
|
|
|
return setTimeout(next, JOBS_FETCHING_INTERVAL)
|
|
|
|
})
|
|
|
|
.catch(err => logger.error('Cannot list pending jobs.', { error: err }))
|
|
|
|
},
|
|
|
|
|
|
|
|
err => logger.error('Error in job scheduler queue.', { error: err })
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.catch(err => logger.error('Cannot list pending jobs.', { error: err }))
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
createJob (transaction: Sequelize.Transaction, handlerName: string, handlerInputData: object) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const createQuery = {
|
|
|
|
state: JOB_STATES.PENDING,
|
|
|
|
handlerName,
|
|
|
|
handlerInputData
|
|
|
|
}
|
|
|
|
const options = { transaction }
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.Job.create(createQuery, options)
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
private enqueueJobs (jobsQueue: AsyncQueue<JobInstance>, jobs: JobInstance[]) {
|
|
|
|
jobs.forEach(job => jobsQueue.push(job))
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
private processJob (job: JobInstance, callback: (err: Error) => void) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const jobHandler = jobHandlers[job.handlerName]
|
2017-07-05 06:26:25 -05:00
|
|
|
if (jobHandler === undefined) {
|
|
|
|
logger.error('Unknown job handler for job %s.', job.handlerName)
|
|
|
|
return callback(null)
|
|
|
|
}
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
logger.info('Processing job %d with handler %s.', job.id, job.handlerName)
|
|
|
|
|
|
|
|
job.state = JOB_STATES.PROCESSING
|
2017-07-05 06:26:25 -05:00
|
|
|
return job.save()
|
|
|
|
.then(() => {
|
|
|
|
return jobHandler.process(job.handlerInputData)
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
result => {
|
|
|
|
return this.onJobSuccess(jobHandler, job, result)
|
|
|
|
},
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
err => {
|
2017-05-15 15:22:03 -05:00
|
|
|
logger.error('Error in job handler %s.', job.handlerName, { error: err })
|
2017-07-05 06:26:25 -05:00
|
|
|
return this.onJobError(jobHandler, job, err)
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
2017-07-05 06:26:25 -05:00
|
|
|
)
|
|
|
|
.then(() => callback(null))
|
|
|
|
.catch(err => {
|
|
|
|
this.cannotSaveJobError(err)
|
|
|
|
return callback(err)
|
2017-05-15 15:22:03 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
private onJobError (jobHandler: JobHandler<any>, job: JobInstance, err: Error) {
|
2017-05-15 15:22:03 -05:00
|
|
|
job.state = JOB_STATES.ERROR
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return job.save()
|
|
|
|
.then(() => jobHandler.onError(err, job.id))
|
|
|
|
.catch(err => this.cannotSaveJobError(err))
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
private onJobSuccess (jobHandler: JobHandler<any>, job: JobInstance, jobResult: any) {
|
2017-05-15 15:22:03 -05:00
|
|
|
job.state = JOB_STATES.SUCCESS
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return job.save()
|
|
|
|
.then(() => jobHandler.onSuccess(job.id, jobResult))
|
|
|
|
.catch(err => this.cannotSaveJobError(err))
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
private cannotSaveJobError (err: Error) {
|
2017-05-15 15:22:03 -05:00
|
|
|
logger.error('Cannot save new job state.', { error: err })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
JobScheduler
|
|
|
|
}
|