-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
43 lines (37 loc) · 1.15 KB
/
worker.js
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
import Queue from 'bull/lib/queue';
import imageThumbnail from 'image-thumbnail';
import fs from 'fs';
import { ObjectId } from 'mongodb';
import dbClient from './utils/db';
export const fileQueue = new Queue('Thumbnail Generation');
export const userQueue = new Queue('User Welcome email');
fileQueue.process(async (job) => {
const { fileId, userId } = job.data;
if (!fileId) {
throw new Error('Missing fileId');
}
if (!userId) {
throw new Error('Missing userId');
}
const file = await dbClient.findFile({ _id: ObjectId(fileId), userId: ObjectId(userId) });
if (!file) {
throw new Error('File not found');
}
const { localPath } = file;
const widths = [500, 250, 100];
widths.forEach(async (width) => {
const thumbnail = await imageThumbnail(localPath, { width });
fs.writeFileSync(`${localPath}_${width}`, thumbnail);
});
});
userQueue.process(async (job) => {
const { userId } = job.data;
if (!userId) {
throw new Error('Missing userId');
}
const user = await dbClient.findUser({ _id: ObjectId(userId) });
if (!user) {
throw new Error('User not found');
}
console.log(`Welcome ${user.email}!`);
});