handling external apis with elixir - alex rozumii

13
HANDLING EXTERNAL APIS WITH ELIXIR ALEX ROZUMII

Upload: elixir-club

Post on 17-Mar-2018

37 views

Category:

Technology


0 download

TRANSCRIPT

HANDLING EXTERNAL APIS WITH ELIXIR

ALEX ROZUMII

ТЕКСТ

WHY?

▸ Because:

▸ External API fail (50x)

▸ Network fails

▸ Have rate limits

TelegramInterface.pull_messages(Keyword.get(state, :offset, 0)) Enum.each(items, fn(update) -> case update do %Nadia.Model.Update{message: %Nadia.Model.Message{date: the_date}} -> #IT WORKS _ -> Rollbax.report() end end end

case Dota.live_league_games do {:ok, curr_state} -> # REAL LOGIC

{:noreply, tick(prev_state, curr_state)}

{:error, %Nadia.Model.Error{reason: :connect_timeout}} -> Process.send_after(self, :timer_event, @wait_ms_before_next_polling_error) {:noreply, tick(prev_state, prev_state)}

{:error, {:error, %HTTPoison.Error{id: nil, reason: _reason}}} -> Process.send_after(self, :timer_event, @wait_ms_before_next_empty_response) {:noreply, tick(prev_state, prev_state)}

{:error, {:ok, %HTTPoison.Response{status_code: 500}}} -> Process.send_after(self, :timer_event, @wait_ms_before_next_500_error) {:noreply, tick(prev_state, prev_state)} end

ТЕКСТ

HOW?

▸ OTP

▸ Agents

▸ Behaviours

defmodule Telegram.MessageQueue do def start_link do Agent.start_link(fn -> [] end, name: __MODULE__) end

# @doc Flushes all existing records def flush! do Agent.cast(__MODULE__, fn(_) -> [] end) end

# @doc Pushes message for given receiver to queue def push_message(tlg_id, message, opts) do item = {tlg_id, message, opts} # Agent.get(GosuObs.Telegram.MessageQueue, fn set -> set end) Agent.update(__MODULE__, &( [item | &1 ])) end

# @doc Gets a top message, grouped if possible def pull_top_message do Agent.get_and_update(__MODULE__, fn queue -> case List.last(queue) do nil -> {nil, []} msg -> end end end end

def handle_info(:timer_event, state) do case Telegram.MessageQueue.pull_top_message do {tlg_id, message, opts} -> case send_message(tlg_id, message, opts) do :ok -> Process.send_after(self, :timer_event, @wait_ms_before_next_message_send) nil :error -> Process.send_after(self, :timer_event, @wait_ms_before_next_message_send) nil :wait -> Process.send_after(self, :timer_event, @wait_ms_before_next_message_send_limit) nil :retry -> Process.send_after(self, :timer_event, @wait_ms_before_next_message) GosuObs.Telegram.MessageQueue.push_message(tlg_id, message, opts) end {:noreply, state} nil -> Process.send_after(self, :timer_event, @wait_ms_before_next_message_pull) {:noreply, state} end end

defp send_message(t, message, opts) do case Nadia.send_message(t, message, opts) do {:ok, _result} -> :ok {:error, %Nadia.Model.Error{reason: "Please wait»}} -> :wait result -> Rollbax.report() :retry end end

THANKS!