jarvism

jarvism

Controller ack POST ASAP and pass data to another function for processing

Hello, all!
I need to be able to get POST data in my Phoenix API, acknowledge as quickly as possible, then use the POST data to process in another function.
My controller looks like this:

def just_ack(conn, params) do

    process_data(params)

    json(conn, %{ack: "Attempting to process data"})
  end
...
defp process_data(params) do

    # Process the params data
  end

But this waits for process_data to complete before returning. What is the right way to get the POST data, ack quickly, then process the data?

Thank you in advance!

Marked As Solved

krasenyp

krasenyp

If you want to supervise the processing, you can employ a Task.Supervisor and start children in your controller.

Also Liked

dimitarvp

dimitarvp

Just spawn a new process to do the work in the background?

jarvism

jarvism

In case it’s useful for anyone landing here later…
I ended up making my controller like this:

def send_msg(conn, params) do
    # Use Task to handle send msg in background
    # while returning ASAP
    Task.start_link(fn ->
      IO.puts "I am running in a task"
      sendMsg(params)
    end)

    IO.puts("EXIT send_msg")
    json(conn)
  end

and the logs show “EXIT send_msg” before “I’m running in a task.” This is great for me because I need the API call to “fire and forget”, as the Task documentation describes:

We encourage developers to rely on supervised tasks as much as possible… Here is a summary:

and here:

By default, the functions Task.start and Task.start_link are for fire-and-forget tasks, where you don’t care about the results or if it completes successfully or not.

jarvism

jarvism

Thanks for this, @dimitarvp!
I did not know about this. I was able to read some of your other answers to similar questions and get a solution in place.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New

We're in Beta

About us Mission Statement