1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
import { Op, QueryTypes, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, IsInt, Model, Table, Unique, UpdatedAt } from 'sequelize-typescript'
import { forceNumber } from '@shared/core-utils'
import { AttributesOnly } from '@shared/typescript-utils'
import { VideoModel } from './video'
export type VideoJobInfoColumnType = 'pendingMove' | 'pendingTranscode'
@Table({
tableName: 'videoJobInfo',
indexes: [
{
fields: [ 'videoId' ],
where: {
videoId: {
[Op.ne]: null
}
}
}
]
})
export class VideoJobInfoModel extends Model<Partial<AttributesOnly<VideoJobInfoModel>>> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Default(0)
@IsInt
@Column
pendingMove: number
@AllowNull(false)
@Default(0)
@IsInt
@Column
pendingTranscode: number
@ForeignKey(() => VideoModel)
@Unique
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: VideoModel
static load (videoId: number, transaction?: Transaction) {
const where = {
videoId
}
return VideoJobInfoModel.findOne({ where, transaction })
}
static async increaseOrCreate (videoUUID: string, column: VideoJobInfoColumnType, amountArg = 1): Promise<number> {
const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } }
const amount = forceNumber(amountArg)
const [ result ] = await VideoJobInfoModel.sequelize.query<{ pendingMove: number }>(`
INSERT INTO "videoJobInfo" ("videoId", "${column}", "createdAt", "updatedAt")
SELECT
"video"."id" AS "videoId", ${amount}, NOW(), NOW()
FROM
"video"
WHERE
"video"."uuid" = $videoUUID
ON CONFLICT ("videoId") DO UPDATE
SET
"${column}" = "videoJobInfo"."${column}" + ${amount},
"updatedAt" = NOW()
RETURNING
"${column}"
`, options)
return result[column]
}
static async decrease (videoUUID: string, column: VideoJobInfoColumnType): Promise<number> {
const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } }
const result = await VideoJobInfoModel.sequelize.query(`
UPDATE
"videoJobInfo"
SET
"${column}" = "videoJobInfo"."${column}" - 1,
"updatedAt" = NOW()
FROM "video"
WHERE
"video"."id" = "videoJobInfo"."videoId" AND "video"."uuid" = $videoUUID
RETURNING
"${column}";
`, options)
if (result.length === 0) return undefined
return result[0][column]
}
static async abortAllTasks (videoUUID: string, column: VideoJobInfoColumnType): Promise<void> {
const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE, bind: { videoUUID } }
await VideoJobInfoModel.sequelize.query(`
UPDATE
"videoJobInfo"
SET
"${column}" = 0,
"updatedAt" = NOW()
FROM "video"
WHERE
"video"."id" = "videoJobInfo"."videoId" AND "video"."uuid" = $videoUUID
`, options)
}
}
|