Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const amqp = require('amqplib');
- const amqpUrl = 'amqp://guest:guest@rabbitmq:5672';
- const QUEUE = 'resize_img_queue';
- const Jimp = require('jimp');
- const Instant = require('../controllers/models/instant');
- function bootstrap() {
- amqp.connect(amqpUrl)
- .then(conn => conn.createChannel())
- .then(ch => {
- ch.assertQueue(QUEUE)
- .then(() => {
- ch.consume(QUEUE, msg => {
- ch.prefetch(1);
- const jobData = JSON.parse(msg.content.toString());
- const photoUrl = jobData.url;
- const photoId = jobData.id;
- Jimp.read(jobData.url, (err, image) => {
- if (err) throw err;
- const resizedUrl = `src/instants/resized_${photoUrl.split('/')[4]}`;
- image
- .resize(140, 140) // resize
- .write(resizedUrl); // save
- Instant.findById(photoId).exec((instant, err) => {
- if (err) {
- instant.STATUS = 'FAILED';
- throw new Error;
- }
- console.log(photoId);
- console.log(instant);
- // instant.url = resizedUrl;
- // instant.STATUS = 'DONE'
- // instant.save();
- // ch.ack(msg);
- })
- });
- }, {
- noAck: false
- })
- })
- }).catch(err => {
- console.log(err);
- })
- };
- module.exports = {
- bootstrap,
- amqpUrl,
- QUEUE
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement