Skip to content

Commit

Permalink
Filter out deregistered hosts from heartbeat failed checking (#1593)
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 authored Jul 5, 2023
1 parent c9e56ba commit 68c66b6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/trento/application/usecases/hosts/heartbeats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ defmodule Trento.Heartbeats do
"""

alias Trento.Domain.Commands.UpdateHeartbeat
alias Trento.Heartbeat

alias Trento.{
Heartbeat,
HostReadModel
}

alias Trento.Support.DateService

alias Trento.Repo
Expand Down Expand Up @@ -65,9 +70,12 @@ defmodule Trento.Heartbeats do
defp get_all_expired_heartbeats(date_service) do
query =
from h in Heartbeat,
join: host in HostReadModel,
on: h.agent_id == type(host.id, :string),
where:
h.timestamp <
^DateTime.add(date_service.utc_now(), -@heartbeat_interval, :millisecond)
is_nil(host.deregistered_at) and
h.timestamp <
^DateTime.add(date_service.utc_now(), -@heartbeat_interval, :millisecond)

Repo.all(query)
end
Expand Down
27 changes: 27 additions & 0 deletions test/trento/application/usecases/heartbeats_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,31 @@ defmodule Trento.HeartbeatsTest do

Heartbeats.dispatch_heartbeat_failed_commands(Trento.Support.DateService.Mock)
end

test "filter deregistered hosts from heartbeat failed check" do
%{id: agent_id} = insert(:host, heartbeat: :passing, deregistered_at: DateTime.utc_now())
%{timestamp: now} = insert(:heartbeat, agent_id: agent_id)

expired_time =
DateTime.add(
now,
Application.get_env(:trento, Heartbeats)[:interval] + 1,
:millisecond
)

expect(
Trento.Support.DateService.Mock,
:utc_now,
fn -> expired_time end
)

expect(
Trento.Commanded.Mock,
:dispatch,
0,
fn _ -> :ok end
)

Heartbeats.dispatch_heartbeat_failed_commands(Trento.Support.DateService.Mock)
end
end

0 comments on commit 68c66b6

Please sign in to comment.