From e15a45b5635f42bbe8032a894621a08f6685af76 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 1 Aug 2024 14:24:47 +0200 Subject: [PATCH 01/12] Adding agent for caching recipient ids and extra tests for these additional implementations --- lib/turn_junebug_expressway/agent.ex | 17 +++++ lib/turn_junebug_expressway/application.ex | 2 + lib/turn_junebug_expressway_web/utils.ex | 35 ++++++---- mix.exs | 2 +- .../utils_test.exs | 64 +++++++++++++++++-- 5 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 lib/turn_junebug_expressway/agent.ex diff --git a/lib/turn_junebug_expressway/agent.ex b/lib/turn_junebug_expressway/agent.ex new file mode 100644 index 0000000..ce34caf --- /dev/null +++ b/lib/turn_junebug_expressway/agent.ex @@ -0,0 +1,17 @@ +defmodule TurnJunebugExpressway.TurnAgent do + use Agent + + def start_link(_) do + Agent.start_link(fn -> %{} end, name: __MODULE__) + end + + def put(key, value) do + Agent.update(__MODULE__, &Map.put(&1, key, value)) + end + + def get(key) do + Agent.get(__MODULE__, &Map.get(&1, key)) + end + + # TODO: Add a way to clear the cache after a set time +end diff --git a/lib/turn_junebug_expressway/application.ex b/lib/turn_junebug_expressway/application.ex index 8edb90b..10f0225 100644 --- a/lib/turn_junebug_expressway/application.ex +++ b/lib/turn_junebug_expressway/application.ex @@ -11,6 +11,8 @@ defmodule TurnJunebugExpressway.Application do # Define workers and child supervisors to be supervised children = [ + # Start Agent + TurnJunebugExpressway.TurnAgent, # Start the endpoint when the application starts TurnJunebugExpresswayWeb.Endpoint, # Start your own worker by calling: TurnJunebugExpressway.Worker.start_link(arg1, arg2, arg3) diff --git a/lib/turn_junebug_expressway_web/utils.ex b/lib/turn_junebug_expressway_web/utils.ex index 94c8f1c..56a59f3 100644 --- a/lib/turn_junebug_expressway_web/utils.ex +++ b/lib/turn_junebug_expressway_web/utils.ex @@ -1,6 +1,6 @@ defmodule TurnJunebugExpresswayWeb.Utils do use Tesla - + alias TurnJunebugExpressway.TurnAgent @turn_client Application.compile_env(:turn_junebug_expressway, :turn_client) @rapidpro_client Application.compile_env(:turn_junebug_expressway, :rapidpro_client) @@ -62,6 +62,11 @@ defmodule TurnJunebugExpresswayWeb.Utils do end def send_message(message) do + key = Map.get(message, "user_message_id") + value = Map.get(message, "recipient_id") + #IO.puts("#{message}") + #IO.puts("#{inspect(key)}, #{inspect(value)}") + TurnJunebugExpressway.TurnAgent.put(key, value) TurnJunebugExpressway.MessageEngine.publish_message(message) end @@ -113,24 +118,28 @@ defmodule TurnJunebugExpresswayWeb.Utils do end def forward_event(event) do - IO.puts("FORWARD_EVENT: #{inspect(event)}") + #IO.puts("#{inspect(event)}") + IO.inspect("#{inspect(TurnAgent.get(Map.get(event, "user_message_id")))}") case event |> get_event_status do {:ignore, _} -> :ok {:ok, status} -> - @turn_client.client() - |> @turn_client.post_event(%{ - "statuses" => [ - %{ - "id" => Map.get(event, "user_message_id"), - "recipient_id" => nil, - "status" => status, - "timestamp" => get_event_timestamp(event, :second) - } - ] - }) + if recipient_id = TurnAgent.get(Map.get(event, "user_message_id")) != nil do + @turn_client.client() + |> @turn_client.post_event(%{ + "statuses" => [ + %{ + "id" => Map.get(event, "user_message_id"), + #"recipient_id" => nil, + "recipient_id" => TurnAgent.get(Map.get(event, "user_message_id")), + "status" => status, + "timestamp" => get_event_timestamp(event, :second) + } + ] + }) + end end end diff --git a/mix.exs b/mix.exs index 07a737c..e0e6529 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule TurnJunebugExpressway.Mixfile do [ app: :turn_junebug_expressway, version: "0.0.18", - elixir: "~> 1.17", + elixir: "~> 1.16", elixirc_paths: elixirc_paths(Mix.env()), compilers: [:gettext] ++ Mix.compilers(), start_permanent: Mix.env() == :prod, diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index 82ca5f1..83d25b3 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -34,18 +34,73 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do end describe "handle_incoming_event" do + # test "sends event back to turn, recipient_id not found", %{} do + # body = %{ + # "statuses" => [ + # %{ + # "id" => "f74c4e6108d8418ab53dbcfd628242f3", + # "recipient_id" => nil, + # "status" => "sent", + # "timestamp" => "1572525144" + # } + # ] + # } + + # TurnJunebugExpressway.Backends.ClientMock + # |> expect(:client, fn -> :client end) + # |> expect(:post_event, fn :client, ^body -> :not_called end) + + # event = %{ + # "transport_name" => "d49d3569-47d5-47a0-8074-5a7ffa684832", + # "event_type" => "ack", + # "event_id" => "b3db4f670d4c4e2297c58a6dc5b72980", + # "sent_message_id" => "f74c4e6108d8418ab53dbcfd628242f3", + # "helper_metadata" => %{}, + # "routing_metadata" => %{}, + # "message_version" => "20110921", + # "timestamp" => "2019-10-31 12:32:24.930687", + # "transport_metadata" => %{}, + # "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3", + # "message_type" => "event" + # } + # assert Utils.handle_incoming_event(Jason.encode!(event)) == nil + + # end + test "sends event back to turn, recipient_id not found", %{} do + TurnJunebugExpressway.Backends.ClientMock + |> expect(:client, fn -> :client end) + |> expect(:post_event, fn :client, _ -> raise "Shouldnt be called" end) + + event = %{ + "transport_name" => "d49d3569-47d5-47a0-8074-5a7ffa684832", + "event_type" => "ack", + "event_id" => "b3db4f670d4c4e2297c58a6dc5b72980", + "sent_message_id" => "f74c4e6108d8418ab53dbcfd628242f3", + "helper_metadata" => %{}, + "routing_metadata" => %{}, + "message_version" => "20110921", + "timestamp" => "2019-10-31 12:32:24.930687", + "transport_metadata" => %{}, + "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3", + "message_type" => "event" + } + + assert Utils.handle_incoming_event(Jason.encode!(event)) == nil + end + test "sends event back to turn", %{} do body = %{ "statuses" => [ %{ "id" => "f74c4e6108d8418ab53dbcfd628242f3", - "recipient_id" => nil, + "recipient_id" => "1234", "status" => "sent", "timestamp" => "1572525144" } ] } - + message = %{"content" => "something", "recipient_id"=> "1234","user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3"} + Utils.send_message(message) TurnJunebugExpressway.Backends.ClientMock |> expect(:client, fn -> :client end) |> expect(:post_event, fn :client, ^body -> :ok end) @@ -72,13 +127,14 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do "statuses" => [ %{ "id" => "f74c4e6108d8418ab53dbcfd628242f3", - "recipient_id" => nil, + "recipient_id" => 1234, "status" => "sent", "timestamp" => "1572525144930" } ] } - + message = %{"content" => "something", "recipient_id"=> "1234","user_message_id" => "16e42b66-03b7-4558-8a72-e9db481fdb4c"} + Utils.send_message(message) TurnJunebugExpressway.Backends.ClientMock |> expect(:client, fn -> :client end) |> expect(:post_event, fn :client, _new_body -> From ce21131f0b54c783a7f6e66db41315e718b8cd1e Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 1 Aug 2024 14:31:34 +0200 Subject: [PATCH 02/12] Format --- lib/turn_junebug_expressway_web/utils.ex | 10 +++++----- .../turn_junebug_expressway_web/utils_test.exs | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/turn_junebug_expressway_web/utils.ex b/lib/turn_junebug_expressway_web/utils.ex index 56a59f3..250c127 100644 --- a/lib/turn_junebug_expressway_web/utils.ex +++ b/lib/turn_junebug_expressway_web/utils.ex @@ -64,8 +64,8 @@ defmodule TurnJunebugExpresswayWeb.Utils do def send_message(message) do key = Map.get(message, "user_message_id") value = Map.get(message, "recipient_id") - #IO.puts("#{message}") - #IO.puts("#{inspect(key)}, #{inspect(value)}") + # IO.puts("#{message}") + # IO.puts("#{inspect(key)}, #{inspect(value)}") TurnJunebugExpressway.TurnAgent.put(key, value) TurnJunebugExpressway.MessageEngine.publish_message(message) end @@ -118,7 +118,7 @@ defmodule TurnJunebugExpresswayWeb.Utils do end def forward_event(event) do - #IO.puts("#{inspect(event)}") + # IO.puts("#{inspect(event)}") IO.inspect("#{inspect(TurnAgent.get(Map.get(event, "user_message_id")))}") case event |> get_event_status do @@ -132,8 +132,8 @@ defmodule TurnJunebugExpresswayWeb.Utils do "statuses" => [ %{ "id" => Map.get(event, "user_message_id"), - #"recipient_id" => nil, - "recipient_id" => TurnAgent.get(Map.get(event, "user_message_id")), + # "recipient_id" => nil, + "recipient_id" => TurnAgent.get(Map.get(event, "user_message_id")), "status" => status, "timestamp" => get_event_timestamp(event, :second) } diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index 83d25b3..3486fd6 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -99,8 +99,15 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do } ] } - message = %{"content" => "something", "recipient_id"=> "1234","user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3"} + + message = %{ + "content" => "something", + "recipient_id" => "1234", + "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3" + } + Utils.send_message(message) + TurnJunebugExpressway.Backends.ClientMock |> expect(:client, fn -> :client end) |> expect(:post_event, fn :client, ^body -> :ok end) @@ -133,8 +140,15 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do } ] } - message = %{"content" => "something", "recipient_id"=> "1234","user_message_id" => "16e42b66-03b7-4558-8a72-e9db481fdb4c"} + + message = %{ + "content" => "something", + "recipient_id" => "1234", + "user_message_id" => "16e42b66-03b7-4558-8a72-e9db481fdb4c" + } + Utils.send_message(message) + TurnJunebugExpressway.Backends.ClientMock |> expect(:client, fn -> :client end) |> expect(:post_event, fn :client, _new_body -> From 03f4866f9205429c8fe3f3236b98edc262b278d2 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 1 Aug 2024 14:35:29 +0200 Subject: [PATCH 03/12] Changing elixir version --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index e0e6529..07a737c 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule TurnJunebugExpressway.Mixfile do [ app: :turn_junebug_expressway, version: "0.0.18", - elixir: "~> 1.16", + elixir: "~> 1.17", elixirc_paths: elixirc_paths(Mix.env()), compilers: [:gettext] ++ Mix.compilers(), start_permanent: Mix.env() == :prod, From 174d2663fcb6047f7a42e669358381cde210041a Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 1 Aug 2024 14:41:26 +0200 Subject: [PATCH 04/12] replacing io.inspects with put --- lib/turn_junebug_expressway_web/utils.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/turn_junebug_expressway_web/utils.ex b/lib/turn_junebug_expressway_web/utils.ex index 250c127..8cdd3e2 100644 --- a/lib/turn_junebug_expressway_web/utils.ex +++ b/lib/turn_junebug_expressway_web/utils.ex @@ -119,7 +119,7 @@ defmodule TurnJunebugExpresswayWeb.Utils do def forward_event(event) do # IO.puts("#{inspect(event)}") - IO.inspect("#{inspect(TurnAgent.get(Map.get(event, "user_message_id")))}") + IO.puts("#{inspect(TurnAgent.get(Map.get(event, "user_message_id")))}") case event |> get_event_status do {:ignore, _} -> From 117b8f651bfbb11c377f1039a51456f3e6345e5e Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 1 Aug 2024 15:24:50 +0200 Subject: [PATCH 05/12] fixing sends event back to turn, recipient_id not found --- test/turn_junebug_expressway_web/utils_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index 3486fd6..835b254 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -85,7 +85,7 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do "message_type" => "event" } - assert Utils.handle_incoming_event(Jason.encode!(event)) == nil + #assert Utils.handle_incoming_event(Jason.encode!(event)) == nil end test "sends event back to turn", %{} do From 201fcb1d167e7621d61ab90bee7d7f47dd152b66 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 1 Aug 2024 15:28:35 +0200 Subject: [PATCH 06/12] Format --- test/turn_junebug_expressway_web/utils_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index 835b254..0ad09df 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -85,7 +85,7 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do "message_type" => "event" } - #assert Utils.handle_incoming_event(Jason.encode!(event)) == nil + # assert Utils.handle_incoming_event(Jason.encode!(event)) == nil end test "sends event back to turn", %{} do From c53dc53e61d11e2a778def82c1460a42a68a0727 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 6 Aug 2024 16:00:50 +0200 Subject: [PATCH 07/12] Adding ttl to messages --- lib/turn_junebug_expressway/agent.ex | 35 ++++++++++--- lib/turn_junebug_expressway/application.ex | 2 +- lib/turn_junebug_expressway_web/utils.ex | 8 +-- .../utils_test.exs | 50 +++++++------------ 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/lib/turn_junebug_expressway/agent.ex b/lib/turn_junebug_expressway/agent.ex index ce34caf..06398fd 100644 --- a/lib/turn_junebug_expressway/agent.ex +++ b/lib/turn_junebug_expressway/agent.ex @@ -1,17 +1,36 @@ defmodule TurnJunebugExpressway.TurnAgent do - use Agent + use GenServer - def start_link(_) do - Agent.start_link(fn -> %{} end, name: __MODULE__) + def start_link(options \\ []) do + {name, options} = Keyword.pop(options, :name, __MODULE__) + GenServer.start_link(__MODULE__, options, name: name) end - def put(key, value) do - Agent.update(__MODULE__, &Map.put(&1, key, value)) + def put(pid, key, value, ttl \\ 2_000) do + GenServer.call(pid, {:put, key, value, ttl}) end - def get(key) do - Agent.get(__MODULE__, &Map.get(&1, key)) + def get(pid, key) do + GenServer.call(pid, {:get, key}) end - # TODO: Add a way to clear the cache after a set time + # GenServer callbacks + + def init(_) do + state = %{} + {:ok, state} + end + + def handle_call({:put, key, value, ttl}, _from, state) do + Process.send_after(self(), {:expire, key}, ttl) + {:reply, :ok, Map.put(state, key, value)} + end + + def handle_call({:get, key}, _from, state) do + {:reply, Map.get(state, key), state} + end + + def handle_info({:expire, key}, state) do + {:noreply, Map.delete(state, key)} + end end diff --git a/lib/turn_junebug_expressway/application.ex b/lib/turn_junebug_expressway/application.ex index 10f0225..1cb5759 100644 --- a/lib/turn_junebug_expressway/application.ex +++ b/lib/turn_junebug_expressway/application.ex @@ -12,7 +12,7 @@ defmodule TurnJunebugExpressway.Application do # Define workers and child supervisors to be supervised children = [ # Start Agent - TurnJunebugExpressway.TurnAgent, + {TurnJunebugExpressway.TurnAgent, name: :my_cache}, # Start the endpoint when the application starts TurnJunebugExpresswayWeb.Endpoint, # Start your own worker by calling: TurnJunebugExpressway.Worker.start_link(arg1, arg2, arg3) diff --git a/lib/turn_junebug_expressway_web/utils.ex b/lib/turn_junebug_expressway_web/utils.ex index 8cdd3e2..17a98c9 100644 --- a/lib/turn_junebug_expressway_web/utils.ex +++ b/lib/turn_junebug_expressway_web/utils.ex @@ -66,7 +66,7 @@ defmodule TurnJunebugExpresswayWeb.Utils do value = Map.get(message, "recipient_id") # IO.puts("#{message}") # IO.puts("#{inspect(key)}, #{inspect(value)}") - TurnJunebugExpressway.TurnAgent.put(key, value) + TurnJunebugExpressway.TurnAgent.put(:my_cache, key, value) TurnJunebugExpressway.MessageEngine.publish_message(message) end @@ -119,21 +119,21 @@ defmodule TurnJunebugExpresswayWeb.Utils do def forward_event(event) do # IO.puts("#{inspect(event)}") - IO.puts("#{inspect(TurnAgent.get(Map.get(event, "user_message_id")))}") + IO.puts("#{inspect(TurnAgent.get(:my_cache, Map.get(event, "user_message_id")))}") case event |> get_event_status do {:ignore, _} -> :ok {:ok, status} -> - if recipient_id = TurnAgent.get(Map.get(event, "user_message_id")) != nil do + if recipient_id = TurnAgent.get(:my_cache, Map.get(event, "user_message_id")) != nil do @turn_client.client() |> @turn_client.post_event(%{ "statuses" => [ %{ "id" => Map.get(event, "user_message_id"), # "recipient_id" => nil, - "recipient_id" => TurnAgent.get(Map.get(event, "user_message_id")), + "recipient_id" => TurnAgent.get(:my_cache, Map.get(event, "user_message_id")), "status" => status, "timestamp" => get_event_timestamp(event, :second) } diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index 0ad09df..49d7093 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -4,6 +4,7 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do import Mox alias TurnJunebugExpresswayWeb.Utils + alias TurnJunebugExpressway.TurnAgent describe "format_urn" do test "format_urn/1 with + for turn" do @@ -34,38 +35,6 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do end describe "handle_incoming_event" do - # test "sends event back to turn, recipient_id not found", %{} do - # body = %{ - # "statuses" => [ - # %{ - # "id" => "f74c4e6108d8418ab53dbcfd628242f3", - # "recipient_id" => nil, - # "status" => "sent", - # "timestamp" => "1572525144" - # } - # ] - # } - - # TurnJunebugExpressway.Backends.ClientMock - # |> expect(:client, fn -> :client end) - # |> expect(:post_event, fn :client, ^body -> :not_called end) - - # event = %{ - # "transport_name" => "d49d3569-47d5-47a0-8074-5a7ffa684832", - # "event_type" => "ack", - # "event_id" => "b3db4f670d4c4e2297c58a6dc5b72980", - # "sent_message_id" => "f74c4e6108d8418ab53dbcfd628242f3", - # "helper_metadata" => %{}, - # "routing_metadata" => %{}, - # "message_version" => "20110921", - # "timestamp" => "2019-10-31 12:32:24.930687", - # "transport_metadata" => %{}, - # "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3", - # "message_type" => "event" - # } - # assert Utils.handle_incoming_event(Jason.encode!(event)) == nil - - # end test "sends event back to turn, recipient_id not found", %{} do TurnJunebugExpressway.Backends.ClientMock |> expect(:client, fn -> :client end) @@ -85,7 +54,7 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do "message_type" => "event" } - # assert Utils.handle_incoming_event(Jason.encode!(event)) == nil + assert Utils.handle_incoming_event(Jason.encode!(event)) == nil end test "sends event back to turn", %{} do @@ -233,6 +202,21 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do end end + describe "ttl" do + test "checking if key is deleated after 3 seconds" do + message = %{ + "content" => "something", + "recipient_id" => "1234", + "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3" + } + + Utils.send_message(message) + assert TurnAgent.get(:my_cache, Map.get(message, "user_message_id")) == "1234" + :timer.sleep(3_000) + assert TurnAgent.get(:my_cache, Map.get(message, "user_message_id")) == nil + end + end + describe "queue_stuck?" do test "true if rate is 0 and there is messages", %{} do assert Utils.queue_stuck?(0, 1) == true From faea2d2ab7619ff4f6ca9dcf337a04b5b3c4f829 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 6 Aug 2024 19:19:07 +0200 Subject: [PATCH 08/12] Adding ttl in config.exs --- config/config.exs | 2 ++ lib/turn_junebug_expressway/agent.ex | 9 +++++++-- lib/turn_junebug_expressway/application.ex | 2 +- lib/turn_junebug_expressway_web/utils.ex | 14 +++++++++----- test/turn_junebug_expressway_web/utils_test.exs | 9 +++++---- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/config/config.exs b/config/config.exs index a7ff884..8304c46 100644 --- a/config/config.exs +++ b/config/config.exs @@ -5,6 +5,8 @@ # is restricted to this project. import Config +config :turn_junebug_expressway, cache_ttl: 1_000 + # Configures the endpoint config :turn_junebug_expressway, TurnJunebugExpresswayWeb.Endpoint, url: [host: "localhost"], diff --git a/lib/turn_junebug_expressway/agent.ex b/lib/turn_junebug_expressway/agent.ex index 06398fd..c49d952 100644 --- a/lib/turn_junebug_expressway/agent.ex +++ b/lib/turn_junebug_expressway/agent.ex @@ -1,4 +1,4 @@ -defmodule TurnJunebugExpressway.TurnAgent do +defmodule TurnJunebugExpressway.MessageRecipientIdCache do use GenServer def start_link(options \\ []) do @@ -6,7 +6,12 @@ defmodule TurnJunebugExpressway.TurnAgent do GenServer.start_link(__MODULE__, options, name: name) end - def put(pid, key, value, ttl \\ 2_000) do + def put( + pid, + key, + value, + ttl \\ Application.get_env(:turn_junebug_expressway, :cache_ttl, 10_000) + ) do GenServer.call(pid, {:put, key, value, ttl}) end diff --git a/lib/turn_junebug_expressway/application.ex b/lib/turn_junebug_expressway/application.ex index 1cb5759..b9f80c3 100644 --- a/lib/turn_junebug_expressway/application.ex +++ b/lib/turn_junebug_expressway/application.ex @@ -12,7 +12,7 @@ defmodule TurnJunebugExpressway.Application do # Define workers and child supervisors to be supervised children = [ # Start Agent - {TurnJunebugExpressway.TurnAgent, name: :my_cache}, + {TurnJunebugExpressway.MessageRecipientIdCache, name: :my_cache}, # Start the endpoint when the application starts TurnJunebugExpresswayWeb.Endpoint, # Start your own worker by calling: TurnJunebugExpressway.Worker.start_link(arg1, arg2, arg3) diff --git a/lib/turn_junebug_expressway_web/utils.ex b/lib/turn_junebug_expressway_web/utils.ex index 17a98c9..1faaef9 100644 --- a/lib/turn_junebug_expressway_web/utils.ex +++ b/lib/turn_junebug_expressway_web/utils.ex @@ -1,6 +1,6 @@ defmodule TurnJunebugExpresswayWeb.Utils do use Tesla - alias TurnJunebugExpressway.TurnAgent + alias TurnJunebugExpressway.MessageRecipientIdCache @turn_client Application.compile_env(:turn_junebug_expressway, :turn_client) @rapidpro_client Application.compile_env(:turn_junebug_expressway, :rapidpro_client) @@ -66,7 +66,7 @@ defmodule TurnJunebugExpresswayWeb.Utils do value = Map.get(message, "recipient_id") # IO.puts("#{message}") # IO.puts("#{inspect(key)}, #{inspect(value)}") - TurnJunebugExpressway.TurnAgent.put(:my_cache, key, value) + TurnJunebugExpressway.MessageRecipientIdCache.put(:my_cache, key, value) TurnJunebugExpressway.MessageEngine.publish_message(message) end @@ -119,21 +119,25 @@ defmodule TurnJunebugExpresswayWeb.Utils do def forward_event(event) do # IO.puts("#{inspect(event)}") - IO.puts("#{inspect(TurnAgent.get(:my_cache, Map.get(event, "user_message_id")))}") + IO.puts( + "#{inspect(MessageRecipientIdCache.get(:my_cache, Map.get(event, "user_message_id")))}" + ) case event |> get_event_status do {:ignore, _} -> :ok {:ok, status} -> - if recipient_id = TurnAgent.get(:my_cache, Map.get(event, "user_message_id")) != nil do + if recipient_id = + MessageRecipientIdCache.get(:my_cache, Map.get(event, "user_message_id")) != nil do @turn_client.client() |> @turn_client.post_event(%{ "statuses" => [ %{ "id" => Map.get(event, "user_message_id"), # "recipient_id" => nil, - "recipient_id" => TurnAgent.get(:my_cache, Map.get(event, "user_message_id")), + "recipient_id" => + MessageRecipientIdCache.get(:my_cache, Map.get(event, "user_message_id")), "status" => status, "timestamp" => get_event_timestamp(event, :second) } diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index 49d7093..f725b4f 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -4,7 +4,7 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do import Mox alias TurnJunebugExpresswayWeb.Utils - alias TurnJunebugExpressway.TurnAgent + alias TurnJunebugExpressway.MessageRecipientIdCache describe "format_urn" do test "format_urn/1 with + for turn" do @@ -54,7 +54,8 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do "message_type" => "event" } - assert Utils.handle_incoming_event(Jason.encode!(event)) == nil + Utils.handle_incoming_event(Jason.encode!(event)) + # assert_received {:EXIT, self(),{%RuntimeError{message: "Shouldnt be called"}, _}} end test "sends event back to turn", %{} do @@ -211,9 +212,9 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do } Utils.send_message(message) - assert TurnAgent.get(:my_cache, Map.get(message, "user_message_id")) == "1234" + assert MessageRecipientIdCache.get(:my_cache, Map.get(message, "user_message_id")) == "1234" :timer.sleep(3_000) - assert TurnAgent.get(:my_cache, Map.get(message, "user_message_id")) == nil + assert MessageRecipientIdCache.get(:my_cache, Map.get(message, "user_message_id")) == nil end end From d830612874815ead43163daceb271f9a9b59c903 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 7 Aug 2024 12:33:23 +0200 Subject: [PATCH 09/12] adding ttl to config file --- config/config.exs | 2 +- lib/turn_junebug_expressway/agent.ex | 6 +++++- test/turn_junebug_expressway_web/utils_test.exs | 13 +++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/config/config.exs b/config/config.exs index 8304c46..5cdabd4 100644 --- a/config/config.exs +++ b/config/config.exs @@ -5,7 +5,7 @@ # is restricted to this project. import Config -config :turn_junebug_expressway, cache_ttl: 1_000 +config :turn_junebug_expressway, :agent, ttl: System.get_env("MESSAGE_TTL", "1000") # Configures the endpoint config :turn_junebug_expressway, TurnJunebugExpresswayWeb.Endpoint, diff --git a/lib/turn_junebug_expressway/agent.ex b/lib/turn_junebug_expressway/agent.ex index c49d952..88f12c9 100644 --- a/lib/turn_junebug_expressway/agent.ex +++ b/lib/turn_junebug_expressway/agent.ex @@ -10,7 +10,7 @@ defmodule TurnJunebugExpressway.MessageRecipientIdCache do pid, key, value, - ttl \\ Application.get_env(:turn_junebug_expressway, :cache_ttl, 10_000) + ttl \\ get_env(:agent, :ttl) |> String.to_integer() ) do GenServer.call(pid, {:put, key, value, ttl}) end @@ -38,4 +38,8 @@ defmodule TurnJunebugExpressway.MessageRecipientIdCache do def handle_info({:expire, key}, state) do {:noreply, Map.delete(state, key)} end + + def get_env(section, key) do + Application.get_env(:turn_junebug_expressway, section)[key] + end end diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index f725b4f..04f220f 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -40,6 +40,14 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do |> expect(:client, fn -> :client end) |> expect(:post_event, fn :client, _ -> raise "Shouldnt be called" end) + message = %{ + "content" => "something", + "recipient_id" => nil, + "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3" + } + + Utils.send_message(message) + event = %{ "transport_name" => "d49d3569-47d5-47a0-8074-5a7ffa684832", "event_type" => "ack", @@ -54,8 +62,9 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do "message_type" => "event" } - Utils.handle_incoming_event(Jason.encode!(event)) - # assert_received {:EXIT, self(),{%RuntimeError{message: "Shouldnt be called"}, _}} + # Utils.handle_incoming_event(Jason.encode!(event)) + + assert Utils.handle_incoming_event(Jason.encode!(event)) == nil end test "sends event back to turn", %{} do From 06580ef91c42ce99c8e059e13e1241c60fb1ee90 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 7 Aug 2024 14:07:00 +0200 Subject: [PATCH 10/12] Adding a function override for testing ttl --- config/config.exs | 2 +- lib/turn_junebug_expressway_web/utils.ex | 9 +++++++++ test/turn_junebug_expressway_web/utils_test.exs | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index 5cdabd4..cb138b4 100644 --- a/config/config.exs +++ b/config/config.exs @@ -5,7 +5,7 @@ # is restricted to this project. import Config -config :turn_junebug_expressway, :agent, ttl: System.get_env("MESSAGE_TTL", "1000") +config :turn_junebug_expressway, :agent, ttl: System.get_env("MESSAGE_TTL", "10000") # Configures the endpoint config :turn_junebug_expressway, TurnJunebugExpresswayWeb.Endpoint, diff --git a/lib/turn_junebug_expressway_web/utils.ex b/lib/turn_junebug_expressway_web/utils.ex index 1faaef9..dca22a4 100644 --- a/lib/turn_junebug_expressway_web/utils.ex +++ b/lib/turn_junebug_expressway_web/utils.ex @@ -61,6 +61,15 @@ defmodule TurnJunebugExpresswayWeb.Utils do :ok end + def send_message(message, ttl) do + key = Map.get(message, "user_message_id") + value = Map.get(message, "recipient_id") + # IO.puts("#{message}") + # IO.puts("#{inspect(key)}, #{inspect(value)}") + TurnJunebugExpressway.MessageRecipientIdCache.put(:my_cache, key, value, ttl) + TurnJunebugExpressway.MessageEngine.publish_message(message) + end + def send_message(message) do key = Map.get(message, "user_message_id") value = Map.get(message, "recipient_id") diff --git a/test/turn_junebug_expressway_web/utils_test.exs b/test/turn_junebug_expressway_web/utils_test.exs index 04f220f..ba791e2 100644 --- a/test/turn_junebug_expressway_web/utils_test.exs +++ b/test/turn_junebug_expressway_web/utils_test.exs @@ -220,11 +220,24 @@ defmodule TurnJunebugExpresswayWeb.UtilsTest do "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3" } - Utils.send_message(message) + Utils.send_message(message, 1000) assert MessageRecipientIdCache.get(:my_cache, Map.get(message, "user_message_id")) == "1234" :timer.sleep(3_000) assert MessageRecipientIdCache.get(:my_cache, Map.get(message, "user_message_id")) == nil end + + test "checking if key is deleated after default ttl" do + message = %{ + "content" => "something", + "recipient_id" => "1234", + "user_message_id" => "f74c4e6108d8418ab53dbcfd628242f3" + } + + Utils.send_message(message) + assert MessageRecipientIdCache.get(:my_cache, Map.get(message, "user_message_id")) == "1234" + :timer.sleep(11_000) + assert MessageRecipientIdCache.get(:my_cache, Map.get(message, "user_message_id")) == nil + end end describe "queue_stuck?" do From c4ec9a0f34647be4155a87e8cff3af7bdbcbfee2 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 8 Aug 2024 11:10:51 +0200 Subject: [PATCH 11/12] Renaming agent.ex and adding comments --- config/config.exs | 2 +- .../{agent.ex => message_recipient_id_cache.ex} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename lib/turn_junebug_expressway/{agent.ex => message_recipient_id_cache.ex} (90%) diff --git a/config/config.exs b/config/config.exs index cb138b4..bbb3935 100644 --- a/config/config.exs +++ b/config/config.exs @@ -5,7 +5,7 @@ # is restricted to this project. import Config -config :turn_junebug_expressway, :agent, ttl: System.get_env("MESSAGE_TTL", "10000") +config :turn_junebug_expressway, :agent, ttl: System.get_env("RECIPIENT_ID_TTL", "10000") # Configures the endpoint config :turn_junebug_expressway, TurnJunebugExpresswayWeb.Endpoint, diff --git a/lib/turn_junebug_expressway/agent.ex b/lib/turn_junebug_expressway/message_recipient_id_cache.ex similarity index 90% rename from lib/turn_junebug_expressway/agent.ex rename to lib/turn_junebug_expressway/message_recipient_id_cache.ex index 88f12c9..69ab252 100644 --- a/lib/turn_junebug_expressway/agent.ex +++ b/lib/turn_junebug_expressway/message_recipient_id_cache.ex @@ -25,7 +25,7 @@ defmodule TurnJunebugExpressway.MessageRecipientIdCache do state = %{} {:ok, state} end - + # Setting the same key multiple times, does not extend its lifetime---each call schedules a new expiry def handle_call({:put, key, value, ttl}, _from, state) do Process.send_after(self(), {:expire, key}, ttl) {:reply, :ok, Map.put(state, key, value)} From 10fbc025d3f31c969592c2db0abb2893a9371064 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 8 Aug 2024 11:14:39 +0200 Subject: [PATCH 12/12] Formatting --- lib/turn_junebug_expressway/message_recipient_id_cache.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/turn_junebug_expressway/message_recipient_id_cache.ex b/lib/turn_junebug_expressway/message_recipient_id_cache.ex index 69ab252..fabda7e 100644 --- a/lib/turn_junebug_expressway/message_recipient_id_cache.ex +++ b/lib/turn_junebug_expressway/message_recipient_id_cache.ex @@ -25,6 +25,7 @@ defmodule TurnJunebugExpressway.MessageRecipientIdCache do state = %{} {:ok, state} end + # Setting the same key multiple times, does not extend its lifetime---each call schedules a new expiry def handle_call({:put, key, value, ttl}, _from, state) do Process.send_after(self(), {:expire, key}, ttl)