Questions tagged [gen-server]

gen_server stands for "generic server", a module behaviour in Erlang.

315 questions
37
votes
4 answers

How to perform actions periodically with Erlang's gen_server?

I want to start a gen_server that additionally, will perform one action every minute. What is the best way to schedule that?
Piotr Usewicz
  • 620
  • 2
  • 6
  • 13
27
votes
1 answer

Does Elixir provide an easier way to get a GenServer process's current state?

Given a simple GenServer process. defmodule KVServer do use GenServer def start do GenServer.start(__MODULE__, %{}, name: :kv_server) end def store(k, v) do GenServer.cast(:kv_server, {:store, k, v}) end def…
sbs
  • 4,102
  • 5
  • 40
  • 54
26
votes
3 answers

Graceful shutdown of GenServer

I writing an Elixir app with GenServer that starts an external application on boot and shuts it down and does other clean-up on exit. I've added bootup functionality in the init/1 callback and cleanup code in the terminate/2 callback. The init code…
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
16
votes
4 answers

What's the benefit of registering name using {:via, module, term} in GenServer.start_link/3?

In GenServer.start_link/3 I can register a name locally using an atom for a process like this: defmodule Worker do use GenServer def start_link do GenServer.start_link(__MODULE__, nil, name: :worker) end end Then I can start a supervisor…
sbs
  • 4,102
  • 5
  • 40
  • 54
14
votes
2 answers

Erlang: difference between using gen_server:cast/2 and standard message passing

I was working though a problem and noticed some code where a previous programmer was passing messages using the standard convention of PID ! Message. I have been using gen_server:cast/2. I was wondering if somebody could explain to me the critical…
RockyMountainHigh
  • 2,871
  • 5
  • 34
  • 68
13
votes
2 answers

Getting gen_server/gen_fsm state for debugging

Is it possible to obtain the current state of a gen_server process (presumably by sending some system message)? It could be useful when debugging. Of course, I can add a message which returns the current state to handle_call: get_state(Server) ->…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
13
votes
1 answer

gen_server with a dict vs mnesia table vs ets

I'm building an erlang server. Users sends http requests to the server to update their status. The http request process on the server saves the user status message in memory. Every minute the server sends all messages to a remote server and clear…
pablo
  • 2,719
  • 11
  • 49
  • 67
12
votes
1 answer

How to handle timeouts in poolboy?

I have a problem with a long-time consuming migration, which I desired to run in the parallel (it can be runned in the parallel). Actually migration is about taking all records in the database and implement time- and resource- consuming operations…
Alex Antonov
  • 14,134
  • 7
  • 65
  • 142
11
votes
2 answers

In Erlang, what's the difference between gen_server:start() and gen_server:start_link()?

Can someone explain what's the difference between gen_server:start() and gen_server:start_link()? I've been told that it's something about multi threading stuff. EDIT: If my gen_server is called from multiple threads, will it execute them all at…
Guga Melkadze
  • 275
  • 2
  • 12
10
votes
1 answer

when to use Gen_Fsm and when to use Gen_Server?

After checking out Gen_Fsm and Gen_Server documents, I found that, more or less, they act as similar behavior. In my opinion, if there is one loop function for sending broadcast or listening tcp sock, it is better to use Gen_Fsm, or else to use…
why
  • 23,923
  • 29
  • 97
  • 142
10
votes
2 answers

Proper way to structure GenServer calls to self

I know it's pretty much impossible to have a GenServer process call itself because you essentially hit a deadlock. But, I'm curious if there's a preferred way to do this kind of thing. Assume the following scenario: I've got a queue that I'm popping…
Micah
  • 17,584
  • 8
  • 40
  • 46
10
votes
3 answers

erlang OTP Supervisor crashing

I'm working through the Erlang documentation, trying to understand the basics of setting up an OTP gen_server and supervisor. Whenever my gen_server crashes, my supervisor crashes as well. In fact, whenever I have an error on the command line, my…
drfloob
  • 3,154
  • 1
  • 24
  • 31
10
votes
1 answer

My supervisor crashes when I try to start it from eshell?

I'm very new to OTP, I'm trying to create simple example to understand supervisor behaviour: Here is simple increment server -module( inc_serv ). -behaviour( gen_server ). -export( [ start/0, inc/1, stop/0 ] ). -export( [ init/1, handle_call/3,…
stemm
  • 5,960
  • 2
  • 34
  • 64
8
votes
3 answers

What kind of types can be sent on an Erlang message?

Mainly I want to know if I can send a function in a message in a distributed Erlang setup. On Machine 1: F1 = Fun()-> hey end, gen_server:call(on_other_machine,F1) On Machine 2: handler_call(Function,From,State) -> {reply,Function(),State) Does…
Federico
  • 5,438
  • 5
  • 39
  • 47
8
votes
2 answers

How to implement a resetable countdown timer with a GenServer in Elixir or Erlang

How do we implement a reset-able countdown timer with a GenServer? 1) perform a task after fixed amount of time, say every 60 seconds 2) have a way to reset the countdown back to 60 seconds before the timer elapses I have looked at How to perform…
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
1
2 3
20 21