2017-10-09 07:55:13 -05:00
|
|
|
import { AsyncQueue, forever, queue } from 'async'
|
2017-06-10 15:15:25 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-11-10 10:27:49 -06:00
|
|
|
import { JobCategory } from '../../../shared'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { logger } from '../../helpers'
|
2017-11-10 10:27:49 -06:00
|
|
|
import { database as db, JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers'
|
2017-06-10 15:15:25 -05:00
|
|
|
import { JobInstance } from '../../models'
|
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
export interface JobHandler<P, T> {
|
|
|
|
process (data: object, jobId: number): Promise<T>
|
2017-11-09 10:51:58 -06:00
|
|
|
onError (err: Error, jobId: number)
|
2017-11-10 10:27:49 -06:00
|
|
|
onSuccess (jobId: number, jobResult: T, jobScheduler: JobScheduler<P, T>)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
2017-06-10 15:15:25 -05:00
|
|
|
type JobQueueCallback = (err: Error) => void
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
class JobScheduler<P, T> {
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
constructor (
|
|
|
|
private jobCategory: JobCategory,
|
2017-11-10 10:27:49 -06:00
|
|
|
private jobHandlers: { [ id: string ]: JobHandler<P, T> }
|
2017-11-09 10:51:58 -06:00
|
|
|
) {}
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
async activate () {
|
2017-11-09 10:51:58 -06:00
|
|
|
const limit = JOBS_FETCH_LIMIT_PER_CYCLE[this.jobCategory]
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
logger.info('Jobs scheduler %s activated.', this.jobCategory)
|
2017-05-15 15:22:03 -05:00
|
|
|
|
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-10-25 09:03:33 -05:00
|
|
|
try {
|
2017-11-09 10:51:58 -06:00
|
|
|
const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory)
|
2017-10-25 09:03:33 -05:00
|
|
|
|
|
|
|
this.enqueueJobs(jobsQueue, jobs)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot list pending jobs.', err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forever(
|
|
|
|
async next => {
|
|
|
|
if (jobsQueue.length() !== 0) {
|
|
|
|
// Finish processing the queue first
|
|
|
|
return setTimeout(next, JOBS_FETCHING_INTERVAL)
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = JOB_STATES.PENDING
|
|
|
|
try {
|
2017-11-09 10:51:58 -06:00
|
|
|
const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory)
|
2017-10-25 09:03:33 -05:00
|
|
|
|
|
|
|
this.enqueueJobs(jobsQueue, jobs)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot list pending jobs.', err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization: we could use "drain" from queue object
|
|
|
|
return setTimeout(next, JOBS_FETCHING_INTERVAL)
|
|
|
|
},
|
|
|
|
|
|
|
|
err => logger.error('Error in job scheduler queue.', err)
|
|
|
|
)
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
createJob (transaction: Sequelize.Transaction, handlerName: string, handlerInputData: P) {
|
2017-05-15 15:22:03 -05:00
|
|
|
const createQuery = {
|
|
|
|
state: JOB_STATES.PENDING,
|
2017-11-10 10:27:49 -06:00
|
|
|
category: this.jobCategory,
|
2017-05-15 15:22:03 -05:00
|
|
|
handlerName,
|
|
|
|
handlerInputData
|
|
|
|
}
|
2017-11-10 10:27:49 -06:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
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-10-25 09:03:33 -05:00
|
|
|
private async processJob (job: JobInstance, callback: (err: Error) => void) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const jobHandler = this.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-10-25 09:03:33 -05:00
|
|
|
await job.save()
|
|
|
|
|
|
|
|
try {
|
2017-11-10 10:27:49 -06:00
|
|
|
const result: T = await jobHandler.process(job.handlerInputData, job.id)
|
2017-10-25 09:03:33 -05:00
|
|
|
await this.onJobSuccess(jobHandler, job, result)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Error in job handler %s.', job.handlerName, err)
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.onJobError(jobHandler, job, err)
|
|
|
|
} catch (innerErr) {
|
|
|
|
this.cannotSaveJobError(innerErr)
|
|
|
|
return callback(innerErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null)
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
private async onJobError (jobHandler: JobHandler<P, T>, job: JobInstance, err: Error) {
|
2017-05-15 15:22:03 -05:00
|
|
|
job.state = JOB_STATES.ERROR
|
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
try {
|
|
|
|
await job.save()
|
|
|
|
await jobHandler.onError(err, job.id)
|
|
|
|
} catch (err) {
|
|
|
|
this.cannotSaveJobError(err)
|
|
|
|
}
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
private async onJobSuccess (jobHandler: JobHandler<P, T>, job: JobInstance, jobResult: T) {
|
2017-05-15 15:22:03 -05:00
|
|
|
job.state = JOB_STATES.SUCCESS
|
|
|
|
|
2017-10-25 09:03:33 -05:00
|
|
|
try {
|
|
|
|
await job.save()
|
2017-11-10 10:27:49 -06:00
|
|
|
jobHandler.onSuccess(job.id, jobResult, this)
|
2017-10-25 09:03:33 -05:00
|
|
|
} 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-07-07 11:26:12 -05:00
|
|
|
logger.error('Cannot save new job state.', err)
|
2017-05-15 15:22:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
JobScheduler
|
|
|
|
}
|