2017-10-09 08:55:13 -04:00
|
|
|
import { AsyncQueue, forever, queue } from 'async'
|
2017-06-10 16:15:25 -04:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-11-10 11:27:49 -05:00
|
|
|
import { JobCategory } from '../../../shared'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers'
|
|
|
|
import { JobModel } from '../../models/job/job'
|
2017-06-10 16:15:25 -04:00
|
|
|
|
2017-11-10 11:27:49 -05:00
|
|
|
export interface JobHandler<P, T> {
|
|
|
|
process (data: object, jobId: number): Promise<T>
|
2017-11-09 11:51:58 -05:00
|
|
|
onError (err: Error, jobId: number)
|
2017-11-15 10:28:35 -05:00
|
|
|
onSuccess (jobId: number, jobResult: T, jobScheduler: JobScheduler<P, T>): Promise<any>
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
2017-06-10 16:15:25 -04:00
|
|
|
type JobQueueCallback = (err: Error) => void
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-11-10 11:27:49 -05:00
|
|
|
class JobScheduler<P, T> {
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-11-09 11:51:58 -05:00
|
|
|
constructor (
|
|
|
|
private jobCategory: JobCategory,
|
2017-11-10 11:27:49 -05:00
|
|
|
private jobHandlers: { [ id: string ]: JobHandler<P, T> }
|
2017-11-09 11:51:58 -05:00
|
|
|
) {}
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
async activate () {
|
2017-11-09 11:51:58 -05:00
|
|
|
const limit = JOBS_FETCH_LIMIT_PER_CYCLE[this.jobCategory]
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-11-09 11:51:58 -05:00
|
|
|
logger.info('Jobs scheduler %s activated.', this.jobCategory)
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const jobsQueue = queue<JobModel, JobQueueCallback>(this.processJob.bind(this))
|
2017-05-15 16:22:03 -04:00
|
|
|
|
|
|
|
// Finish processing jobs from a previous start
|
|
|
|
const state = JOB_STATES.PROCESSING
|
2017-10-25 10:03:33 -04:00
|
|
|
try {
|
2017-12-12 11:53:50 -05:00
|
|
|
const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory)
|
2017-10-25 10:03:33 -04: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-12-12 11:53:50 -05:00
|
|
|
const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory)
|
2017-10-25 10:03:33 -04: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 16:22:03 -04:00
|
|
|
}
|
|
|
|
|
2017-11-10 11:27:49 -05:00
|
|
|
createJob (transaction: Sequelize.Transaction, handlerName: string, handlerInputData: P) {
|
2017-05-15 16:22:03 -04:00
|
|
|
const createQuery = {
|
|
|
|
state: JOB_STATES.PENDING,
|
2017-11-10 11:27:49 -05:00
|
|
|
category: this.jobCategory,
|
2017-05-15 16:22:03 -04:00
|
|
|
handlerName,
|
|
|
|
handlerInputData
|
|
|
|
}
|
2017-11-10 11:27:49 -05:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
const options = { transaction }
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
return JobModel.create(createQuery, options)
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
private enqueueJobs (jobsQueue: AsyncQueue<JobModel>, jobs: JobModel[]) {
|
2017-07-05 07:26:25 -04:00
|
|
|
jobs.forEach(job => jobsQueue.push(job))
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
private async processJob (job: JobModel, callback: (err: Error) => void) {
|
2017-11-09 11:51:58 -05:00
|
|
|
const jobHandler = this.jobHandlers[job.handlerName]
|
2017-07-05 07:26:25 -04:00
|
|
|
if (jobHandler === undefined) {
|
2017-11-14 11:31:26 -05:00
|
|
|
const errorString = 'Unknown job handler ' + job.handlerName + ' for job ' + job.id
|
|
|
|
logger.error(errorString)
|
|
|
|
|
|
|
|
const error = new Error(errorString)
|
|
|
|
await this.onJobError(jobHandler, job, error)
|
|
|
|
return callback(error)
|
2017-07-05 07:26:25 -04:00
|
|
|
}
|
2017-05-15 16:22:03 -04:00
|
|
|
|
|
|
|
logger.info('Processing job %d with handler %s.', job.id, job.handlerName)
|
|
|
|
|
|
|
|
job.state = JOB_STATES.PROCESSING
|
2017-10-25 10:03:33 -04:00
|
|
|
await job.save()
|
|
|
|
|
|
|
|
try {
|
2017-11-10 11:27:49 -05:00
|
|
|
const result: T = await jobHandler.process(job.handlerInputData, job.id)
|
2017-10-25 10:03:33 -04: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:31:26 -05:00
|
|
|
return callback(null)
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
private async onJobError (jobHandler: JobHandler<P, T>, job: JobModel, err: Error) {
|
2017-05-15 16:22:03 -04:00
|
|
|
job.state = JOB_STATES.ERROR
|
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
try {
|
|
|
|
await job.save()
|
2017-11-14 11:31:26 -05:00
|
|
|
if (jobHandler) await jobHandler.onError(err, job.id)
|
2017-10-25 10:03:33 -04:00
|
|
|
} catch (err) {
|
|
|
|
this.cannotSaveJobError(err)
|
|
|
|
}
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
private async onJobSuccess (jobHandler: JobHandler<P, T>, job: JobModel, jobResult: T) {
|
2017-05-15 16:22:03 -04:00
|
|
|
job.state = JOB_STATES.SUCCESS
|
|
|
|
|
2017-10-25 10:03:33 -04:00
|
|
|
try {
|
|
|
|
await job.save()
|
2017-11-15 10:28:35 -05:00
|
|
|
await jobHandler.onSuccess(job.id, jobResult, this)
|
2017-10-25 10:03:33 -04:00
|
|
|
} catch (err) {
|
|
|
|
this.cannotSaveJobError(err)
|
|
|
|
}
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
private cannotSaveJobError (err: Error) {
|
2017-07-07 12:26:12 -04:00
|
|
|
logger.error('Cannot save new job state.', err)
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
JobScheduler
|
|
|
|
}
|