Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. const amqp = require('amqplib');
  2. const amqpUrl = 'amqp://guest:guest@rabbitmq:5672';
  3. const QUEUE = 'resize_img_queue';
  4. const Jimp = require('jimp');
  5. const Instant = require('../controllers/models/instant');
  6.  
  7.  
  8. function bootstrap() {
  9. amqp.connect(amqpUrl)
  10. .then(conn => conn.createChannel())
  11. .then(ch => {
  12. ch.assertQueue(QUEUE)
  13. .then(() => {
  14. ch.consume(QUEUE, msg => {
  15. ch.prefetch(1);
  16. const jobData = JSON.parse(msg.content.toString());
  17. const photoUrl = jobData.url;
  18. const photoId = jobData.id;
  19. Jimp.read(jobData.url, (err, image) => {
  20. if (err) throw err;
  21. const resizedUrl = `src/instants/resized_${photoUrl.split('/')[4]}`;
  22. image
  23. .resize(140, 140) // resize
  24. .write(resizedUrl); // save
  25.  
  26. Instant.findById(photoId).exec((instant, err) => {
  27. if (err) {
  28. instant.STATUS = 'FAILED';
  29. throw new Error;
  30. }
  31. console.log(photoId);
  32. console.log(instant);
  33. // instant.url = resizedUrl;
  34. // instant.STATUS = 'DONE'
  35. // instant.save();
  36. // ch.ack(msg);
  37. })
  38. });
  39. }, {
  40. noAck: false
  41. })
  42. })
  43.  
  44. }).catch(err => {
  45. console.log(err);
  46. })
  47. };
  48.  
  49. module.exports = {
  50. bootstrap,
  51. amqpUrl,
  52. QUEUE
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement