aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/job/job.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-30 10:51:13 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-30 10:55:06 +0100
commit5cd80545422bba855cc9a730a2e13cc9d982c34b (patch)
tree9a60cd7c9218c296a1460938d11e3bce784f7cc0 /server/models/job/job.ts
parent1f3e9feca2caf68024168b0ea9ed39d8438fa235 (diff)
downloadPeerTube-5cd80545422bba855cc9a730a2e13cc9d982c34b.tar.gz
PeerTube-5cd80545422bba855cc9a730a2e13cc9d982c34b.tar.zst
PeerTube-5cd80545422bba855cc9a730a2e13cc9d982c34b.zip
Add ability to list jobs
Diffstat (limited to 'server/models/job/job.ts')
-rw-r--r--server/models/job/job.ts52
1 files changed, 40 insertions, 12 deletions
diff --git a/server/models/job/job.ts b/server/models/job/job.ts
index c2d088090..f428e26db 100644
--- a/server/models/job/job.ts
+++ b/server/models/job/job.ts
@@ -1,19 +1,14 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as Sequelize from 'sequelize' 2import * as Sequelize from 'sequelize'
3
4import { JOB_STATES, JOB_CATEGORIES } from '../../initializers'
5
6import { addMethodsToModel } from '../utils'
7import {
8 JobInstance,
9 JobAttributes,
10
11 JobMethods
12} from './job-interface'
13import { JobCategory, JobState } from '../../../shared/models/job.model' 3import { JobCategory, JobState } from '../../../shared/models/job.model'
4import { JOB_CATEGORIES, JOB_STATES } from '../../initializers'
5import { addMethodsToModel, getSort } from '../utils'
6import { JobAttributes, JobInstance, JobMethods } from './job-interface'
14 7
15let Job: Sequelize.Model<JobInstance, JobAttributes> 8let Job: Sequelize.Model<JobInstance, JobAttributes>
16let listWithLimitByCategory: JobMethods.ListWithLimitByCategory 9let listWithLimitByCategory: JobMethods.ListWithLimitByCategory
10let listForApi: JobMethods.ListForApi
11let toFormattedJSON: JobMethods.ToFormattedJSON
17 12
18export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { 13export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
19 Job = sequelize.define<JobInstance, JobAttributes>('Job', 14 Job = sequelize.define<JobInstance, JobAttributes>('Job',
@@ -44,12 +39,30 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se
44 } 39 }
45 ) 40 )
46 41
47 const classMethods = [ listWithLimitByCategory ] 42 const classMethods = [
48 addMethodsToModel(Job, classMethods) 43 listWithLimitByCategory,
44 listForApi
45 ]
46 const instanceMethods = [
47 toFormattedJSON
48 ]
49 addMethodsToModel(Job, classMethods, instanceMethods)
49 50
50 return Job 51 return Job
51} 52}
52 53
54toFormattedJSON = function (this: JobInstance) {
55 return {
56 id: this.id,
57 state: this.state,
58 category: this.category,
59 handlerName: this.handlerName,
60 handlerInputData: this.handlerInputData,
61 createdAt: this.createdAt,
62 updatedAt: this.updatedAt
63 }
64}
65
53// --------------------------------------------------------------------------- 66// ---------------------------------------------------------------------------
54 67
55listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { 68listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) {
@@ -66,3 +79,18 @@ listWithLimitByCategory = function (limit: number, state: JobState, jobCategory:
66 79
67 return Job.findAll(query) 80 return Job.findAll(query)
68} 81}
82
83listForApi = function (start: number, count: number, sort: string) {
84 const query = {
85 offset: start,
86 limit: count,
87 order: [ getSort(sort) ]
88 }
89
90 return Job.findAndCountAll(query).then(({ rows, count }) => {
91 return {
92 data: rows,
93 total: count
94 }
95 })
96}