codecakes
How to Supervisor.start_child in a Supervision Tree?
I have an app like
stackdelivery
├── lib
└── stackdelivery
└── application.ex
└── stackgen
└── stackgen.ex
└── supervisor.ex
stackdelivery.ex
mix.exs
application.ex is
defmodule Stackdelivery.Application do
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
use Supervisor
def start(_type, _args) do
import Supervisor.Spec, warn: true
# Define workers and child supervisors to be supervised
children = [
supervisor(Stackdelivery.StackGen.Supervisor, [])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Stackdelivery.StackAppSupervisor]
Supervisor.start_link(children, opts)
end
end
supervisor.ex is
defmodule Stackdelivery.StackGen.Supervisor do
@moduledoc """
Supervisor for StackGen module
"""
use Supervisor
alias Stackdelivery.Stack
def start_link do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
IO.puts "starting StackGen.Supervisor supervisor.."
children = [
worker(Stack, [])
]
opts = [strategy: :simple_one_for_one, name: Stackdelivery.StackGenSupervisor]
supervise(children, opts)
end
def shoot, do: Supervisor.start_child(__MODULE__, [])
end
stackgen is a simple Stacn Genserver. for brevity:
defmodule Stackdelivery.Stack do
use GenServer
def init(_) do
IO.puts "Starting a New GenServer Stack"
{:ok, []}
end
....
I can’t start multiple child processes using Stackdelivery.StackGen.Supervisor.shoot
The way I am thinking is how supervision tree should be is:
Application (Main Supervisor)
└── StackGen.Supervisor (Module Supervisor)
└── Stackgen (GenServer)
Now when Application.start_link happens, how can we invoke child proceses in StackGen.Supervisor for Stackgen workers?
Most Liked
voughtdq
Supervisor.start_child/2 needs a spec! See Supervisor.Spec
Also, I think it has to have an id if you’re starting multiple GenServers of the same module.
def shoot(unique_name) do
spec = worker(Stackgen, [], [id: unique_name, restart: :transient])
Supervisor.start_child(__MODULE__, spec)
end
4
Popular in Questions
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
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
How to handle excepions in elixir?
Suppose i have A, B, C ,D, E modules. and each module has get() function.
A.get() method will call t...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









