Last active
March 14, 2018 18:15
-
-
Save desmondmonster/e55f34ff8c29a50b9f7822700ed11d30 to your computer and use it in GitHub Desktop.
Quick GenServer for asynchronously sending email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PopularApp.EmailSender do | |
use GenServer | |
def start_link do | |
GenServer.start_link(__MODULE__, :ok, name: __MODULE__) | |
end | |
def init(:ok) do | |
{:ok, %{last_sent: nil, last_data: nil}} | |
end | |
# Public API for callers - convenience wrapper around `cast` | |
def send_email(data) do | |
GenServer.cast(__MODULE__, {:send_email, data}) | |
end | |
# Server functions | |
def handle_cast({:send_email, data}, state) do | |
data | |
|> send_email() | |
{:noreply, %{last_sent: calendar.local_time(), last_data: data}} | |
end | |
defp send_email(data) do | |
# extract to, from, etc from `data` | |
# send email through your provider | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment