Skip to content

Commit

Permalink
Remove Multi usage when emitting software updates discovery events
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Apr 12, 2024
1 parent 3b0fcc4 commit 772f280
Showing 1 changed file with 38 additions and 68 deletions.
106 changes: 38 additions & 68 deletions lib/trento/hosts/host.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ defmodule Trento.Hosts.Host do

# New host registered
def execute(
%Host{host_id: nil} = host,
%Host{host_id: nil},
%RegisterHost{
host_id: host_id,
hostname: hostname,
Expand All @@ -172,9 +172,7 @@ defmodule Trento.Hosts.Host do
installation_source: installation_source
}
) do
host
|> Multi.new()
|> Multi.execute(fn _ ->
[
%HostRegistered{
host_id: host_id,
hostname: hostname,
Expand All @@ -188,8 +186,7 @@ defmodule Trento.Hosts.Host do
fully_qualified_domain_name: fully_qualified_domain_name,
heartbeat: :unknown
}
end)
|> Multi.execute(&maybe_emit_software_updates_discovery_requested_event/1)
] ++ maybe_emit_software_updates_discovery_events(host_id, nil, fully_qualified_domain_name)
end

# Reject all the commands, except for the registration ones when the host_id does not exists
Expand All @@ -214,7 +211,7 @@ defmodule Trento.Hosts.Host do
fully_qualified_domain_name: fully_qualified_domain_name,
installation_source: installation_source,
deregistered_at: deregistered_at
} = host,
},
%RegisterHost{
hostname: hostname,
ip_addresses: ip_addresses,
Expand All @@ -228,19 +225,17 @@ defmodule Trento.Hosts.Host do
}
)
when not is_nil(deregistered_at) do
host
|> Multi.new()
|> Multi.execute(fn _ ->
[
%HostRestored{host_id: host_id}
end)
|> Multi.execute(&maybe_emit_software_updates_discovery_requested_event/1)
] ++ maybe_emit_software_updates_discovery_events(host_id, nil, fully_qualified_domain_name)
end

def execute(
%Host{
host_id: host_id,
deregistered_at: deregistered_at
} = host,
deregistered_at: deregistered_at,
fully_qualified_domain_name: current_fully_qualified_domain_name
},
%RegisterHost{
hostname: hostname,
ip_addresses: ip_addresses,
Expand All @@ -249,17 +244,13 @@ defmodule Trento.Hosts.Host do
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version,
fully_qualified_domain_name: fully_qualified_domain_name,
fully_qualified_domain_name: new_fully_qualified_domain_name,
installation_source: installation_source
}
)
when not is_nil(deregistered_at) do
host
|> Multi.new()
|> Multi.execute(fn _ ->
%HostRestored{host_id: host_id}
end)
|> Multi.execute(fn _ ->
[
%HostRestored{host_id: host_id},
%HostDetailsUpdated{
host_id: host_id,
hostname: hostname,
Expand All @@ -269,11 +260,15 @@ defmodule Trento.Hosts.Host do
total_memory_mb: total_memory_mb,
socket_count: socket_count,
os_version: os_version,
fully_qualified_domain_name: fully_qualified_domain_name,
fully_qualified_domain_name: new_fully_qualified_domain_name,
installation_source: installation_source
}
end)
|> Multi.execute(&maybe_emit_software_updates_discovery_requested_event/1)
] ++
maybe_emit_software_updates_discovery_events(
host_id,
current_fully_qualified_domain_name,
new_fully_qualified_domain_name
)
end

def execute(
Expand Down Expand Up @@ -325,7 +320,7 @@ defmodule Trento.Hosts.Host do
def execute(
%Host{
fully_qualified_domain_name: current_fully_qualified_domain_name
} = host,
},
%RegisterHost{
host_id: host_id,
hostname: hostname,
Expand All @@ -339,9 +334,7 @@ defmodule Trento.Hosts.Host do
installation_source: installation_source
}
) do
host
|> Multi.new()
|> Multi.execute(fn _ ->
[
%HostDetailsUpdated{
host_id: host_id,
hostname: hostname,
Expand All @@ -354,14 +347,12 @@ defmodule Trento.Hosts.Host do
os_version: os_version,
installation_source: installation_source
}
end)
|> Multi.execute(fn _ ->
maybe_emit_software_updates_discovery_requested_event_or_clear_up(
] ++
maybe_emit_software_updates_discovery_events(
host_id,
current_fully_qualified_domain_name,
new_fully_qualified_domain_name
)
end)
end

def execute(
Expand Down Expand Up @@ -851,44 +842,23 @@ defmodule Trento.Hosts.Host do
end
end

defp maybe_emit_software_updates_discovery_requested_event(%Host{
fully_qualified_domain_name: nil
}),
do: nil

defp maybe_emit_software_updates_discovery_requested_event(%Host{
host_id: host_id,
fully_qualified_domain_name: fully_qualified_domain_name
}) do
%SoftwareUpdatesDiscoveryRequested{
host_id: host_id,
fully_qualified_domain_name: fully_qualified_domain_name
}
end

defp maybe_emit_software_updates_discovery_requested_event_or_clear_up(
host_id,
current_fully_qualified_domain_name,
new_fully_qualified_domain_name
)
when current_fully_qualified_domain_name != new_fully_qualified_domain_name and
is_nil(new_fully_qualified_domain_name),
do: %SoftwareUpdatesDiscoveryCleared{
host_id: host_id
}
def maybe_emit_software_updates_discovery_events(_host_id, nil, nil), do: []
def maybe_emit_software_updates_discovery_events(_host_id, fqdn, fqdn), do: []

defp maybe_emit_software_updates_discovery_requested_event_or_clear_up(
host_id,
current_fully_qualified_domain_name,
new_fully_qualified_domain_name
)
when current_fully_qualified_domain_name != new_fully_qualified_domain_name,
do: %SoftwareUpdatesDiscoveryRequested{
host_id: host_id,
fully_qualified_domain_name: new_fully_qualified_domain_name
}
def maybe_emit_software_updates_discovery_events(host_id, _old_fqdn, nil),
do: [
%SoftwareUpdatesDiscoveryCleared{
host_id: host_id
}
]

defp maybe_emit_software_updates_discovery_requested_event_or_clear_up(_, _, _), do: nil
def maybe_emit_software_updates_discovery_events(host_id, _old_fqdn, new_fqdn),
do: [
%SoftwareUpdatesDiscoveryRequested{
host_id: host_id,
fully_qualified_domain_name: new_fqdn
}
]

defp compute_saptune_health(nil), do: Health.warning()

Expand Down

0 comments on commit 772f280

Please sign in to comment.