Skip to content

Commit

Permalink
Merge pull request #8 from zvkemp/faster_startup
Browse files Browse the repository at this point in the history
async API init calls
  • Loading branch information
zvkemp authored Oct 12, 2017
2 parents 8c5160e + 4e1ebc2 commit cbbe9f4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
22 changes: 13 additions & 9 deletions lib/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ defmodule Slack.Supervisor do

def init(_arg) do
registry_spec = supervisor(Registry, [[keys: :unique, name: Slack.BotRegistry]])
bot_specs = bot_configs() |> Enum.map(fn (%{name: name, workspace: ws} = config) ->
supervisor(Slack.Bot.Supervisor, [config], id: {{ws, name}, Slack.Bot.Supervisor})

bot_specs = bot_configs()
|> Task.async_stream(&bot_config_to_spec/1)
|> Enum.map(fn ({:ok, {%{name: bot} = config, data}}) ->
supervisor(Slack.Bot.Supervisor, [config, data], id: {bot, Slack.Bot.Supervisor})
end)

children = [registry_spec | bot_specs]
Expand All @@ -38,14 +41,9 @@ defmodule Slack.Supervisor do
Application.get_env(:slack, :use_console, false)
end

# def start_bot(name) when is_binary(name) or is_atom(name) do
# config = bot_configs() |> Enum.find(fn (%{name: n}) -> n == name end)
# start_bot(config)
# end

# TODO: use normal supervisor handler
def start_bot(%{} = config) do
Slack.Bot.Supervisor.start_link(config)
{config, data} = bot_config_to_spec(config)
Slack.Bot.Supervisor.start_link(config, data)
end

def stop_bot(name) do
Expand All @@ -57,4 +55,10 @@ defmodule Slack.Supervisor do
|> Application.get_env(:bots, [])
|> Enum.map(&struct(Slack.Bot.Config, &1))
end

defp bot_config_to_spec(conf) do
bot_name = {conf.workspace, conf.name}
api_data = Slack.Bot.Supervisor.init_api_calls(conf.api_client, conf.token, bot_name)
{Map.put(conf, :name, bot_name), api_data}
end
end
11 changes: 5 additions & 6 deletions lib/slack/bot/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ defmodule Slack.Bot.Supervisor do
import Slack.BotRegistry
alias Slack.Bot

def start_link(%{name: name, workspace: ws} = config) do
Supervisor.start_link(__MODULE__, config, name: registry_key({ws, name}, __MODULE__))
def start_link(%{name: bot} = config, data) do
Supervisor.start_link(__MODULE__, {config, data}, name: registry_key(bot, __MODULE__))
end

# ---

def init(%Slack.Bot.Config{} = c) do
name = {c.workspace, c.name}
{uid, ws_url, channels} = init_api_calls(c.api_client, c.token, name)
def init({%Slack.Bot.Config{} = c, {uid, ws_url, channels}}) do
name = c.name
Agent.start_link(fn -> channels end, name: registry_key(name, :channels))

children = [
Expand All @@ -24,7 +23,7 @@ defmodule Slack.Bot.Supervisor do
supervise(children, strategy: :rest_for_one)
end

defp init_api_calls(client, token, name) do
def init_api_calls(client, token, name) do
%{
"self" => %{"id" => uid},
"url" => ws_url
Expand Down
3 changes: 2 additions & 1 deletion lib/slack/console/pub_sub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ defmodule Slack.Console.PubSub do
@impl true
@spec handle_cast({:broadcast, String.t, String.t, map, pid}, {map, map}) :: {:noreply, {map, map}}
def handle_cast({:broadcast, workspace, channel, unencoded_message, from_socket} = m, {channels, _} = state) do
ts = System.os_time(:microseconds) / 10_000_00
channel_key = {workspace, channel}
uid = channels[channel_key][from_socket] || "console user"
message = unencoded_message |> Map.put("user", uid) |> Poison.encode!
message = unencoded_message |> Map.merge(%{"user" => uid, "ts" => "#{ts}"}) |> Poison.encode!
text = unencoded_message["text"]

Slack.Console.print(workspace, channel, uid, text)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Slack.Mixfile do

def project do
[app: :slack,
version: "0.7.0",
version: "0.7.1",
elixir: "~> 1.5",
elixirc_paths: elixirc_paths(Mix.env),
build_embedded: Mix.env == :prod,
Expand Down

0 comments on commit cbbe9f4

Please sign in to comment.