Last active
August 31, 2018 11:33
-
-
Save bvjebin/1a192ec0ad661de0ec0ca3584ab97129 to your computer and use it in GitHub Desktop.
handle cast code for medium post
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
@agent :export_email_state | |
def start_link() do | |
GenServer.start_link(__MODULE__, %{}, [name: __MODULE__]) | |
Agent.start_link(fn -> Map.new end, name: @agent) | |
end | |
def handle_cast({:export_email, {data, email}}, state) do | |
key = email | |
job_status = Agent.get(@agent, fn agent_state -> Map.get(agent_state, key) end) | |
try do | |
case job_status do | |
nil -> | |
Agent.update(@agent, fn agent_state -> Map.put(agent_state, key, {:started, System.system_time(:millisecond)}) end) | |
export_download_zip_email(data, email) | |
Agent.update(@agent, fn agent_state -> Map.delete(agent_state, key) end) | |
{:noreply, state} | |
{:started, _} -> | |
Logger.warn "Already in progress" | |
{:noreply, state} | |
end | |
catch | |
:error, error -> | |
Logger.error "Error occured #{inspect error}" | |
{:noreply, state} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment