PeerTube/server/lib/jobs/job-scheduler.ts

145 lines
4.1 KiB
TypeScript
Raw Normal View History

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