diff options
Diffstat (limited to 'server/models/video/video-job-info.ts')
-rw-r--r-- | server/models/video/video-job-info.ts | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/server/models/video/video-job-info.ts b/server/models/video/video-job-info.ts index 740f6b5c6..5845b8c74 100644 --- a/server/models/video/video-job-info.ts +++ b/server/models/video/video-job-info.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import { Op, QueryTypes, Transaction } from 'sequelize' | 1 | import { Op, QueryTypes, Transaction } from 'sequelize' |
2 | import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, IsInt, Model, Table, Unique, UpdatedAt } from 'sequelize-typescript' | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, IsInt, Model, Table, Unique, UpdatedAt } from 'sequelize-typescript' |
3 | import { forceNumber } from '@shared/core-utils' | ||
3 | import { AttributesOnly } from '@shared/typescript-utils' | 4 | import { AttributesOnly } from '@shared/typescript-utils' |
4 | import { VideoModel } from './video' | 5 | import { VideoModel } from './video' |
5 | 6 | ||
@@ -59,32 +60,33 @@ export class VideoJobInfoModel extends Model<Partial<AttributesOnly<VideoJobInfo | |||
59 | return VideoJobInfoModel.findOne({ where, transaction }) | 60 | return VideoJobInfoModel.findOne({ where, transaction }) |
60 | } | 61 | } |
61 | 62 | ||
62 | static async increaseOrCreate (videoUUID: string, column: VideoJobInfoColumnType): Promise<number> { | 63 | static async increaseOrCreate (videoUUID: string, column: VideoJobInfoColumnType, amountArg = 1): Promise<number> { |
63 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } } | 64 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } } |
65 | const amount = forceNumber(amountArg) | ||
64 | 66 | ||
65 | const [ { pendingMove } ] = await VideoJobInfoModel.sequelize.query<{ pendingMove: number }>(` | 67 | const [ result ] = await VideoJobInfoModel.sequelize.query<{ pendingMove: number }>(` |
66 | INSERT INTO "videoJobInfo" ("videoId", "${column}", "createdAt", "updatedAt") | 68 | INSERT INTO "videoJobInfo" ("videoId", "${column}", "createdAt", "updatedAt") |
67 | SELECT | 69 | SELECT |
68 | "video"."id" AS "videoId", 1, NOW(), NOW() | 70 | "video"."id" AS "videoId", ${amount}, NOW(), NOW() |
69 | FROM | 71 | FROM |
70 | "video" | 72 | "video" |
71 | WHERE | 73 | WHERE |
72 | "video"."uuid" = $videoUUID | 74 | "video"."uuid" = $videoUUID |
73 | ON CONFLICT ("videoId") DO UPDATE | 75 | ON CONFLICT ("videoId") DO UPDATE |
74 | SET | 76 | SET |
75 | "${column}" = "videoJobInfo"."${column}" + 1, | 77 | "${column}" = "videoJobInfo"."${column}" + ${amount}, |
76 | "updatedAt" = NOW() | 78 | "updatedAt" = NOW() |
77 | RETURNING | 79 | RETURNING |
78 | "${column}" | 80 | "${column}" |
79 | `, options) | 81 | `, options) |
80 | 82 | ||
81 | return pendingMove | 83 | return result[column] |
82 | } | 84 | } |
83 | 85 | ||
84 | static async decrease (videoUUID: string, column: VideoJobInfoColumnType): Promise<number> { | 86 | static async decrease (videoUUID: string, column: VideoJobInfoColumnType): Promise<number> { |
85 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } } | 87 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } } |
86 | 88 | ||
87 | const result = await VideoJobInfoModel.sequelize.query<{ pendingMove: number }>(` | 89 | const result = await VideoJobInfoModel.sequelize.query(` |
88 | UPDATE | 90 | UPDATE |
89 | "videoJobInfo" | 91 | "videoJobInfo" |
90 | SET | 92 | SET |
@@ -99,7 +101,7 @@ export class VideoJobInfoModel extends Model<Partial<AttributesOnly<VideoJobInfo | |||
99 | 101 | ||
100 | if (result.length === 0) return undefined | 102 | if (result.length === 0) return undefined |
101 | 103 | ||
102 | return result[0].pendingMove | 104 | return result[0][column] |
103 | } | 105 | } |
104 | 106 | ||
105 | static async abortAllTasks (videoUUID: string, column: VideoJobInfoColumnType): Promise<void> { | 107 | static async abortAllTasks (videoUUID: string, column: VideoJobInfoColumnType): Promise<void> { |