/queue-bootstrap

IHostedService bootstrap application with RabbitMQ integration using resilient queues configuration and mysql managed database transactions.

Primary LanguageC#

Queue Bootstrap

The philosophy behind this repository is pretty simple: every time a new Consumer has to be created, the same set of functionalities must be written, and I got tired of writing it over and over again. You see, good programmers are lazy, and I am very lazy.

How to use it

If you have Make available on your OS, you can run dependecies services using it (On Makefile folder):

make run

If you don't and have Docker Compose (On docker-compose.yaml folder):

docker-compose -f docker-compose.yml up

If you don't have Docker Compose:

Install it :)

You can run the application through Visual Studio or from command line (On the API folder):

dotnet run

Some philosophies on this software

This is by no means the right way to code, it's just a way that I found easier to code on my daily bases:

Logs

Log everywhere and everything you can, mind your log level (you don't want to crash your log server). If you're writing software that does many differents thing accordingly of the input, be clear in your logs when your software does and doesn't do something. Always put a identifier on every log so you can trace to the affected entity you're working on.

Cache

Cache is cool, use it, but don't reinvent the wheel. You don't want to solve a distributed cache problem, when Redis, for example, already solved.

Publishing on message queues

If you're using RabbitMQ, don't publish on a queue, that's what exchanges are for (mostly).

Interfaces

Hey, ever use Inversion of Control? I didn't, I just use my interfaces for unit testing, if you won't use IoC, keep your interfaces and classes together, it's easier to maintain and expand.

Exceptions

RabbitMQ is well able to treat exceptions, so everytime an Exception occurs, so every thrown exception shouldn't be catched, just let RabbitMQ deadletters and errors messages do their work.

Services

Get everything your entity needs, validate it through FluentValidation and ValidationService, send it to it's repository (or cache, or message broker)

Input, processing, output.

Validations

Assyncronous validations should use ValidationService.cs, syncronous validation should use fluentvalidation

Repositories

They run queries, what's more for repositories?

Many Class Libraries

Just don't.

Logs, yeah, again.

Really, log everything, when your application crashes, they'll be your best friend.

Some cool things I think you should know

  • When a RabbitMQ connection is opened, the client tries to connect to, or create, a new set of exchanges and queues. Every queue will be created with a deadletter, so when a message cannot be fully proccessed it will be sent to a different queue to be processed afterwards, and in case this behavior happens a few times, the message goes to an error queue (with original queue name and exception value), where the developer team should analyze it and see what is going on, all this behavior is based on appsettings values.

  • Cache is present with Redis, and it's very simples to use, just open, and don't forget to close it, the connection with the server, there is a service written to facilitate your cache needs.

  • Infraestructure as code with docker is able to start all services this application uses: Redis, MySQL and RabbitMQ. Some configurations are overwritten through Dockerfiles, like MySQL user table and RabbitMQ user, virtual host and permissions configurations.