From 4b269675d5799011fa931caedc41215db999fbee Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Sun, 14 Jun 2020 18:00:31 -0400 Subject: [PATCH] Clean up tasks being started by Omnibot.Irc.route_msg/2 Previously, tasks would be started with an auxiliary task that would time out based on the module's timeout value - i.e. two tasks per module per message. This was a little silly, so I've migrated to using Task.Supervisor.async_stream_nolink/4. The only downside is that module-defined timeout is not available for config, because all function calls need to have the same timeout. This can probably be fixed by breaking down the async_stream_nolink() function, but for now setting a hard 30 second timeout works well enough. Signed-off-by: Alek Ratzloff --- lib/irc.ex | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/irc.ex b/lib/irc.ex index 6a450b2..bad4842 100644 --- a/lib/irc.ex +++ b/lib/irc.ex @@ -29,26 +29,16 @@ defmodule Omnibot.Irc do def part(irc, channel), do: send_msg(irc, "PART", channel) defp route_msg(irc, msg) do - channel = Msg.channel(msg) + modules = Msg.channel(msg) |> State.channel_modules() - State.channel_modules(channel) - |> Enum.each(fn {module, mod_cfg} -> - # Create a new task for each module - {:ok, _task} = - Task.Supervisor.start_child( - Omnibot.RouterSupervisor, - fn -> - task = Task.Supervisor.async( - Omnibot.RouterSupervisor, - fn -> module.on_msg(irc, msg) end - ) + Task.Supervisor.async_stream_nolink( + Omnibot.RouterSupervisor, + modules, + fn {module, _mod_cfg} -> module.on_msg(irc, msg) end, + timeout: 30_000, + on_timeout: :kill_task + ) |> Stream.run() - # Time out after 10 seconds by default - timeout = mod_cfg[:timeout] || 10_000 - Task.await(task, timeout) - end - ) - end) end ## Server callbacks