francois

francois

What is different between a supervisor and a worker?

Hi!

I read Supervisor.Spec – Elixir v1.4.2 multiple times, but I still don’t understand what’s different between a worker and a supervisor? Is it just that the supervisor is used to supervise? And the worker should be doing work?

If that is the case, then the skeleton code below should be a worker rather than a supervisor?

defmodule Notifier do
  def init(state) do
    Notifier.schedule_send_push_notifications
    {:ok, state}
  end

  def handle_info(:send_push_notifications, state) do
    {:ok, pid} = Task.start_link(fn -> Notifier.send_push_notifications(self(), state[:last_notified_at]) end)
    %{state | notifier_task_pid: pid}
  end

  def schedule_send_push_notifications() do
    Process.send_after(self(), :send_push_notifications, 30*1000)
  end

  def send_push_notifications(from, last_notified_at) do
    # send push notifications
    # reply with correct last_notified_at
  end
end

The Notifier process is responsible for starting a task when it’s time to send the push notifications, but beyond that, this process holds some state and passively waits for messages. It feels like my understanding of workers vs supervisors is flawed.

Thanks!
François

Most Liked

wmnnd

wmnnd

You can create supervised children in a Supervisor with worker/3 and supervisor/3.

While worker/3 is used to start generic processes, supervisor/3 is specifically for processes that are also Supervisors. Like this you can have Supervisors that are supervised by other Supervisors, eventually creating the Supervision tree.

The module you’ve posted here is not a Supervisor, so it should be started with worker/3.

zambal

zambal

Yes, apart from supervising, supervisors should do as little as possible. While your Notifier module is pretty simple and could technically be implemented as a supervisor, the general mindset in the Erlang and Elixir community is to implement it as a worker.

I think the most important reasons are that a supervisor should not crash from buggy application code and that it should do as little work as possible in order to be able to react quickly to crashing workers.

Edit: forgot to mention that supervisors are implemented as gen_servers, so supervisors are internally no different than any other process in OTP

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement