citxx
citxx

Reputation: 2545

How to implement temporary workers in Erlang/OTP

What is the most conventional way of implementing a process, which only should do its work and exit?

In my case these workers are supervised by simple_one_for_one supervisor. I was searching around, but haven't found anything better than just spawn_link:

work(Args) ->
{ok, spawn_link(fun() ->
    ... do_the_necessary_work ...
end)}.

Nevertheless, this way doesn't seem to be the good OTP design.

Any ideas?

Upvotes: 0

Views: 266

Answers (1)

demeshchuk
demeshchuk

Reputation: 742

That's actually a decent way, just make sure you are using proc_lib:spawn_link(...) instead of erlang:spawn_link(...) (to store ancestors for supervision tree traversing, and for more verbose error logs).

And, of course, you might want to make restart strategy for all workers set to temporary or transient, depending on what you are going to do in case of worker failure (restart the worker and attempt to do the job once again or discard the job in case of any failures).

Upvotes: 4

Related Questions