Skip to content

Commit

Permalink
Add application instance deregistration logic (#1357)
Browse files Browse the repository at this point in the history

Co-authored-by: Jamie Rodríguez <jamie.rodriguez@suse.com>
  • Loading branch information
rtorrero and jamie-suse committed Jun 16, 2023
1 parent 668d9fb commit ace0e7a
Show file tree
Hide file tree
Showing 3 changed files with 576 additions and 4 deletions.
96 changes: 95 additions & 1 deletion lib/trento/domain/sap_system/sap_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,22 @@ defmodule Trento.Domain.SapSystem do
}

alias Trento.Domain.Commands.{
DeregisterApplicationInstance,
RegisterApplicationInstance,
RegisterDatabaseInstance,
RollUpSapSystem
}

alias Trento.Domain.Events.{
ApplicationInstanceDeregistered,
ApplicationInstanceHealthChanged,
ApplicationInstanceRegistered,
DatabaseHealthChanged,
DatabaseInstanceHealthChanged,
DatabaseInstanceRegistered,
DatabaseInstanceSystemReplicationChanged,
DatabaseRegistered,
SapSystemDeregistered,
SapSystemHealthChanged,
SapSystemRegistered,
SapSystemRolledUp,
Expand All @@ -81,9 +84,10 @@ defmodule Trento.Domain.SapSystem do

deftype do
field :sap_system_id, Ecto.UUID
field :sid, :string
field :sid, :string, default: nil
field :health, Ecto.Enum, values: Health.values()
field :rolling_up, :boolean, default: false
field :deregistered_at, :utc_datetime_usec, default: nil

embeds_one :database, Database
embeds_one :application, Application
Expand Down Expand Up @@ -290,6 +294,71 @@ defmodule Trento.Domain.SapSystem do
}
end

# Deregister an application instance and emit a ApplicationInstanceDeregistered
# also emit SapSystemDeregistered event if this was the last application instance
def execute(
%SapSystem{
sap_system_id: sap_system_id
} = sap_system,
%DeregisterApplicationInstance{
instance_number: instance_number,
sap_system_id: sap_system_id,
host_id: host_id,
deregistered_at: deregistered_at
}
) do
sap_system
|> Multi.new()
|> Multi.execute(fn _ ->
%ApplicationInstanceDeregistered{
sap_system_id: sap_system_id,
instance_number: instance_number,
host_id: host_id,
deregistered_at: deregistered_at
}
end)
|> Multi.execute(fn sap_system ->
maybe_emit_sap_system_deregistered_event(
sap_system,
deregistered_at
)
end)
end

def apply(
%SapSystem{application: %Application{instances: instances}} = sap_system,
%ApplicationInstanceDeregistered{instance_number: instance_number, host_id: host_id}
) do
instances =
Enum.reject(instances, fn
%Instance{instance_number: ^instance_number, host_id: ^host_id} ->
true

_ ->
false
end)

%SapSystem{
sap_system
| application: %Application{
instances: instances
}
}
end

def apply(
%SapSystem{} = sap_system,
%SapSystemDeregistered{
deregistered_at: deregistered_at
}
) do
%SapSystem{
sap_system
| deregistered_at: deregistered_at,
sid: nil
}
end

def apply(
%SapSystem{sap_system_id: nil},
%DatabaseRegistered{
Expand Down Expand Up @@ -633,6 +702,31 @@ defmodule Trento.Domain.SapSystem do
end
end

defp maybe_emit_sap_system_deregistered_event(
%SapSystem{sid: nil},
_deregistered_at
),
do: []

defp maybe_emit_sap_system_deregistered_event(
%SapSystem{
sap_system_id: sap_system_id,
application: %Application{
instances: instances
}
},
deregistered_at
) do
has_abap? = Enum.any?(instances, fn %{features: features} -> features =~ "ABAP" end)

has_messageserver? =
Enum.any?(instances, fn %{features: features} -> features =~ "MESSAGESERVER" end)

unless has_abap? and has_messageserver? do
%SapSystemDeregistered{sap_system_id: sap_system_id, deregistered_at: deregistered_at}
end
end

defp get_instance(instances, host_id, instance_number) do
Enum.find(instances, fn
%Instance{host_id: ^host_id, instance_number: ^instance_number} ->
Expand Down
12 changes: 9 additions & 3 deletions lib/trento/infrastructure/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Trento.Router do

alias Trento.Domain.Commands.{
CompleteChecksExecution,
DeregisterApplicationInstance,
DeregisterClusterHost,
DeregisterHost,
RegisterApplicationInstance,
Expand Down Expand Up @@ -58,7 +59,12 @@ defmodule Trento.Router do

identify SapSystem, by: :sap_system_id

dispatch [RegisterApplicationInstance, RegisterDatabaseInstance, RollUpSapSystem],
to: SapSystem,
lifespan: SapSystem.Lifespan
dispatch [
DeregisterApplicationInstance,
RegisterApplicationInstance,
RegisterDatabaseInstance,
RollUpSapSystem
],
to: SapSystem,
lifespan: SapSystem.Lifespan
end
Loading

0 comments on commit ace0e7a

Please sign in to comment.