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
If you want to supervise the processing, you can employ a Task.Supervisor and start children in your controller.
Also Liked
dimitarvp
Just spawn a new process to do the work in the background?
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:
- Use
Task.Supervisor.start_child/2to start a fire-and-forget task and you don’t care about its results nor about if it completes successfully- Use
Task.Supervisor.async/2+Task.await/2allows you to execute tasks concurrently and retrieve its result. If the task fails, the caller will also fail- Use
Task.Supervisor.async_nolink/2+Task.yield/2+Task.shutdown/2allows you to execute tasks concurrently and retrieve their results or the reason they failed within a given time frame. If the task fails, the caller won’t fail: you will receive the error reason either onyieldorshutdown
and here:
By default, the functions
Task.startandTask.start_linkare for fire-and-forget tasks, where you don’t care about the results or if it completes successfully or not.
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








