jhefreyzz

jhefreyzz

Running GenServer only once

I created a GenServer to keep track of the current index in case the client reconnects to the socket. I intend to only use one GenServer.

I can make the GenServer start automatically by adding it to the children list of supervisor but my GenServer state is depended on the result of a query so I cannot put it there.

 children = [
      # Start the Ecto repository
      Senti.Repo,
      ...
      {Senti.QuestionState, [%{}]}
    ]

I started the GenServer in my join callback. I’m aware that the start_link will always be called when there are new clients connected but I have no idea where should I put this one.

I have my code below to describe more about my issue.

  def join("session:" <> session_id, _payload, socket) do
    presentation_state = Questionnaire.get_presentation_info(session_id)
    {:ok, _pid} = QuestionState.start_link(question_state(presentation_state))

    question_index = QuestionState.get_next_question_index() || 0

    {:ok, %{presentation: presentation_state, current_index: question_index}, socket}
  end

If information provided above is insufficient, I will update it ASAP.
Thank you.

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure

Application.start_phase/3 seems to be a good fit. Add a DynamicSupervisor to the main supervision tree and make it start and supervise your GenServer in the start_phase.

Also Liked

lud

lud

I am not sure you need start_phase at all. You can query your repo in the init of your GenServer and that will be executed before the supervisor starts the next child.

So you can start your repo, start the GenServer that depends on your repo, and then start the rest of the app (web stack with channels).

Where Next?

Popular in Questions Top

9mm
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
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JorisKok
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
svb
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 Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
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
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New

We're in Beta

About us Mission Statement