silverdr
Spawn an asynchronous worker
https://dashbit.co/blog/you-may-not-need-redis-with-elixir contains a sentence: “even if you prefer to deliver emails outside of the request, in order to send an earlier response to users, you can spawn an asynchronous worker […]”. Now, is it about something as simple as:
Task.start(
fn ->
Email.important_stuff(user)
|> Mailer.deliver_now!
end
)
or did the author have something else / more sophisticated in mind? What is the typical way of “spawning an asynchronous worker” from within a request-serving process?
Most Liked
stefanchrobot
Almost that, except you should start the task under a supervision tree:
children = [
{Task.Supervisor, name: MyApp.TaskSupervisor}
]
Task.Supervisor.start_child(MyApp.TaskSupervisor, fn ->
IO.puts "I am running in a task"
end)
From the docs:
Note that the spawned process is not linked to the caller, but only to the supervisor. This command is useful in case the task needs to perform side-effects (like I/O) and you have no interest on its results nor if it completes successfully.
But if you’re on PostreSQL, then consider Oban. It’s awesome.
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
- #code-sync
- #javascript
- #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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










