Skip to content

Instantly share code, notes, and snippets.

@bvjebin
Last active August 31, 2018 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvjebin/1a192ec0ad661de0ec0ca3584ab97129 to your computer and use it in GitHub Desktop.
Save bvjebin/1a192ec0ad661de0ec0ca3584ab97129 to your computer and use it in GitHub Desktop.
handle cast code for medium post
@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