Short notes from engineering

Micro-posts on programming and patterns as we build Tramline in public. Follow this feed if you find these bits interesting.


2022 / Sep 28 — 09:02
kitallis

It's not terribly hard to come up with a state-machine DSL like aasm by hand, but the ActiveRecord support is very handy.

I'm using it with an enum against a status field with the following options:

{
  column: :status,
  requires_lock: true,
  requires_new_transaction: false,
  enum: true,
  create_scopes: false
}

The requires_new_transaction is false to avoid landing into unnecessary nested transactions. I'll keep my transactions explicit and intentional. The enum allows me to play well with the Rails enum which already generates scopes, so I keep create_scopes off.

The requires_lock is seemingly quite handy at first glance, since I require locking rows a fair bit because of the distributed nature of the app. But the level at which it locks is far too granular. I generally seem to require locking rows for far longer and more work than simply transitioning state and not everything can be sanely encapsulated in a before or after. I'd rather provide the library with methods that cause a state transition than the inverted way it currently operates: generating methods to transition for you and then locking within that event.