Skip to content

Commit cc48ab3

Browse files
committed
1.0.3 add more functions for Hemdal.Host and tests
1 parent 2efc85f commit cc48ab3

File tree

5 files changed

+143
-7
lines changed

5 files changed

+143
-7
lines changed

lib/hemdal.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ defmodule Hemdal do
6464
linked to the checks, see `Hemdal.Check` for further information.
6565
6666
The events could be implemented via `GenStage`. See `Hemdal.Event` for
67-
futher information.
67+
further information.
6868
6969
## Notification
7070
@@ -75,7 +75,7 @@ defmodule Hemdal do
7575
- `Hemdal.Notifier.Slack` is sending the event to a Slack webhook.
7676
- `Hemdal.Notifier.Mattermost` is sending the even to a Mattermost webhook.
7777
78-
See `Hemdal.Notifier` for futher information.
78+
See `Hemdal.Notifier` for further information.
7979
"""
8080

8181
@doc """

lib/hemdal/host.ex

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ defmodule Hemdal.Host do
8484
"""
8585
@spec start_link([Hemdal.Config.Host.t()]) :: {:ok, pid()}
8686
def start_link([host]) do
87-
Logger.info("starting #{host.id} - #{host.name}")
8887
{:ok, _pid} = GenServer.start_link(__MODULE__, [host], name: via(host.id))
8988
end
9089

@@ -141,8 +140,14 @@ defmodule Hemdal.Host do
141140
"""
142141
@spec reload_all() :: :ok
143142
def reload_all do
144-
Hemdal.Config.get_all_hosts()
145-
|> Enum.each(&update_host/1)
143+
config_hosts = Hemdal.Config.get_all_hosts()
144+
Enum.each(config_hosts, &update_host/1)
145+
146+
config_ids = Enum.map(config_hosts, & &1.id)
147+
running_ids = get_all()
148+
149+
(running_ids -- config_ids)
150+
|> Enum.each(&stop/1)
146151
end
147152

148153
@doc """
@@ -156,6 +161,29 @@ defmodule Hemdal.Host do
156161
|> Enum.each(&start/1)
157162
end
158163

164+
@doc """
165+
Get all the host IDs that are running at the moment of calling this function.
166+
"""
167+
@spec get_all() :: [host_id()]
168+
def get_all do
169+
@registry_name
170+
|> Registry.select([{{:"$1", :"$2", :_}, [], [{{:"$1", :"$2"}}]}])
171+
|> Enum.filter(fn {_key, pid} -> is_pid(pid) and Process.alive?(pid) end)
172+
|> Enum.map(fn {key, _pid} -> key end)
173+
|> Enum.sort()
174+
end
175+
176+
@doc """
177+
Terminate the processes related to a process ID.
178+
"""
179+
@spec stop(host_id()) :: :ok
180+
def stop(host_id) do
181+
Registry.lookup(@registry_name, host_id)
182+
|> Enum.each(fn {pid, _value} ->
183+
DynamicSupervisor.terminate_child(@sup_name, pid)
184+
end)
185+
end
186+
159187
@doc """
160188
Update the host configuration. If the host isn't running it's starting it
161189
and passing it the configuration provided as the `host` parameter. In a
@@ -187,6 +215,7 @@ defmodule Hemdal.Host do
187215
@impl GenServer
188216
@doc false
189217
def init([host]) do
218+
Logger.info("[#{inspect(self())}] starting #{host.id} - #{host.name}")
190219
{:ok, %__MODULE__{host: host, max_workers: host.max_workers}}
191220
end
192221

mix.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Hemdal.MixProject do
22
use Mix.Project
33

4-
@version "1.0.2"
4+
@version "1.0.3"
55

66
def project do
77
[
@@ -15,12 +15,21 @@ defmodule Hemdal.MixProject do
1515
description: "Hemdal Alarms/Alerts System",
1616
docs: docs(),
1717
package: package(),
18+
test_coverage: coverage(),
1819
preferred_cli_env: [
1920
check: :test
2021
]
2122
]
2223
end
2324

25+
defp coverage do
26+
[
27+
ignore_modules: [
28+
Hemdal.Host.Supervisor
29+
]
30+
]
31+
end
32+
2433
def application do
2534
[
2635
mod: {Hemdal.Application, []},

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%{
22
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
33
"construct": {:hex, :construct, "3.0.3", "2c0d6ba6fc00cf099e146b18610d5cd45aa2e5c3dc437914c3d45f6c122402b3", [:mix], [], "hexpm", "b83c73dab2dfccf09d8e74124b1ae4797ac061ab680d3feed9a36c4d874c7947"},
4-
"credo": {:hex, :credo, "1.7.11", "d3e805f7ddf6c9c854fd36f089649d7cf6ba74c42bc3795d587814e3c9847102", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "56826b4306843253a66e47ae45e98e7d284ee1f95d53d1612bb483f88a8cf219"},
4+
"credo": {:hex, :credo, "1.7.12", "9e3c20463de4b5f3f23721527fcaf16722ec815e70ff6c60b86412c695d426c1", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8493d45c656c5427d9c729235b99d498bd133421f3e0a683e5c1b561471291e5"},
55
"decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"},
66
"dialyxir": {:hex, :dialyxir, "1.4.5", "ca1571ac18e0f88d4ab245f0b60fa31ff1b12cbae2b11bd25d207f865e8ae78a", [:mix], [{:erlex, ">= 0.2.7", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "b0fb08bb8107c750db5c0b324fa2df5ceaa0f9307690ee3c1f6ba5b9eb5d35c3"},
77
"doctor": {:hex, :doctor, "0.22.0", "223e1cace1f16a38eda4113a5c435fa9b10d804aa72d3d9f9a71c471cc958fe7", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "96e22cf8c0df2e9777dc55ebaa5798329b9028889c4023fed3305688d902cd5b"},

test/hemdal/host_test.exs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
defmodule Hemdal.HostTest do
2+
use ExUnit.Case, async: false
3+
4+
def host(id) do
5+
[
6+
id: Enum.random(1..1000),
7+
name: "empty alert check",
8+
host: [
9+
id: id,
10+
module: Hemdal.Host.Local,
11+
name: "localhost",
12+
max_workers: 1
13+
]
14+
]
15+
end
16+
17+
setup do
18+
Application.put_env(:hemdal, :config_module, Hemdal.Config.Backend.Env)
19+
20+
Application.put_env(:hemdal, Hemdal.Config, [
21+
host("f8441510-95db-4e00-a3e0-1556bb8a778c"),
22+
host("78eb75f9-2ac7-434c-a1a2-330b23c89982")
23+
])
24+
25+
Enum.each(Hemdal.Host.get_all(), &Hemdal.Host.stop/1)
26+
end
27+
28+
test "start & stop host" do
29+
host = Hemdal.Config.get_host_by_id!("f8441510-95db-4e00-a3e0-1556bb8a778c")
30+
31+
assert %Hemdal.Config.Host{
32+
id: "f8441510-95db-4e00-a3e0-1556bb8a778c",
33+
module: Hemdal.Host.Local
34+
} = host
35+
36+
assert :ok = Hemdal.Host.start(host)
37+
assert Hemdal.Host.exists?(host.id)
38+
39+
assert :ok = Hemdal.Host.stop(host.id)
40+
refute Hemdal.Host.exists?(host.id)
41+
end
42+
43+
test "get all hosts" do
44+
host = Hemdal.Config.get_host_by_id!("78eb75f9-2ac7-434c-a1a2-330b23c89982")
45+
46+
assert :ok = Hemdal.Host.start(host)
47+
assert Hemdal.Host.exists?(host.id)
48+
49+
assert host.id in Hemdal.Host.get_all()
50+
51+
assert :ok = Hemdal.Host.stop(host.id)
52+
refute Hemdal.Host.exists?(host.id)
53+
54+
refute host.id in Hemdal.Host.get_all()
55+
end
56+
57+
test "add one reloading all hosts" do
58+
assert :ok = Hemdal.Host.reload_all()
59+
60+
assert ~w[
61+
78eb75f9-2ac7-434c-a1a2-330b23c89982
62+
f8441510-95db-4e00-a3e0-1556bb8a778c
63+
] == Hemdal.Host.get_all()
64+
65+
Application.put_env(:hemdal, Hemdal.Config, [
66+
host("f8441510-95db-4e00-a3e0-1556bb8a778c"),
67+
host("78eb75f9-2ac7-434c-a1a2-330b23c89982"),
68+
host("92412973-4d6c-4c08-86ed-82d64f8ea756")
69+
])
70+
71+
assert :ok = Hemdal.Host.reload_all()
72+
73+
assert ~w[
74+
78eb75f9-2ac7-434c-a1a2-330b23c89982
75+
92412973-4d6c-4c08-86ed-82d64f8ea756
76+
f8441510-95db-4e00-a3e0-1556bb8a778c
77+
] == Hemdal.Host.get_all()
78+
end
79+
80+
test "remove one reloading all hosts" do
81+
assert :ok = Hemdal.Host.reload_all()
82+
83+
assert ~w[
84+
78eb75f9-2ac7-434c-a1a2-330b23c89982
85+
f8441510-95db-4e00-a3e0-1556bb8a778c
86+
] == Hemdal.Host.get_all()
87+
88+
Application.put_env(:hemdal, Hemdal.Config, [
89+
host("78eb75f9-2ac7-434c-a1a2-330b23c89982")
90+
])
91+
92+
assert :ok = Hemdal.Host.reload_all()
93+
94+
assert ~w[
95+
78eb75f9-2ac7-434c-a1a2-330b23c89982
96+
] == Hemdal.Host.get_all()
97+
end
98+
end

0 commit comments

Comments
 (0)