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!

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

3 Likes

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

2 Likes

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.

1 Like

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.

2 Likes