Monday, May 13, 2024
HomeRuby On RailsSimplify your system debugging by introducing occasion retailer linking

Simplify your system debugging by introducing occasion retailer linking


Final week I used to be coping with an fascinating bug. In our system, there was a person that didn’t belong to any tenant. And a tenant that didn’t have any customers. It’s a state of affairs that shouldn’t occur in our utility.

Debugging that difficulty was fairly easy due to the linking characteristic of RailsEventStore that we use in our utility to correlate all occasions associated to a person in a single stream. By linking to such a person stream you get the likelihood to see all occasions which are associated to that sure person account.

What’s fascinating right here is that there’s a UserLeftTenant occasion, which in our case, ought to result in deleting the person’s account if that was the one tenant that the person belonged to. However that didn’t occur. Moreover, as you’ll be able to see occasions that occurred after, there was nonetheless a chance to log in as that person. Which resulted in a really ugly error.

Nicely, at the very least we will see that the account nonetheless exists and it’s nonetheless potential to log in, proper? Finally, it turned out that there was one other means for a person to go away the tenant. It didn’t comply with the method of checking if that was the one tenant that the person belongs to. It was additionally fairly simple to seek out within the code as I used to be capable of grep by the UserLeftTenant occasion and discover that place in our codebase. One other good thing about utilizing occasions 😉

  class LinkByUserId
    def initialize(event_store: Rails.configuration.event_store, prefix: "$by_user_id_")
      @event_store = event_store
      @prefix = prefix
    finish

    def name(occasion)
      user_id = occasion.metadata[:user_id]
      @event_store.hyperlink([event.event_id], stream_name: "#{@prefix}#{user_id}") if user_id
    finish
  finish

It’s important to subscribe to all occasions

    event_store.subscribe_to_all_events(LinkByUserId.new)

Now we have now so as to add the user_id into the metadata.
Within the standard Rails utility, you’ll be able to set it up in your ApplicationController as around_action callback.

class ApplicationController < ActionController::Base
    
    around_action :enrich_events_with_current_user_metadata, if: :current_user
    
    def enrich_events_with_current_user_metadata
     extra_metadata = { user_id: current_user.id, locale: I18n.locale.to_s }
     event_store.with_metadata(extra_metadata) { yield }
    finish
finish

After all, you don’t need to restrict your self to linking occasions to person streams. Something fascinating for you, your crew, or the enterprise stakeholders will work properly. Don’t be scared to seek out new insights about your utility.

For those who get in bother organising your RES, be at liberty to affix our neighborhood



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments