Add default_config, and cfg[:timeout] for tasks
- Using the @default_config attribute in a module will fill out a default value. - cfg[:timeout] sets the timeout for the task for this module to finish. - Update Omnibot.Irc.route_msg/2 to have each task spawn a second task, while the first task waits for its child to finish, otherwise killing it. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
22
lib/irc.ex
22
lib/irc.ex
@@ -32,12 +32,22 @@ defmodule Omnibot.Irc do
|
||||
channel = Msg.channel(msg)
|
||||
|
||||
State.channel_modules(channel)
|
||||
|> Enum.each(fn {module, _} ->
|
||||
|> Enum.each(fn {module, mod_cfg} ->
|
||||
# Create a new task for each module
|
||||
{:ok, _task} = Task.Supervisor.start_child(
|
||||
Omnibot.RouterSupervisor,
|
||||
fn -> module.on_msg(irc, msg) end
|
||||
)
|
||||
{:ok, _task} =
|
||||
Task.Supervisor.start_child(
|
||||
Omnibot.RouterSupervisor,
|
||||
fn ->
|
||||
task = Task.Supervisor.async(
|
||||
Omnibot.RouterSupervisor,
|
||||
fn -> module.on_msg(irc, msg) end
|
||||
)
|
||||
|
||||
# Time out after 10 seconds by default
|
||||
timeout = mod_cfg[:timeout] || 10_000
|
||||
Task.await(task, timeout)
|
||||
end
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -54,7 +64,7 @@ defmodule Omnibot.Irc do
|
||||
# Wait for first message
|
||||
send_msg(self(), "NICK", cfg.nick)
|
||||
send_msg(self(), "USER", [cfg.user, "0", "*", cfg.real])
|
||||
:inet.setopts(socket, [active: true])
|
||||
:inet.setopts(socket, active: true)
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user