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 <alekratz@gmail.com>
This commit is contained in:
2020-06-14 18:00:31 -04:00
parent 76d96c2fe2
commit 4b269675d5

View File

@@ -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