-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
258 lines (204 loc) · 6.78 KB
/
worker.ts
File metadata and controls
258 lines (204 loc) · 6.78 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { Job, Worker } from "bullmq";
import IORedis from "ioredis";
import { models } from "./models";
import { AcBackgroundJob } from "./models/acBackgroundJob";
const fs = require("fs");
const redisUrl = process.env.REDIS_URL || undefined;
const redisOptions = {
maxRetriesPerRequest: null,
enableReadyCheck: false,
};
const redisConnection = redisUrl
? new IORedis(redisUrl, redisOptions)
: new IORedis(redisOptions);
const uploadAllToS3 = require('./s3').uploadAllToS3;
const uploadToS3 = require('./s3').uploadToS3;
const downloadFromS3 = require('./s3').downloadFromS3;
const encodeVideo = require('./video').encodeVideo;
const createScreenshots = require('./video').createScreenshots;
const encodeAudio = require('./audio').encodeAudio;
const encodeFlac = require('./audio').encodeFlac;
const handleVideoJob = async (job: Job<JobDataAttributes>) => {
let acBackgroundJob: AcBackgroundJob | null = null;
console.log(`Got job ${job.id} ${JSON.stringify(job.data)}`);
try {
const jobData = job.data as JobDataAttributes;
console.log(`AcBackgroundJob Id: ${jobData.acBackgroundJobId}`);
acBackgroundJob = await models.AcBackgroundJob.findOne({
where: {
id: jobData.acBackgroundJobId,
},
});
if (!acBackgroundJob) {
throw new Error("Can't find acBackgroundJob");
}
const tempInDir = `/tmp/${acBackgroundJob.id}/in`;
const tempOutVideoDir = `/tmp/${acBackgroundJob.id}/out`;
const tempOutVThumbnailDir = `/tmp/${acBackgroundJob.id}/outThumbnails`;
await fs.promises.mkdir(tempInDir, { recursive: true });
await fs.promises.mkdir(tempOutVideoDir, { recursive: true });
await fs.promises.mkdir(tempOutVThumbnailDir, { recursive: true });
const videoInFilename = `${tempInDir}/${jobData.fileKey}`;
const videoOutFilename = `${tempOutVideoDir}/${jobData.fileKey}`;
const videoOutFlacFilename = `${tempOutVideoDir}/${jobData.flacFilename}`;
console.log(`Video Download from S3`);
await downloadFromS3(
process.env.S3_VIDEO_UPLOAD_BUCKET!,
jobData.fileKey,
videoInFilename
);
console.log(`Video Encoding`);
const videoDuration = await encodeVideo(
videoInFilename,
videoOutFilename,
jobData,
acBackgroundJob
);
console.log(`Video duration: ${videoDuration}`);
console.log(`Video Upload to S3`);
await uploadToS3(
process.env.S3_VIDEO_PUBLIC_BUCKET!,
jobData.fileKey,
videoOutFilename
);
console.log(`Screenshots created`);
await createScreenshots(
videoOutFilename,
tempOutVThumbnailDir,
jobData,
videoDuration as number
);
console.log(`Screenshots Uploaded to S3`);
await uploadAllToS3(
tempOutVThumbnailDir,
process.env.S3_VIDEO_THUMBNAIL_BUCKET!
);
if (process.env.GOOGLE_TRANSCODING_FLAC_BUCKET) {
console.log("Encode flac");
const hasFlac = (await encodeFlac(
videoOutFilename,
videoOutFlacFilename
)) as boolean;
if (hasFlac) {
console.log("Uploading flac");
await uploadToS3(
process.env.S3_VIDEO_PUBLIC_BUCKET!,
jobData.flacFilename,
videoOutFlacFilename
);
} else {
console.warn("No flac produced");
}
}
console.log(`Video Completed saving...`);
acBackgroundJob.progress = 100;
acBackgroundJob.data.finalDuration = videoDuration;
//@ts-ignore
acBackgroundJob.set("data.status", "Complete");
await acBackgroundJob.save();
console.log(`Video Job ${job.id} Completed`);
return {
duration: videoDuration,
};
} catch (error) {
if (acBackgroundJob) {
try {
console.log(`${JSON.stringify(acBackgroundJob)}`);
acBackgroundJob.progress = 0;
//@ts-ignore
acBackgroundJob.set("data.status", "Error");
await acBackgroundJob.save();
} catch (innerError) {
console.error(innerError);
}
}
console.error(error);
throw error;
}
};
const handleAudioJob = async (job: Job<JobDataAttributes>) => {
let acBackgroundJob: AcBackgroundJob | null = null;
console.log(`Got job ${job.id}`);
try {
const jobData = job.data as JobDataAttributes;
acBackgroundJob = await models.AcBackgroundJob.findOne({
where: {
id: jobData.acBackgroundJobId,
},
});
if (!acBackgroundJob) {
throw new Error("Can't find acBackgroundJob");
}
const tempInDir = `/tmp/${acBackgroundJob.id}/in`;
const tempOutAudioDir = `/tmp/${acBackgroundJob.id}/outAudio`;
await fs.promises.mkdir(tempInDir, { recursive: true });
await fs.promises.mkdir(tempOutAudioDir, { recursive: true });
const audioInFilename = `${tempInDir}/${jobData.fileKey}`;
const audioOutFilename = `${tempOutAudioDir}/${jobData.fileKey}`;
const audioOutFlacFilename = `${tempOutAudioDir}/${jobData.flacFilename}`;
console.log(`Audio Download from S3`);
await downloadFromS3(
process.env.S3_AUDIO_UPLOAD_BUCKET!,
jobData.fileKey,
audioInFilename
);
console.log(`Audio Encoding`);
const audioDuration = await encodeAudio(
audioInFilename,
audioOutFilename,
jobData,
acBackgroundJob
);
console.log(`Audio duration: ${audioDuration}`);
console.log(`Audio Upload to S3`);
await uploadToS3(
process.env.S3_AUDIO_PUBLIC_BUCKET!,
jobData.fileKey,
audioOutFilename
);
if (process.env.GOOGLE_TRANSCODING_FLAC_BUCKET) {
console.log("Encode flac");
await encodeFlac(
audioOutFilename,
audioOutFlacFilename
);
console.log("Uploading flac");
await uploadToS3(
process.env.S3_AUDIO_PUBLIC_BUCKET!,
jobData.flacFilename,
audioOutFlacFilename
);
}
acBackgroundJob.progress = 100;
acBackgroundJob.data.finalDuration = audioDuration;
//@ts-ignore
acBackgroundJob.set("data.status", "Complete");
await acBackgroundJob.save();
console.log(`Audio Job ${job.id} Completed`);
return {
duration: audioDuration,
};
} catch (error) {
if (acBackgroundJob) {
try {
acBackgroundJob.progress = 0;
//@ts-ignore
acBackgroundJob.set("data.status", "Error");
await acBackgroundJob.save();
} catch (innerError) {
console.error(innerError);
}
}
console.error(error);
throw error;
}
};
const workerOptions = { connection: redisConnection };
const videoWorker = new Worker<JobDataAttributes>("VideoEncoding", handleVideoJob, workerOptions);
const audioWorker = new Worker<JobDataAttributes>("AudioEncoding", handleAudioJob, workerOptions);
videoWorker.on("error", (error) => {
console.error("Video worker error", error);
});
audioWorker.on("error", (error) => {
console.error("Audio worker error", error);
});