Server: finish old jobs at startup

This commit is contained in:
Chocobozzz 2017-05-05 17:24:16 +02:00
parent e5b8853905
commit 4e284e97b9
3 changed files with 44 additions and 20 deletions

View File

@ -29,6 +29,11 @@ const missed = checker.checkMissedConfig()
if (missed.length !== 0) { if (missed.length !== 0) {
throw new Error('Miss some configurations keys : ' + missed) throw new Error('Miss some configurations keys : ' + missed)
} }
checker.checkFFmpeg(function (err) {
if (err) {
throw err
}
})
const errorMessage = checker.checkConfig() const errorMessage = checker.checkConfig()
if (errorMessage !== null) { if (errorMessage !== null) {

View File

@ -15,10 +15,17 @@ const jobScheduler = {
} }
function activate () { function activate () {
const limit = constants.JOBS_FETCH_LIMIT_PER_CYCLE
logger.info('Jobs scheduler activated.') logger.info('Jobs scheduler activated.')
const jobsQueue = queue(processJob) const jobsQueue = queue(processJob)
// Finish processing jobs from a previous start
const state = constants.JOB_STATES.PROCESSING
db.Job.listWithLimit(limit, state, function (err, jobs) {
enqueueJobs(err, jobsQueue, jobs)
forever( forever(
function (next) { function (next) {
if (jobsQueue.length() !== 0) { if (jobsQueue.length() !== 0) {
@ -26,7 +33,8 @@ function activate () {
return setTimeout(next, constants.JOBS_FETCHING_INTERVAL) return setTimeout(next, constants.JOBS_FETCHING_INTERVAL)
} }
db.Job.listWithLimit(constants.JOBS_FETCH_LIMIT_PER_CYCLE, function (err, jobs) { const state = constants.JOB_STATES.PENDING
db.Job.listWithLimit(limit, state, function (err, jobs) {
if (err) { if (err) {
logger.error('Cannot list pending jobs.', { error: err }) logger.error('Cannot list pending jobs.', { error: err })
} else { } else {
@ -40,6 +48,7 @@ function activate () {
}) })
} }
) )
})
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -48,6 +57,16 @@ module.exports = jobScheduler
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function enqueueJobs (err, jobsQueue, jobs) {
if (err) {
logger.error('Cannot list pending jobs.', { error: err })
} else {
jobs.forEach(function (job) {
jobsQueue.push(job)
})
}
}
function createJob (transaction, handlerName, handlerInputData, callback) { function createJob (transaction, handlerName, handlerInputData, callback) {
const createQuery = { const createQuery = {
state: constants.JOB_STATES.PENDING, state: constants.JOB_STATES.PENDING,

View File

@ -39,14 +39,14 @@ module.exports = function (sequelize, DataTypes) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function listWithLimit (limit, callback) { function listWithLimit (limit, state, callback) {
const query = { const query = {
order: [ order: [
[ 'id', 'ASC' ] [ 'id', 'ASC' ]
], ],
limit: limit, limit: limit,
where: { where: {
state: constants.JOB_STATES.PENDING state
} }
} }