ANGTFT

Sometimes you do certain tasks that become normal and you kind of go into autopilot, blindly repeating the same thing over and over again. It normally takes someone else or something else to spark something in your mind and give you an idea that there is always a simpler solution.

My spark came in the form of Alan Richardson, I’ve been a huge fan of his work for a while and I love the content that he creates and the weekly YouTube videos! A couple of weeks ago he released a short video about creating a bookmarklet to change some static text URL links on a web page, into actual clickable links, that open up a specific web page, a Twitter profile in this case. I thought that was really cool and looked like something that I could have a go at myself. All of the JavaScript code for doing this was written within the Chrome DevTools console – Perfect, I’m familiar with these already!

Recently, I’ve been using a strategy when testing, that involves creating “Test Queues” within RabbitMQ (The message broker that we use for our microservices) that siphon all the messages from the queues that our microservices are consuming – The Test Queues hold/store them, so that I can investigate deeper into the message data. Unlike the actual queues, these do not have consumers so the messages will stay there until I choose to delete them, adding an extra layer of control. I could just manually stop the service and grab the messages from the service queues but I like to give myself a separate option.

Screenshot from 2017-06-03 11-13-34
RabbitMQ – Publisher > Exchange > Queues > Consumers…

For context – My Test Queues would live alongside the red ones in the image above but will not have consumers, the arrows to the right of the red queues…hopefully, that makes sense. 🙂

My boring repetitive problem…

RabbitMQ is an awesome message broker and like it says on the site “Messaging that just works” what they haven’t concentrated on is the usability of their UI management console, why should they, that’s not their main focus – they currently just have something that’s good for now. As much as having these queues is really useful, clearing them out or purging them is a tedious task, it’s made worse by the fact that I follow the same process several times a day…

Click on the queue name > scroll to the bottom of the page > Click on the “Purge” button > scroll back to the top of the page > Select the Queue tab to get back to the main view…repeat…yawn!

I’m a fan of getting a local instance of the technologies that we use within our feature team and exploring lots of different aspects so I feel more comfortable and more informed about the tools I’m working within. I knew the RabbitMQ has a HTTP Restful API with a limited amount of features, I can use some of these to my advantage!!

RabbitMQ has been around for about 10 years now so I had a hunch that someone has probably had the same problem as me and wanted to use the API to purge the messages within a queue…I headed over to Google…

Basically, the first result that came back was about 90% of the solution that I required…Bingo! I found this from 5 years ago – Like I said, it wasn’t exactly what I wanted so I needed to adapt the code to suit my requirement.

I grabbed the code and pasted it into a new Snippet within the “Sources” tab on the Dev Tools, I love this feature, it’s like a little playground for practising and running basic JavaScript. The code in the gist deletes the messages from every single queue which was the main part that I needed to change, I tend to apply a simple naming convention to my queues and prefix them all with “TestQueue_” – This is just so that they are all grouped together within the management console.

TestQueues_RabbitMQ
My Test Queues with lots of lovely messages…

I only needed to refactor the code slightly and add an IF statement that checks to see if the queue name starts with the “TestQueue_” name if so, purge the queue. All the other queues are unaffected. Job Done!

RabbitManagementConsole
Slightly refactored code with an IF Statement…

During the testing of the code, I added some logging just to sanity check that I was getting the correct queues that I wanted and discarding all the other ones. All the “null” entries below are queues that do not start with “TestQueue_” so are ignored.

Getting_Queue_Names
Iterating over all the queues to get the ones I want…

Once the Snippet was run, I could see that the correct requests are being made…Yay! The messages were deleted from only the queues I wanted.

Console_Output
Console Output showing the DELETE requests…

I have created a gist of the code that I used to Purge the Queues – https://gist.github.com/DannyDainton/2bae06e3ca898440cdc0452a727ee7bf

I could have just settled for that solution and ran this script from the Dev Tools each time but I wanted it to be even easier for me (I’m very lazy) so that’s where Alan’s bookmarklet app comes in handy, you can take any piece of JavaScript code that you have coded to do something on a page and it will create an encoded link that you can add to your browser bookmarks.

Bookmarklet App
Creating a new bookmarklet with my JavaScript code…

So I now have a link on my browser that saves me the pain of going through the tedious repetitive task to purge each queue. It’s the simple things in life that make me happy.

Alan wrote a post to accompany his video that references lots of links about how others have used bookmarklets. One of those is this excellent blog post by Abby Bangser, which I read a while ago before I saw the YouTube video. She explains how she utilizes the bookmarklets to quickly fill in form data – It’s really interesting and I would recommend trying to give it a go yourself.

Hopefully, that has been interesting enough to spark something in your own mind and attempt to give this a go – I would love to hear from anyone who has created a bookmarklet to solve a problem.

Cheers!

Advertisement