gmile

gmile

Design puzzle when using Ecto.Multi

In my application I need to perform multiple things during transaction:

  1. call HTTP and load some data form a microservice,
  2. update some DB records,
  3. add a new record DB record,
  4. send an SMS verification code.

Ecto.Multi suites this purpose very well, but it looks like I’m facing some roadblocks with it. I need someone else’s opinion on my design.

Currently the code for process above looks like this:

# declaration_controller.ex
def create(conn, %{declaration: declaration_attrs} do
  case DeclarationAPI.create(declaration_attrs) do
    {:ok, %{declaration: declaration} = result} ->
      render(conn, "show.json", result)
    {:error, key, reason, _}
      render(conn, "error.json", %{key => reason})
  end
end

# declaration_api.ex
def create_declaration(declaration_attrs) do
  Repo.transaction(new_declaration_request(declaration_attrs))
end

def new_declaration(declaration_attrs) do
  settings = SettingsMicroserviceAPI.get()

  Multi.new
  |> Multi.update_all(:pending_declarations, pending_declarations_query(declaration_attrs), set: [status: "CANCELLED"])
  |> Multi.insert(:declaration, changeset(declaration_attrs, settings))
  |> Multi.run(:verification_code, Verification, :send_verification_code, [declaration_attrs])
end

There’s a problem with this design. Sometimes SettingsMicroserviceAPI.get() HTTP call will not return expected data: it may stumble upon 404 or even time out. In this case transaction has to be rolled back gracefully, and an error saying something along the lines SettingsMicroserviceAPI has timed out
should be returned to user who requested the initial create action.

So I thought: why don’t I add this HTTP call as a Multi step like this?:

Multi.new
|> Multi.run(:fetch_settings, fn _ -> SettingsMicroserviceAPI.get() end)
|> Multi.update_all(:pending_declarations, pending_declarations_query(declaration_attrs), set: [status: "CANCELLED"])
# ...

However in this case there’s no way I can access the settings in the changeset.

So I see two ways to go from here:

  1. Keep the microservice call where it is now, but add a validation to the Ecto.Multi chain,
  2. Push the microservice call directly to changeset.

I tried both approaches and here’s what learned.

1. Keep the microservice call where it is now, but add a validation to the Ecto.Multi chain

The microservice call will always return {:ok, data} or {:error, :data}. This suites Ecto.Multi.run requirement for function’s return value so my code becomes this:

def new_declaration(declaration_attrs, user_id) do
  settings = SettingsMicroserviceAPI.get()

  Multi.new
  |> Multi.run(:fetch_settings, fn _ -> settings end)
  |> Multi.update_all(:pending_declarations, pending_declarations_query(declaration_attrs), set: [status: "CANCELLED"])
  |> Multi.insert(:declaration, changeset(pending_declaration_attrs, settings))
  |> Multi.run(:verification_code, Verification, :send_verification_code, [declaration_attrs])
end

Now, Ecto.Multi will validate the settings: in case of {:error, _} = settings the transaction will be rolled back, and a nice error will be returned to controller.

There are two problems with this, however:

  1. I have to update the signature of changeset from this:

    def changeset(attrs, settings)
      # ...
    end
    

    to this:

    def changeset(attrs, %{:ok, settings})
      # ...
    end
    

    which looks a little ugly.

  2. In case of {:error, _} = settings, I need to add another changeset function clause, as otherwise the code will raise a no matched function error.

2. Push the microservice call directly to changeset

I can do this (the actual code was reduced for brevity):

def create_changeset(attrs) do
  %Declaration{}
  |> cast(attrs, @required_fields)
  |> fetch_settings()
end

def fetch_settings(changeset) do
  result = SettingsMicroserviceAPI.get()

  case result do
    {:ok, settings} ->
      put_change(changeset, :_settings, settings)
    {:error, reason}
      add_error(changeset, :_settings, reason)
  end
end

This works, but with two caveats:

  1. In case I need to use settings across multi steps, inside verification code step for example, I’d have to rewrite the verification step to something like this:

    # ...
    |> Multi.run(:verification_code, fn multi ->
         settings = multi.declaration.get_change(:_settings)
    
         Verification.send_verification_code(declaration_attrs, settings))
       end)
    
  2. I’m not aware of a standard mechanism for storing arbitrary data alongside the changeset, so I have to use a temporal private _settings key.
    This works right now, but there’s no guarantee changeset becomes more strict in future, to disallow fields not described in the schema?


I’m sticking to the second solution right now as it seems to work (at least on the paper;
I’m yet to implement it). But I wonder how everyone handle HTTP interaction with 3rd parties
during transactions?

Maybe this design is inherently wrong, and there’s an “official way” to deal with these kinds
of situations.

Marked As Solved

wojtekmach

wojtekmach

Hex Core Team

Since the 3rd party call does not depend on any data within Multi I’d definitely keep it out. Seems like a good use case for with which will simply propagate the error from API:

with {:ok, settings} <- settings_api.get() do
  Ecto.Multi.new
  |> Multi.update_all(...)
  |> ...
  |> Repo.transaction
end

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement