Tutorial: Decoupling Requests in Trailblazer Operations
Codementor Ruby on Rails Contributor and Expert Nick Sutterer joined us for a session in office hours to talk about the Trailblazer framework he’s working on. During his talks, Nick answered some of our viewers’ questions regarding his upcoming book on Trailblazer.
The text below is a summary done by the Codementor team and may vary from the original video and if you see any issues, please let us know!
In Your Book, You Mentioned How Off Requests Can Be Decoupled and Queued in Operations. Can You Elaborate on that?
Although an operation isn’t stateless, from an outer perspective, an operation is stateless because all you can do is
operations.run. So this makes it really simple to decouple front-end requests, as you can just send operations to a background worker without any work at all. So what I have in my productions applications is I have a top level operation to create a profile, but creating a profile requires things like sending notifications, validating, and uploading the avatar photo, etc. In the Trailblazer framework, these parts will all be implemented in additional smaller operations. This makes it really simple to send several of the operations to a background job because the implementation of the operation doesn’t have to change, since they are already designed as a kind of background object.
In my top level operation, where the validation is finished and thus the profile data is all right, I have a chain of calling smaller operations. In that chain I can decide where every operation goes (e.g. push this to the background job, push this to the background job, and wait for the result of this one.) Though, at the moment all of the operations I’ve had are fire and forget as I’ve never had one that I’d have to wait for, since the smaller operations in the background know how to update the application stage once they were run successfully.
The thing is, modeling all my stuff in operations is a no-brainer…you just push it to a background job. I think this can be quite helpful. For example, if you have an image sharing site where hundreds of thousands of photos are being uploaded and edited (and therefore there is a lot of domain logic going on), it will be much easier to rearrange how those operations are run. If I decided today that I’m going to push a certain part to a background job, it will take me five seconds to do that.
So When Are You Planning to Release the Book?
I was planning to release the book or an almost preview this year.
I’m currently working on an example application called “Gemgem” which is a really small application, but it shows all the different aspects and concepts of Trailblazer in a really simple way. I don’t want to release something half-fake, because I’ve seen several publications that didn’t really satisfy me and my hunger for object orientation. So, I want to finish this example application and maybe even blog about it beforehand while I’m writing the actual book.
I want to publish something that is safe to publish. I mean it’s on leanpub so you can read it while I write it, but the real book is going to take a couple of months. I really want to focus on concepts like why I put things in separate classes.
On that note, I’m still thinking about whether I’m going to walk people through a tutorial or if I’m going to present code to them right away, because it’s a bit difficult to introduce a new concept like an operation or form object without any concept. First you need to establish a kind of problem that everyone understands, for example, I’ll have to establish a problem like: “here’s my model and here are a lot of validations. My model doesn’t work anymore because I used my validations in two different contexts. So I have to put an if and else to activate those validations or disable the validations for a different context.” And then I’ll have to tell them how it would be done in Trailblazer, and prove to them why it would be better. This is the hardest part of the book because I have to install the problem in people’s minds without having too much code and present the solution without having too much code. When I have some pretty simple examples, people complain they don’t see the point of using Trailblazer, but then if the examples are too complex, they don’t understand what I’m talking about. So I’m trying to find a balance, and hopefully, people will find the book useful when it comes out.
Other posts in this series with Nick Sutterer:
- Trailblazer Office Hours: A New Architecture For Rails by Nick Sutterer
- Tutorial: Validations and Classes in Trailblazer