diff options
Diffstat (limited to 'server/models/job')
-rw-r--r-- | server/models/job/index.ts | 1 | ||||
-rw-r--r-- | server/models/job/job-interface.ts | 24 | ||||
-rw-r--r-- | server/models/job/job.ts | 63 |
3 files changed, 88 insertions, 0 deletions
diff --git a/server/models/job/index.ts b/server/models/job/index.ts new file mode 100644 index 000000000..56925fd32 --- /dev/null +++ b/server/models/job/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './job-interface' | |||
diff --git a/server/models/job/job-interface.ts b/server/models/job/job-interface.ts new file mode 100644 index 000000000..ab6678257 --- /dev/null +++ b/server/models/job/job-interface.ts | |||
@@ -0,0 +1,24 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | export namespace JobMethods { | ||
4 | export type ListWithLimitCallback = (err: Error, jobInstances: JobInstance[]) => void | ||
5 | export type ListWithLimit = (limit: number, state: string, callback: ListWithLimitCallback) => void | ||
6 | } | ||
7 | |||
8 | export interface JobClass { | ||
9 | listWithLimit: JobMethods.ListWithLimit | ||
10 | } | ||
11 | |||
12 | export interface JobAttributes { | ||
13 | state: string | ||
14 | handlerName: string | ||
15 | handlerInputData: object | ||
16 | } | ||
17 | |||
18 | export interface JobInstance extends JobClass, JobAttributes, Sequelize.Instance<JobAttributes> { | ||
19 | id: number | ||
20 | createdAt: Date | ||
21 | updatedAt: Date | ||
22 | } | ||
23 | |||
24 | export interface JobModel extends JobClass, Sequelize.Model<JobInstance, JobAttributes> {} | ||
diff --git a/server/models/job/job.ts b/server/models/job/job.ts new file mode 100644 index 000000000..60a6c551b --- /dev/null +++ b/server/models/job/job.ts | |||
@@ -0,0 +1,63 @@ | |||
1 | import { values } from 'lodash' | ||
2 | import * as Sequelize from 'sequelize' | ||
3 | |||
4 | import { JOB_STATES } from '../../initializers' | ||
5 | |||
6 | import { addMethodsToModel } from '../utils' | ||
7 | import { | ||
8 | JobClass, | ||
9 | JobInstance, | ||
10 | JobAttributes, | ||
11 | |||
12 | JobMethods | ||
13 | } from './job-interface' | ||
14 | |||
15 | let Job: Sequelize.Model<JobInstance, JobAttributes> | ||
16 | let listWithLimit: JobMethods.ListWithLimit | ||
17 | |||
18 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
19 | Job = sequelize.define<JobInstance, JobAttributes>('Job', | ||
20 | { | ||
21 | state: { | ||
22 | type: DataTypes.ENUM(values(JOB_STATES)), | ||
23 | allowNull: false | ||
24 | }, | ||
25 | handlerName: { | ||
26 | type: DataTypes.STRING, | ||
27 | allowNull: false | ||
28 | }, | ||
29 | handlerInputData: { | ||
30 | type: DataTypes.JSON, | ||
31 | allowNull: true | ||
32 | } | ||
33 | }, | ||
34 | { | ||
35 | indexes: [ | ||
36 | { | ||
37 | fields: [ 'state' ] | ||
38 | } | ||
39 | ] | ||
40 | } | ||
41 | ) | ||
42 | |||
43 | const classMethods = [ listWithLimit ] | ||
44 | addMethodsToModel(Job, classMethods) | ||
45 | |||
46 | return Job | ||
47 | } | ||
48 | |||
49 | // --------------------------------------------------------------------------- | ||
50 | |||
51 | listWithLimit = function (limit: number, state: string, callback: JobMethods.ListWithLimitCallback) { | ||
52 | const query = { | ||
53 | order: [ | ||
54 | [ 'id', 'ASC' ] | ||
55 | ], | ||
56 | limit: limit, | ||
57 | where: { | ||
58 | state | ||
59 | } | ||
60 | } | ||
61 | |||
62 | return Job.findAll(query).asCallback(callback) | ||
63 | } | ||