alex88

alex88

Genserver isn't restarted on graceful shutdown

I’ve a very simple genserver, I start it like this:

defmodule MyApp.MatchKiller do
  use GenServer

  def start_link() do
    GenServer.start_link(__MODULE__, [], name: :match_killer)
  end

and in my app start function I call

Swarm.register_name("match_killer", MyApp.MatchKiller, :start_link, [], 15_000)

So I start two iex sessions and the process is started and registered globally:

[debug] [swarm on a@AlessandrosiMac] [tracker:handle_call] registering "match_killer" as process started by Elixir.MyApp.MatchKiller.start_link/0 with args []
[debug] [swarm on a@AlessandrosiMac] [tracker:do_track] starting "match_killer" on a@AlessandrosiMac
[debug] [swarm on a@AlessandrosiMac] [tracker:do_track] started "match_killer" on a@AlessandrosiMac
[debug] [swarm on b@AlessandrosiMac] [tracker:sync_registry] local tracker is missing "match_killer", adding to registry
[info] [swarm on b@AlessandrosiMac] [tracker:awaiting_sync_ack] local synchronization with a@AlessandrosiMac complete!
[info] [swarm on b@AlessandrosiMac] [tracker:resolve_pending_sync_requests] pending sync requests cleared
[debug] [swarm on b@AlessandrosiMac] [tracker:handle_call] registering "match_killer" as process started by Elixir.MyApp.MatchKiller.start_link/0 with args []
[debug] [swarm on b@AlessandrosiMac] [tracker:do_track] found "match_killer" already registered on a@AlessandrosiMac

now, since the process is running on a if I ctrl-c a on node a the process is restarted on host b:

[debug] [swarm on b@AlessandrosiMac] [tracker:handle_topology_change] topology change (nodedown for a@AlessandrosiMac)
[debug] [swarm on b@AlessandrosiMac] [tracker:handle_topology_change] restarting "match_killer" on b@AlessandrosiMac
[debug] [swarm on b@AlessandrosiMac] [tracker:do_track] starting "match_killer" on b@AlessandrosiMac
[debug] [swarm on b@AlessandrosiMac] [tracker:do_track] started "match_killer" on b@AlessandrosiMac

So far so good, the problem is that if instead gracefully shutdown sending a SIGTERM the process is just shut down (this is a when I send SIGTERM to b which is running the process):

iex(a@AlessandrosiMac)10> [debug] [swarm on a@AlessandrosiMac] [tracker:handle_monitor] "match_killer" is down: :shutdown
[info] [swarm on a@AlessandrosiMac] [tracker:nodedown] nodedown b@AlessandrosiMac
[debug] [swarm on a@AlessandrosiMac] [tracker:handle_topology_change] topology change (nodedown for b@AlessandrosiMac)
[info] [swarm on a@AlessandrosiMac] [tracker:handle_topology_change] topology change complete

if I then restart b (while a is still running) the process is restarted on b because probably it sees that it’s not running.

the problem here is that when I scale down the nodes it won’t restart the process and it won’t transfer its state. Am I missing something?

Most Liked

balena

balena

Hey @alex88,

I don’t know if you still have this problem, but recently I had exactly the same problem at my side using Swarm and decided that it needs a very simple feature from the regular Supervisor implementation: a restart flag, which indicates how your process should live, cluster-wise. The restart parameter assumes the same values as those passed to Supervisor child spec (check hexdocs.pm):

  • :permanent means a handoff will always occur,
  • :transient means that the process will be shut down only if the termination reason is other than :normal | :shutdown | {shutdown, term} ,
  • :temporary means it is never restarted, only when the node gets down abruptly (generating a :DOWN event with reason :noconnection). This is the default, compatible with the original implementation.

I provided a PR, waiting for approval (or not). It’s here: Include a restart parameter by balena · Pull Request #139 · bitwalker/swarm · GitHub

Where Next?

Popular in Questions Top

New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement