stefpankov
Hey there,
I’m new to Elixir and OTP and coming from an OO background, I’m still wrapping my head around Processes, Supervisors, Tasks etc.
I’m currently working on a little program that has to go through a bunch of files, read from them, parse the data and do something with it.
I’m trying to speed up this process by using Tasks and I’ve setup a TaskSupervisor which I then use to dynamically start Tasks that parse the data from the file.
It looks something like this
def do_the_thing(dir) do
for file <- File.ls!(dir) do
file_name = "#{dir}/#{file}"
File.read!(file_name)
|> Parser.parse!()
|> Enum.each(fn parsed_data ->
Task.Supervisor.start_child(MyApp.TaskSupervisor, MyTask, :run, [
parsed_data
])
end)
end
end
Now this works fine but when an error is raised and MyTask fails, I want to somehow trap the exception and log it in a file with additional metadata so I can see where it failed.
I had a try rescue block inside the MyTask.run function but I have a feeling that’s unnecessary since I’m fine with the Task failing, the ever vigilant Supervisor handles that for me just fine.
I guess my question is, what’s considered best practices when I want to catch exit reasons and log if they interest me. I tried to setup something with Process.monitor but couldn’t really figure out how to match exceptions and not :normal exits.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
david_ex
I’m not sure your approach is the best for what you want to achieve (although I’m no expert): although you’ll get the pid of the child process from
start_child(and could therefore monitor it), it the task fails and restarts you won’t be able to “re-monitor” it.If handling the first failure is good enough for you, the basic steps would be
That said, and depending on what you want to do, you can also use a GenServer and track the state of ongoing tasks. Then, you can use one of the
Task.Supervisor.async_nolinkto trigger a task and store the ref:Where
stateis the GenServer state. To handle task results, usehandle_info:stefpankov
Thanks for your answer!
Regarding re-monitoring tasks, for now, they’re not contacting any external service so the only fail reason is missing data that is crucial, that’s why I want to get to the exception that caused them to fail and format it nicely with more information so I know exactly what caused them to fail. That means they won’t be restarted after failure and that works just fine, the only thing I need to setup is that monitoring logic.
I like the GenServer approach and read a suggestion about it somewhere else but with no example of how to do it. This is extremely helpful, I’ll try it and post progress here.
lud
Hello,
Not sure if it would fit with your supervision tree, but sometimes I just use this pattern to handle failing tasks:
stefpankov
I went with a solution similar to this, a GenServer that acts as a monitor and it works as expected.
I just wanted to ask you about the reason you used async_nolink instead of start_child.
From what I can gather, the async function seem to be oriented towards the async/await approach and start_child seems to be more geared toward the fire and forget approach.
david_ex
Only because it’s slightly more useful as it allows the GenServer to do something with the task result (and I had recently done something similar). But if you don’t need that,
start_childshould work fine also.stefpankov
Okay, thanks for the answer, this should do the job for now