-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
45 lines (34 loc) · 1.39 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
44
45
import DBClient from './utils/db';
const Bull = require('bull');
const { ObjectId } = require('mongodb');
const imageThumbnail = require('image-thumbnail');
const fs = require('fs');
const fileQueue = new Bull('fileQueue');
const userQueue = new Bull('userQueue');
const createImageThumbnail = async (path, options) => {
try {
const thumbnail = await imageThumbnail(path, options);
const pathNail = `${path}_${options.width}`;
await fs.writeFileSync(pathNail, thumbnail);
} catch (error) {
console.log(error);
}
};
fileQueue.process(async (job) => {
const { fileId } = job.data;
if (!fileId) throw Error('Missing fileId');
const { userId } = job.data;
if (!userId) throw Error('Missing userId');
const fileDocument = await DBClient.db.collection('files').findOne({ _id: ObjectId(fileId), userId: ObjectId(userId) });
if (!fileDocument) throw Error('File not found');
createImageThumbnail(fileDocument.localPath, { width: 500 });
createImageThumbnail(fileDocument.localPath, { width: 250 });
createImageThumbnail(fileDocument.localPath, { width: 100 });
});
userQueue.process(async (job) => {
const { userId } = job.data;
if (!userId) throw Error('Missing userId');
const userDocument = await DBClient.db.collection('users').findOne({ _id: ObjectId(userId) });
if (!userDocument) throw Error('User not found');
console.log(`Welcome ${userDocument.email}`);
});