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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement