From a627842717dbf832e6f56b87b14d88b7ccd612de Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Sat, 13 Jun 2020 20:56:02 -0400 Subject: [PATCH] Update/fix Omnibot.Irc.route_msg/2 to spawn a new task for each module, instead of a single task for all modules Previously, if a message was supposed to be handled by 3 modules, they would all be handled, synchronously, in the same process. If any of them crashed, it would affect any other modules that needed to be processed ahead of it. Now, a new task is spun up for each module, so module handlers are now indpendent. Signed-off-by: Alek Ratzloff --- lib/irc.ex | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/irc.ex b/lib/irc.ex index 8a9b3f8..876ed5c 100644 --- a/lib/irc.ex +++ b/lib/irc.ex @@ -30,8 +30,15 @@ defmodule Omnibot.Irc do defp route_msg(irc, msg) do channel = Msg.channel(msg) + State.channel_modules(channel) - |> Enum.each(fn {module, _} -> module.on_msg(irc, msg) end) + |> Enum.each(fn {module, _} -> + # Create a new task for each module + {:ok, _task} = Task.Supervisor.start_child( + Omnibot.RouterSupervisor, + fn -> module.on_msg(irc, msg) end + ) + end) end ## Server callbacks @@ -76,11 +83,7 @@ defmodule Omnibot.Irc do msg = Msg.parse(line) # Send the message to the router - irc = self() - {:ok, _task} = Task.Supervisor.start_child( - Omnibot.RouterSupervisor, - fn -> route_msg(irc, msg) end - ) + route_msg(self(), msg) {:noreply, socket} end end