Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FX-4495): RabbitMQ event when an article is published / un-published #3083

Merged
merged 12 commits into from
Jan 17, 2023

Conversation

nickskalkin
Copy link
Contributor

@nickskalkin nickskalkin commented Dec 20, 2022

Emits a RabbitMQ message when an article is published.

Deployment

  • Create exchange and queue on staging
  • Create exchange and queue on production
  • Create RabbitMQ user & add env variables (RABBITMQ_URL and ENABLE_PUBLISH_RABBITMQ_EVENTS) on staging
  • Create RabbitMQ user & add env variables (RABBITMQ_URL and ENABLE_PUBLISH_RABBITMQ_EVENTS) on production

@nickskalkin nickskalkin self-assigned this Dec 20, 2022
@nickskalkin nickskalkin force-pushed the nickskalkin/fx-4495/article-published-event branch from 7617223 to 9b7279b Compare January 5, 2023 17:10
@codecov
Copy link

codecov bot commented Jan 11, 2023

Codecov Report

Merging #3083 (3087416) into main (5d313c7) will decrease coverage by 0.0%.
The diff coverage is 66.6%.

❗ Current head 3087416 differs from pull request most recent head 4b6f2fb. Consider uploading reports for the commit 4b6f2fb to get more accurate results

@@           Coverage Diff           @@
##            main   #3083     +/-   ##
=======================================
- Coverage   83.1%   83.0%   -0.1%     
=======================================
  Files        196     197      +1     
  Lines       5385    5412     +27     
  Branches     988     989      +1     
=======================================
+ Hits        4477    4497     +20     
- Misses       656     662      +6     
- Partials     252     253      +1     

@nickskalkin nickskalkin marked this pull request as ready for review January 12, 2023 11:24
src/api/apps/articles/model/save.coffee Outdated Show resolved Hide resolved
Copy link
Member

@dblandin dblandin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Nice refactoring 👍🏼

)
}

module.exports = { amqp }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion / non-blocking: As this is a new file, might be nice to have it in TypeScript.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do in a separate PR this week!

@nickskalkin nickskalkin merged commit a0e40e3 into main Jan 17, 2023
@nickskalkin nickskalkin deleted the nickskalkin/fx-4495/article-published-event branch January 17, 2023 09:46
@artsy-peril
Copy link
Contributor

artsy-peril bot commented Jan 17, 2023

amqplib

Author: Michael Bridgen

Description: An AMQP 0-9-1 (e.g., RabbitMQ) library and client.

Homepage: http://amqp-node.github.io/amqplib/

Createdover 9 years ago
Last Updated5 months ago
LicenseMIT
Maintainers2
Releases31
Direct Dependencies@acuminous/bitsyntax, buffer-more-ints, readable-stream and url-parse
KeywordsAMQP, AMQP 0-9-1 and RabbitMQ
README

AMQP 0-9-1 library and client for Node.JS

NPM version
NPM downloads
Node.js CI
amqplib

npm install amqplib

A library for making AMQP 0-9-1 clients for Node.JS, and an AMQP 0-9-1 client for Node.JS v10+.

This library does not implement AMQP
1.0
or AMQP
0-10
.

Project status:

  • Expected to work
  • Complete high-level and low-level APIs (i.e., all bits of the protocol)
  • Stable APIs
  • A fair few tests
  • Measured test coverage
  • Ports of the RabbitMQ tutorials as examples
  • Used in production

Still working on:

  • Getting to 100% (or very close to 100%) test coverage

Callback API example

const amqplib = require('amqplib/callback_api');
const queue = 'tasks';

amqplib.connect('amqp://localhost', (err, conn) => {
  if (err) throw err;

  // Listener
  conn.createChannel((err, ch2) => {
    if (err) throw err;

    ch2.assertQueue(queue);

    ch2.consume(queue, (msg) => {
      if (msg !== null) {
        console.log(msg.content.toString());
        ch2.ack(msg);
      } else {
        console.log('Consumer cancelled by server');
      }
    });
  });

  // Sender
  conn.createChannel((err, ch1) => {
    if (err) throw err;

    ch1.assertQueue(queue);

    setInterval(() => {
      ch1.sendToQueue(queue, Buffer.from('something to do'));
    }, 1000);
  });
});

Promise/Async API example

const amqplib = require('amqplib');

(async () => {
  const queue = 'tasks';
  const conn = await amqplib.connect('amqp://localhost');

  const ch1 = await conn.createChannel();
  await ch1.assertQueue(queue);

  // Listener
  ch1.consume(queue, (msg) => {
    if (msg !== null) {
      console.log('Recieved:', msg.content.toString());
      ch1.ack(msg);
    } else {
      console.log('Consumer cancelled by server');
    }
  });

  // Sender
  const ch2 = await conn.createChannel();

  setInterval(() => {
    ch2.sendToQueue(queue, Buffer.from('something to do'));
  }, 1000);
})();

Running tests

npm test

To run the tests RabbitMQ is required. Either install it with your package
manager, or use docker to run a RabbitMQ instance.

docker run -d --name amqp.test -p 5672:5672 rabbitmq

If prefer not to run RabbitMQ locally it is also possible to use a
instance of RabbitMQ hosted elsewhere. Use the URL environment
variable to configure a different amqp host to connect to. You may
also need to do this if docker is not on localhost; e.g., if it's
running in docker-machine.

One public host is dev.rabbitmq.com:

URL=amqp://dev.rabbitmq.com npm test

NB You may experience test failures due to timeouts if using the
dev.rabbitmq.com instance.

You can run it under different versions of Node.JS using nave:

nave use 10 npm test

or run the tests on all supported versions of Node.JS in one go:

make test-all-nodejs

(which also needs nave installed, of course).

Lastly, setting the environment variable LOG_ERRORS will cause the
tests to output error messages encountered, to the console; this is
really only useful for checking the kind and formatting of the errors.

LOG_ERRORS=true npm test

Test coverage

make coverage
open file://`pwd`/coverage/lcov-report/index.html

New dependencies added: amqplib.

Generated by 🚫 dangerJS against 4b6f2fb

@artsy-peril artsy-peril bot mentioned this pull request Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants