Papey

Papey

Process monitoring inside a Nostrum Consumer

Hi everyone,

I’m working on a bot feature (blind test) on Discord using Nostrum.

A command is used to start the game and spin up a GenStateMachine.

I want to monitor a crash of this GenStateMachine in order to send a simple message to the users saying “kaboom!” to advertise about the crash.

For now, I’m starting the processus here, it works great but I can’t handle a process crash.

I tried this option but it does not work :

defmodule Game.Monitor do
  def run(game_data, dl_data) do
    {:ok, _} = Game.start_link(game_data, dl_data)

    ref = Process.monitor(Game)

    receive do
      {:DOWN, ^ref, :process, _from_pid, reason} -> IO.puts("Exit reason: #{reason}")
    end
  end
end

I don’t get why it does not pass in the receive branch.

I’m open to other solution if monitoring is not the good fit here.

Thanks for reading and thank your answers/feedback.

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @Papey I think I would do things differently. I think there is no point in starting a game in an “empty” state, I would simply spawn a game under a supervisor when they are requested. I would probably use a DynamicSupervisor btw because you’ll need to be able to dynamically start new games.

Secondly, Supervisor modules are not really user editable, you don’t really get to run custom code inside of them, doing so would decrease their reliability. If you want to do something on crash, then you should, in some other process, do {:ok, pid} = DynamicSupervisor.start_child(SomeSuperName, {Game, opts}) and then Process.monitor(pid). Then the game is linked to its supervisor, but is monitored by you.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You have linked the processes together with start_link. If the game crashes, it will also call whatever process is calling Game.Monitor.run. You should start the game under a supervisor.

Papey

Papey

Thanks for answer. I created a GenServer responsible of the Game monitoring and it works !

Here is the code if anyone reading this later needs it

defmodule Game.Monitor do
  @moduledoc """
  A simple GenServer used to monitor a game process
  """

  use GenServer
  require Logger

  def init(channel_id) do
    # On init try to attach to an existing game process
    # In case of Monitor failure, this will reattach the monitor to the game process
    if Process.whereis(Game) do
      Process.monitor(Game)
    end

    {:ok, channel_id}
  end

  def start_link(channel_id) do
    GenServer.start_link(__MODULE__, channel_id, name: __MODULE__)
  end

  def monitor() do
    GenServer.cast(__MODULE__, :monitor)
  end

  def handle_cast(:monitor, channel_id) do
    Process.monitor(Game)
    {:noreply, channel_id}
  end

  def handle_info({:DOWN, _ref, :process, object, reason}, channel_id) when reason != :killed do
    Logger.info("Game process monitor detected a game crash", reason: reason, data: object)

    Nostrum.Api.create_message!(
      channel_id,
      embed: %Nostrum.Struct.Embed{
        :title => "KABOOM ALERT, I just explode, sorry.",
        :description => "💥",
        :color => Colors.get_color(:danger)
      }
    )

    {:noreply, channel_id}
  end
end

I now need to make it less static but I get the idea. Thaks a lot.

Where Next?

Popular in Questions Top

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
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
pmjoe
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
gausby
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...
1207 39467 209
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
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
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement