PeerTube/server/models/job.ts

64 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-05-15 15:22:03 -05:00
import { values } from 'lodash'
2017-05-22 13:58:25 -05:00
import * as Sequelize from 'sequelize'
2017-05-15 15:22:03 -05:00
import { JOB_STATES } from '../initializers'
2017-05-22 13:58:25 -05:00
import { addMethodsToModel } from './utils'
import {
JobClass,
JobInstance,
JobAttributes,
JobMethods
} from './job-interface'
let Job: Sequelize.Model<JobInstance, JobAttributes>
let listWithLimit: JobMethods.ListWithLimit
2017-05-22 13:58:25 -05:00
export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes) {
Job = sequelize.define<JobInstance, JobAttributes>('Job',
{
state: {
2017-05-15 15:22:03 -05:00
type: DataTypes.ENUM(values(JOB_STATES)),
allowNull: false
},
handlerName: {
type: DataTypes.STRING,
allowNull: false
},
handlerInputData: {
type: DataTypes.JSON,
allowNull: true
}
},
{
indexes: [
{
fields: [ 'state' ]
}
2017-05-22 13:58:25 -05:00
]
}
)
2017-05-22 13:58:25 -05:00
const classMethods = [ listWithLimit ]
addMethodsToModel(Job, classMethods)
return Job
}
// ---------------------------------------------------------------------------
2017-05-22 13:58:25 -05:00
listWithLimit = function (limit, state, callback) {
const query = {
order: [
[ 'id', 'ASC' ]
],
limit: limit,
where: {
2017-05-05 10:24:16 -05:00
state
}
}
2017-05-22 13:58:25 -05:00
return Job.findAll(query).asCallback(callback)
}