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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
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
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
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

We're in Beta

About us Mission Statement