Created
March 20, 2019 15:35
-
-
Save cybrox/77036835210a4bd96264ab10931e1c2d to your computer and use it in GitHub Desktop.
phx-ete4.ex
This file contains hidden or 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
post "/db/checkout" do | |
# If the agent is registered and alive, a db connection is checked out already | |
# Otherwise, we spawn the agent and let it(!) check out the db connection | |
owner_process = Process.whereis(:db_owner_agent) | |
if owner_process && Process.alive?(owner_process) do | |
send_resp(conn, 200, "connection has already been checked out") | |
else | |
{:ok, _pid} = Agent.start_link(&checkout_shared_db_conn/0, name: :db_owner_agent) | |
send_resp(conn, 200, "checked out database connection") | |
end | |
end | |
post "/db/checkin" do | |
# If the agent is registered and alive, we check the connection back in | |
# Otherwise, no connection has been checked out, we ignore this | |
owner_process = Process.whereis(:db_owner_agent) | |
if owner_process && Process.alive?(owner_process) do | |
Agent.get(owner_process, &checkin_shared_db_conn/1) | |
Agent.stop(owner_process) | |
send_resp(conn, 200, "checked in database connection") | |
else | |
send_resp(conn, 200, "connection has already been checked back in") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment