WIP: Supervisor-based plugin base
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
defmodule Omnibot.Contrib.Fortune do
|
defmodule Omnibot.Contrib.Fortune do
|
||||||
use Omnibot.Plugin.GenServer
|
use Omnibot.Plugin
|
||||||
|
|
||||||
@fortunes [
|
@fortunes [
|
||||||
"Reply hazy, try again",
|
"Reply hazy, try again",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
defmodule Omnibot.Contrib.Linkbot do
|
defmodule Omnibot.Contrib.Linkbot do
|
||||||
use Omnibot.Plugin.GenServer
|
use Omnibot.Plugin
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@default_config timeout: 30_000
|
@default_config timeout: 30_000
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
defmodule Omnibot.Contrib.Markov do
|
defmodule Omnibot.Contrib.Markov do
|
||||||
use Omnibot.Plugin.Supervisor
|
use Omnibot.Plugin
|
||||||
alias Omnibot.Contrib.Markov.Chain
|
alias Omnibot.Contrib.Markov.Chain
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
defmodule Omnibot.Contrib.OnConnect do
|
defmodule Omnibot.Contrib.OnConnect do
|
||||||
use Omnibot.Plugin.GenServer
|
use Omnibot.Plugin
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@default_config [channels: :all, commands: []]
|
@default_config [channels: :all, commands: []]
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def on_msg(irc, msg) do
|
def on_msg(irc, %Irc.Msg {command: "001"}) do
|
||||||
if msg.command == "001" do
|
Logger.info("Running OnConnect commands")
|
||||||
Logger.debug("Got welcome message")
|
cfg()[:commands]
|
||||||
cfg()[:commands]
|
|> Enum.each(fn [cmd | params] -> Irc.send_msg(irc, cmd, params) end)
|
||||||
|> Enum.each(fn [cmd | params] -> Irc.send_msg(irc, cmd, params) end)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
defmodule Omnibot.Contrib.Wordbot do
|
defmodule Omnibot.Contrib.Wordbot do
|
||||||
use Omnibot.Plugin.Supervisor
|
use Omnibot.Plugin
|
||||||
alias Omnibot.{Contrib.Wordbot, State, Util}
|
alias Omnibot.{Contrib.Wordbot, State, Util}
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
defmodule Omnibot.Core do
|
defmodule Omnibot.Core do
|
||||||
use Omnibot.Plugin.GenServer
|
use Omnibot.Plugin
|
||||||
alias Omnibot.State
|
alias Omnibot.State
|
||||||
|
|
||||||
@default_config [channels: :all]
|
@default_config [channels: :all]
|
||||||
@@ -33,7 +33,8 @@ defmodule Omnibot.Irc do
|
|||||||
Task.Supervisor.async_stream_nolink(
|
Task.Supervisor.async_stream_nolink(
|
||||||
Omnibot.RouterSupervisor,
|
Omnibot.RouterSupervisor,
|
||||||
plugins,
|
plugins,
|
||||||
fn {plugin, _plug_cfg} -> plugin.on_msg(irc, msg) end,
|
# Spin up tasks to handle messages in case handle_msg() hangs
|
||||||
|
fn {plugin, _plug_cfg} -> plugin.handle_msg(irc, msg) end,
|
||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
on_timeout: :kill_task
|
on_timeout: :kill_task
|
||||||
) |> Stream.run()
|
) |> Stream.run()
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ defmodule Omnibot.Plugin.Base do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#@callback handle_msg(irc :: pid(), msg :: %Omnibot.Irc.Msg{}) :: any
|
@callback handle_msg(irc :: pid(), msg :: %Omnibot.Irc.Msg{}) :: any
|
||||||
@callback on_msg(irc :: pid(), msg :: %Omnibot.Irc.Msg{}) :: any
|
@callback on_msg(irc :: pid(), msg :: %Omnibot.Irc.Msg{}) :: any
|
||||||
@callback on_channel_msg(irc :: pid(), channel :: String.t(), nick :: String.t(), line :: String.t()) :: any
|
@callback on_channel_msg(irc :: pid(), channel :: String.t(), nick :: String.t(), line :: String.t()) :: any
|
||||||
@callback on_channel_msg(
|
@callback on_channel_msg(
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
defmodule Omnibot.Plugin.GenServer do
|
|
||||||
defmacro __using__([]) do
|
|
||||||
quote do
|
|
||||||
use Omnibot.Plugin.Base
|
|
||||||
use GenServer
|
|
||||||
|
|
||||||
def start_link(opts) do
|
|
||||||
cfg = opts[:cfg]
|
|
||||||
state = opts[:state]
|
|
||||||
GenServer.start_link(__MODULE__, {cfg, state}, opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
def cfg() do
|
|
||||||
GenServer.call(__MODULE__, :cfg)
|
|
||||||
end
|
|
||||||
|
|
||||||
def state() do
|
|
||||||
GenServer.call(__MODULE__, :state)
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_state(update) do
|
|
||||||
GenServer.cast(__MODULE__, {:state, update})
|
|
||||||
end
|
|
||||||
|
|
||||||
## Server callbacks
|
|
||||||
|
|
||||||
@impl GenServer
|
|
||||||
def init({cfg, state}) do
|
|
||||||
state = state || on_init(cfg)
|
|
||||||
{:ok, {cfg, state}}
|
|
||||||
end
|
|
||||||
|
|
||||||
@impl GenServer
|
|
||||||
def handle_call(:cfg, _from, {cfg, state}) do
|
|
||||||
{:reply, cfg, {cfg, state}}
|
|
||||||
end
|
|
||||||
|
|
||||||
@impl GenServer
|
|
||||||
def handle_call(:state, _from, {cfg, state}) do
|
|
||||||
{:reply, state, {cfg, state}}
|
|
||||||
end
|
|
||||||
|
|
||||||
@impl GenServer
|
|
||||||
def handle_cast({:state, update}, {cfg, state}) do
|
|
||||||
{:noreply, {cfg, apply(update, [state])}}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
defmodule Omnibot.Plugin.Supervisor do
|
defmodule Omnibot.Plugin do
|
||||||
@default_opts [include_base: true, opts: [strategy: :one_for_one]]
|
@default_opts [include_base: true, opts: [strategy: :one_for_one]]
|
||||||
|
|
||||||
defmodule CfgState do
|
defmodule CfgState do
|
||||||
@@ -32,31 +32,44 @@ defmodule Omnibot.Plugin.Supervisor do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cfg() do
|
def cfg() do
|
||||||
Omnibot.Plugin.Supervisor.CfgState.cfg(__MODULE__.CfgState)
|
Omnibot.Plugin.CfgState.cfg(__MODULE__.CfgState)
|
||||||
end
|
end
|
||||||
|
|
||||||
def state() do
|
def state() do
|
||||||
Omnibot.Plugin.Supervisor.CfgState.state(__MODULE__.CfgState)
|
Omnibot.Plugin.CfgState.state(__MODULE__.CfgState)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_state(fun) do
|
def update_state(fun) do
|
||||||
Omnibot.Plugin.Supervisor.CfgState.update_state(__MODULE__.CfgState, fun)
|
Omnibot.Plugin.CfgState.update_state(__MODULE__.CfgState, fun)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@impl Omnibot.Plugin.Base
|
||||||
|
def handle_msg(irc, msg) do
|
||||||
|
GenServer.cast(__MODULE__, {:handle_msg, irc, msg})
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl Omnibot.Plugin
|
||||||
|
def children(_cfg, _state), do: []
|
||||||
|
|
||||||
## Server callbacks
|
## Server callbacks
|
||||||
|
|
||||||
@impl Supervisor
|
@impl Supervisor
|
||||||
def init({cfg, state}) do
|
def init({cfg, state}) do
|
||||||
|
|
||||||
base_children = [
|
base_children = [
|
||||||
{Omnibot.Plugin.Supervisor.CfgState, cfg: cfg, state: state, name: __MODULE__.CfgState},
|
{Omnibot.Plugin.CfgState, cfg: cfg, state: state, name: __MODULE__.CfgState},
|
||||||
]
|
]
|
||||||
children =
|
children =
|
||||||
(if unquote(opts[:include_base]), do: base_children, else: []) ++ children(cfg, state)
|
(if unquote(opts[:include_base]), do: base_children, else: []) ++ children(cfg, state)
|
||||||
Supervisor.init(children, unquote(opts[:opts]))
|
Supervisor.init(children, unquote(opts[:opts]))
|
||||||
end
|
end
|
||||||
|
|
||||||
@behaviour Omnibot.Plugin.Supervisor
|
def handle_cast({:handle_msg, irc, msg}, state) do
|
||||||
|
on_msg(irc, msg)
|
||||||
|
{:noreply, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
@behaviour Omnibot.Plugin
|
||||||
|
defoverridable Omnibot.Plugin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Reference in New Issue
Block a user