<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manuel González Merino on Medium]]></title>
        <description><![CDATA[Stories by Manuel González Merino on Medium]]></description>
        <link>https://medium.com/@mgmerino?source=rss-e14e2861df35------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*O4EryxNpMFryYnBXYUPmaw.png</url>
            <title>Stories by Manuel González Merino on Medium</title>
            <link>https://medium.com/@mgmerino?source=rss-e14e2861df35------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:37:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mgmerino/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Command pattern: how and why we use it]]></title>
            <link>https://medium.com/jobandtalenteng/command-pattern-how-and-why-we-use-it-fa8af952bca1?source=rss-e14e2861df35------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa8af952bca1</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Manuel González Merino]]></dc:creator>
            <pubDate>Fri, 05 Jul 2019 08:47:37 GMT</pubDate>
            <atom:updated>2019-09-25T08:07:31.241Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pmS_ir6hZpFgVBwS4qxgjw.png" /></figure><p>Here at Jobandtalent, we face numerous challenges every day. One of them is to make it easier for people to use our software, and covering us on worst case scenarios.</p><p>Our internal ops people work with large amounts of data, scheduling rosters for hundreds of employees, distributed along with a handful of locations. Some of these operations come along with some other automated actions, like sending emails and changing the status of other actors. The UI that allows us to prepare and trigger these events has to be good enough to avoid unintentional mistakes, and we have put a lot of effort into achieving this.</p><p>When we are building features that involve direct interaction with the user interface, we design the feature keeping things like this in mind:</p><ul><li>Do we need to allow undo/redo operations?</li><li>Is it a <em>batch</em> operation?</li><li>Do we need to keep track of <em>who</em> triggered the operation?</li><li>Do we need to keep track of the <em>current</em> and <em>past</em> <em>status</em> of the operation?</li><li>Do we need to track <em>internal</em> data of the operation?</li><li>Does the operation need to be <em>transactional</em>?</li></ul><p>These kinds of problems can be approached with the command pattern.</p><h3>What is the command pattern?</h3><p>In short, the command pattern enforces the design encapsulation of a request or an action as an object, so you can parameterize all the data for every request, queue it, log it, and support un/redoable operations. The most simple analogy is to think of it as a text note application, where you are allowed to undo and redo using the history of the document. Actually, almost every modern user interface that involves some kind of editing operation, supports this.</p><p>Let’s think about a more specific use case. Imagine that you want to schedule a collection of shifts, distributed in a week, and match a collection of workers to those shifts. Plus, you want to optimize this match adding constraints and worker preferences (this example will support the following definitions).</p><p>To do that, we could use an external service to do the calculations. Sending it a payload of workers, shifts, locations, and constraints. The service would respond to us with a payload of matches between workers and shifts. Ideally, we want to keep track of:</p><ul><li><em>Who</em> triggered the event</li><li>Whether it was a <em>single</em> or <em>batch</em> operation</li><li>Whether it was <em>scheduled</em> or executed at that moment</li><li>The data <em>payload</em> we <em>sent</em> to the service</li><li>The data <em>payload</em> we <em>received</em> from the service</li><li>Other internal <em>props</em> that allow us to redo/undo the operation</li><li>If the operation should be <em>idempotent</em></li></ul><p>Given that we work with considerable amounts of data, we prefer to schedule the action, tear up the process and log it more thoroughly. We also need to keep the operation transactional so, in case of some failure arises, we are able to rollback database commits.</p><p>What we design when creating a new feature is based on two concepts: an <strong>action</strong> to implement behaviour and a <strong>runner</strong> to decide the strategy when running the action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*_74ao_920a4OOPSEPRSXIw.png" /><figcaption>Runner schematic implementation</figcaption></figure><p><a href="https://jobandtalent.engineering/adopting-elixir-the-bff-case-study-b575a81ec794">Adopting Elixir: The BFF Case study</a></p><h3>Action: design and lifecycle</h3><p>Take a look at this class implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8828a50ef9818cd180f584c006f3ea3/href">https://medium.com/media/f8828a50ef9818cd180f584c006f3ea3/href</a></iframe><p>The example above is built on top of ActiveRecord::Base superclass to handle the transaction work. In turn, the next example will implement the former class as base to model a customized action, in which we can add, in the form of a JSONB field, the data we want to log:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97aefd91800b224481de4262a4cea7ee/href">https://medium.com/media/97aefd91800b224481de4262a4cea7ee/href</a></iframe><p>With this example, we have designed an action that could prepare a payload, then send it to an external service, and finally save the results. Plus, we could save some interesting props about the status and data of the action while it is being performed.</p><p>The method process_data called in the example above, could, for example, hold three private method calls, one would handle the logic to build a payload, another one would send it to the external service, and the last one would persist the results.</p><p>Some issues could come up here. Given that we depend on an external service and the action could be scheduled to perform later on —some data could change while the action is being completed. To keep track of what could go wrong, we define the following states to properly mark the action in specific scenarios:</p><ul><li><strong>Waiting to run</strong>, init state.</li><li><strong>Running</strong>, the action is now performing. The runner set this state.</li><li><strong>Succeeded</strong>: everything went well, i.e., the data payload could be formed, the service had no problems with the data we sent and then responded with a result that we saved successfully in the database.</li><li><strong>Failed</strong>: some business rule or constraint didn’t allow us to perform the action. For example, there could be changes to the availability of the workers, deleted shifts or some other scenarios where the data we need to send to the other service is malformed or not available in that specific moment.</li><li><strong>Crashed</strong>: a tech failure. Connectivity issues, response times longer than expected, etc.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LB16ZXJC9alOgIfKWPjraw.png" /><figcaption>Action’s Lifecycle</figcaption></figure><p>Those states are managed in part by the action and in part by the runner. Specifically, in case the action has crashed, it is desirable to re-schedule it to try again later, so the runner will manage the status and will ask for more retries. On the other hand, succeeded and failed states are managed by the action.</p><h3>Runner: inline and async</h3><p>Now it’s time to think about performing our action. Do we want to execute it at the very moment? Or do we want to wait to some point in the future?</p><p>Both approaches are supported thanks to an implementation that allows us to:</p><ul><li>Launch a Runner that executes the action inline (same thread)</li><li>Run the given action in the background using Sidekiq to manage the queues.</li></ul><p>The base class to achieve this could be wrapped like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2968a24c12f6af99b9ea942fcd668fc/href">https://medium.com/media/e2968a24c12f6af99b9ea942fcd668fc/href</a></iframe><p>Now, following this next implementation using the former as a base class, the inline runner simply runs the action as an inline method:</p><p>Actions::Runner.run self</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cde2d08afd0ee1485ae599332983338/href">https://medium.com/media/0cde2d08afd0ee1485ae599332983338/href</a></iframe><p>In some slightly different fashion, the async runner works with Sidekiq retries and only crashes after all the retries have expired. We can pass as well the action method we want to run.</p><p>The Worker implementation that handles the async work under the hood is the following (some details have been omitted for the sake of brevity):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/faecf41da02b41e683bf600c690e0a17/href">https://medium.com/media/faecf41da02b41e683bf600c690e0a17/href</a></iframe><h3>Audit trail</h3><p>On top of the previous features, we finally would want a good audit trail about what happened when performing the action. To be more precise:</p><ul><li><strong>Account</strong>, as a scope</li><li><strong>Initiator</strong>: the user/component that started the action</li><li><strong>Target</strong>: the thing that we are acting upon</li><li><strong>Timestamps</strong></li><li>Action-specific configuration/data payload</li></ul><p>As all of the properties, except the last one, are general enough to be applied in business rules, we can take advantage of a JSONB field. This helps us to create a specific data structure to hold other kinds of information we want to track when designing new actions.</p><p>All this data could be nicely formatted and available in a backoffice for devs as well as for ops to check their work, and do some troubleshooting in case of errors.</p><p>We can also undo a series of actions and redo them exactly to where they were before with relative ease, thanks to the detailed logging data we have.</p><h3>Conclusion</h3><p>The point behind this approach is to be able to treat operations that come along with big amounts of data thoroughly, especially for those non-friendly scenarios where things went wrong.</p><p>The states will show us where the action failed and hopefully, why.</p><p>Logging all the data involved in the operation brings us the possibility of undoing mistakes, and redoing them just feeding input into the action.</p><p>Also, avoiding wrongly re-doing several batches of hundreds of actions gives peace of mind to our platform engineers.</p><p>Sometimes, there is a set of obscure business rules that only operations people know, or at least, know the <em>why</em> more deeply than developers. Sharing error responses to them through the UI, conveniently translated into their jargon, usually results in spotting the error quicker.</p><p>Finally, being in possession of the payload which the action sent to external/third party services, is tremendously helpful when debugging issues between both ends.</p><p>Obviously, this approach is not always valid or the silver bullet for all of our problems. As always, we must first think in the internals of our intention to choose the best pattern, and if you read the article, now you will have a basic idea of what kind of problems could be solved with this implementation.</p><p>Looking forward, we want to include the following ideas in future iterations of development:</p><ul><li>An external workflow/actions engine</li><li>Reusability between services</li><li>Orchestration of complex flows (multiple services, human interaction, per client workflows…)</li></ul><p><em>Acknowledgments: special thanks to </em><a href="https://twitter.com/spejman"><em>Sergio Espeja</em></a><em>, </em><a href="https://twitter.com/GermanDZ"><em>German Del Zotto</em></a><em>, </em><a href="https://twitter.com/luisrecuenco"><em>Luis Recuenco</em></a><em> and </em><a href="https://twitter.com/john_mclachlan"><em>John McLachlan</em></a><em> for his valuable feedback while writing this article.</em></p><h4>We are hiring!</h4><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=twitter&amp;url=https%3A//twitter.com/jobandtalenteng/status/1125743791196049409&amp;image=" width="500" height="281" frameborder="0" scrolling="no"><a href="https://medium.com/media/7171964a0e0f5cd8313526de1e33393f/href">https://medium.com/media/7171964a0e0f5cd8313526de1e33393f/href</a></iframe><p>If you want to know more about it’s like work at Jobandtalent you can read the first impressions of some of our teammates in this <a href="https://jobandtalent.engineering/our-first-impressions-working-at-jobandtalent-part-1-991a48eac2a4">blog post</a> or visit our <a href="https://twitter.com/jobandtalentEng">twitter</a>.</p><p><a href="https://jobandtalent.engineering/hack-talent-2019-jobandtalent-engineering-hackathon-2489e978e011">Hack&amp;Talent 2019: Jobandtalent Engineering Hackathon</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa8af952bca1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jobandtalenteng/command-pattern-how-and-why-we-use-it-fa8af952bca1">Command pattern: how and why we use it</a> was originally published in <a href="https://medium.com/jobandtalenteng">Job&amp;Talent Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>