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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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...
3268 119930 1237
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement