-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added healthcheck controller * Added tests * Created documentation for this endpoint * Updated Sparrow dependency
- Loading branch information
Showing
7 changed files
with
248 additions
and
3 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
lib/mongoose_push_web/controllers/healthcheck_controller.ex
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule MongoosePushWeb.HealthcheckController do | ||
use MongoosePushWeb, :controller | ||
|
||
def send(conn = %Plug.Conn{}, %{}) do | ||
stats = :wpool.stats() | ||
|
||
payload = | ||
stats | ||
|> Enum.map(&extract_connection_info_from_pool/1) | ||
|
||
status = get_status(payload) | ||
|
||
conn | ||
|> put_status(status) | ||
|> json(payload) | ||
end | ||
|
||
defp extract_connection_info_from_pool(pool) do | ||
pool_pid = pool[:supervisor] | ||
children = Supervisor.which_children(pool_pid) | ||
{_, sup_pid, _, _} = List.keyfind(children, [:wpool_process_sup], 3) | ||
workers = Supervisor.which_children(sup_pid) | ||
|
||
connections = | ||
workers | ||
|> Enum.map(fn worker_info -> | ||
{_, worker_pid, _, _} = worker_info | ||
|
||
if Sparrow.H2Worker.is_alive_connection(worker_pid) do | ||
:connected | ||
else | ||
:disconnected | ||
end | ||
end) | ||
|
||
%{ | ||
pool: pool[:pool], | ||
connection_status: connections | ||
} | ||
end | ||
|
||
defp get_status(connections) do | ||
is_everything_disconnected = | ||
Enum.all?(connections, fn pool_info -> | ||
Enum.all?(pool_info[:connection_status], fn | ||
status -> status == :disconnected | ||
end) | ||
end) | ||
|
||
case is_everything_disconnected do | ||
true -> | ||
503 | ||
|
||
false -> | ||
200 | ||
end | ||
end | ||
end |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
defmodule MongoosePushWeb.HealthcheckTest do | ||
alias MongoosePush.Support.API | ||
use ExUnit.Case, async: false | ||
import Mox | ||
|
||
setup :verify_on_exit! | ||
|
||
setup do | ||
TestHelper.reload_app() | ||
end | ||
|
||
test "Successful connection to services" do | ||
{200, _, body} = API.get("/healthcheck") | ||
pools = Jason.decode!(body) | ||
|
||
fcm_pools = | ||
:mongoose_push | ||
|> Application.get_env(:fcm) | ||
|> Enum.map(fn {name, info} -> | ||
{name, Keyword.get(info, :pool_size)} | ||
end) | ||
|
||
apns_pools = | ||
:mongoose_push | ||
|> Application.get_env(:apns) | ||
|> Enum.map(fn {name, info} -> | ||
{name, Keyword.get(info, :pool_size)} | ||
end) | ||
|
||
for {pool_name, worker_count} <- fcm_pools ++ apns_pools do | ||
pool_info = %{ | ||
"pool" => Atom.to_string(pool_name), | ||
"connection_status" => List.duplicate("connected", worker_count) | ||
} | ||
|
||
assert true == Enum.member?(pools, pool_info) | ||
end | ||
end | ||
|
||
describe "Unsuccessful FCM connection" do | ||
setup do | ||
old_config = Application.fetch_env!(:mongoose_push, :fcm) | ||
|
||
new_config = | ||
old_config | ||
|> Enum.map(fn {pool_name, pool_info} -> | ||
# We simulate broken FCM connection by changing the port | ||
{pool_name, Keyword.update(pool_info, :port, 4444, &(&1 + 1))} | ||
end) | ||
|
||
Application.stop(:mongoose_push) | ||
Application.load(:mongoose_push) | ||
Application.put_env(:mongoose_push, :fcm, new_config) | ||
Application.start(:mongoose_push) | ||
end | ||
|
||
test "is reflected in heathcheck endpoint" do | ||
{200, _, body} = API.get("/healthcheck") | ||
pools = Jason.decode!(body) | ||
|
||
fcm_pools = | ||
:mongoose_push | ||
|> Application.get_env(:fcm) | ||
|> Enum.map(fn {name, info} -> | ||
{name, Keyword.get(info, :pool_size)} | ||
end) | ||
|
||
apns_pools = | ||
:mongoose_push | ||
|> Application.get_env(:apns) | ||
|> Enum.map(fn {name, info} -> | ||
{name, Keyword.get(info, :pool_size)} | ||
end) | ||
|
||
for {pool_name, worker_count} <- apns_pools do | ||
pool_info = %{ | ||
"pool" => Atom.to_string(pool_name), | ||
"connection_status" => List.duplicate("connected", worker_count) | ||
} | ||
|
||
assert true == Enum.member?(pools, pool_info) | ||
end | ||
|
||
for {pool_name, worker_count} <- fcm_pools do | ||
pool_info = %{ | ||
"pool" => Atom.to_string(pool_name), | ||
"connection_status" => List.duplicate("disconnected", worker_count) | ||
} | ||
|
||
assert true == Enum.member?(pools, pool_info) | ||
end | ||
end | ||
end | ||
|
||
describe "Unsuccessful APNS and FCM connection" do | ||
setup do | ||
old_fcm_config = Application.fetch_env!(:mongoose_push, :fcm) | ||
|
||
new_fcm_config = | ||
old_fcm_config | ||
|> Enum.map(fn {pool_name, pool_info} -> | ||
# We simulate broken FCM connection by changing the port | ||
{pool_name, Keyword.update(pool_info, :port, 4444, &(&1 + 1))} | ||
end) | ||
|
||
old_apns_config = Application.fetch_env!(:mongoose_push, :apns) | ||
|
||
new_apns_config = | ||
old_apns_config | ||
|> Enum.map(fn {pool_name, pool_info} -> | ||
# We simulate broken APNS connection by changing the port | ||
{pool_name, Keyword.update(pool_info, :use_2197, false, &(!&1))} | ||
end) | ||
|
||
Application.stop(:mongoose_push) | ||
Application.load(:mongoose_push) | ||
Application.put_env(:mongoose_push, :fcm, new_fcm_config) | ||
Application.put_env(:mongoose_push, :apns, new_apns_config) | ||
Application.start(:mongoose_push) | ||
end | ||
|
||
test "is reflected in heathcheck endpoint" do | ||
{503, _, body} = API.get("/healthcheck") | ||
pools = Jason.decode!(body) | ||
|
||
fcm_pools = | ||
:mongoose_push | ||
|> Application.get_env(:fcm) | ||
|> Enum.map(fn {name, info} -> | ||
{name, Keyword.get(info, :pool_size)} | ||
end) | ||
|
||
apns_pools = | ||
:mongoose_push | ||
|> Application.get_env(:apns) | ||
|> Enum.map(fn {name, info} -> | ||
{name, Keyword.get(info, :pool_size)} | ||
end) | ||
|
||
for {pool_name, worker_count} <- apns_pools do | ||
pool_info = %{ | ||
"pool" => Atom.to_string(pool_name), | ||
"connection_status" => List.duplicate("disconnected", worker_count) | ||
} | ||
|
||
assert true == Enum.member?(pools, pool_info) | ||
end | ||
|
||
for {pool_name, worker_count} <- fcm_pools do | ||
pool_info = %{ | ||
"pool" => Atom.to_string(pool_name), | ||
"connection_status" => List.duplicate("disconnected", worker_count) | ||
} | ||
|
||
assert true == Enum.member?(pools, pool_info) | ||
end | ||
end | ||
end | ||
end |