]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/job/job.ts
Better view counter
[github/Chocobozzz/PeerTube.git] / server / models / job / job.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
e02643f3 2import * as Sequelize from 'sequelize'
227d02fe 3
e4f97bab 4import { JOB_STATES, JOB_CATEGORIES } from '../../initializers'
227d02fe 5
74889a71 6import { addMethodsToModel } from '../utils'
e02643f3 7import {
e02643f3
C
8 JobInstance,
9 JobAttributes,
10
11 JobMethods
12} from './job-interface'
350e31d6 13import { JobCategory, JobState } from '../../../shared/models/job.model'
e02643f3
C
14
15let Job: Sequelize.Model<JobInstance, JobAttributes>
e4f97bab 16let listWithLimitByCategory: JobMethods.ListWithLimitByCategory
227d02fe 17
127944aa 18export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
e02643f3 19 Job = sequelize.define<JobInstance, JobAttributes>('Job',
227d02fe
C
20 {
21 state: {
65fcc311 22 type: DataTypes.ENUM(values(JOB_STATES)),
227d02fe
C
23 allowNull: false
24 },
e4f97bab
C
25 category: {
26 type: DataTypes.ENUM(values(JOB_CATEGORIES)),
27 allowNull: false
28 },
227d02fe
C
29 handlerName: {
30 type: DataTypes.STRING,
31 allowNull: false
32 },
33 handlerInputData: {
34 type: DataTypes.JSON,
35 allowNull: true
36 }
37 },
38 {
39 indexes: [
40 {
350e31d6 41 fields: [ 'state', 'category' ]
227d02fe 42 }
e02643f3 43 ]
227d02fe
C
44 }
45 )
46
e4f97bab 47 const classMethods = [ listWithLimitByCategory ]
e02643f3
C
48 addMethodsToModel(Job, classMethods)
49
227d02fe
C
50 return Job
51}
52
53// ---------------------------------------------------------------------------
54
350e31d6 55listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) {
227d02fe
C
56 const query = {
57 order: [
58 [ 'id', 'ASC' ]
59 ],
60 limit: limit,
61 where: {
350e31d6
C
62 state,
63 category: jobCategory
227d02fe
C
64 }
65 }
66
6fcd19ba 67 return Job.findAll(query)
227d02fe 68}