Skip to content

Commit

Permalink
wip - message passing on resource readyness
Browse files Browse the repository at this point in the history
  • Loading branch information
lusergit committed Dec 13, 2024
1 parent 4b1c9b7 commit aa34bfe
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
41 changes: 41 additions & 0 deletions backend/lib/edgehog/containers/container/changes/emit_if_new.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Container.Changes.EmitIfNew do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.PubSub

@impl Ash.Resource.Change
def change(changeset, _opts, _context) do
container_deploy = changeset.data

if container_deploy.state == :sent,
do:
Ash.Changeset.after_transaction(changeset, fn _changeset, result ->
with {:ok, _} <- result do
# publish the available container event for the deployment only if the
# transaction succeds
PubSub.publish!(:available_container, container_deploy)
end
end)
end
end
36 changes: 36 additions & 0 deletions backend/lib/edgehog/containers/container/checker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Container.Checker do
@moduledoc false
use GenStateMachine, callback_mode: [:handle_event_function, :state_enter]

alias Edgehog.PubSub

@impl GenStateMachine
def init(opts) do
# Assumption: opts contains the image deployment to listen for events
image_deployment = Keyword.fetch!(opts, :image_deployment)
deployment = Keyword.fetch!(opts, :deployment)

PubSub.subscribe_to_events_for(image_deployment)
PubSub.subscribe_to_events_for(deployment)
end
end
1 change: 1 addition & 0 deletions backend/lib/edgehog/containers/container/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ defmodule Edgehog.Containers.Container.Deployment do
end

update :received do
change Changes.EmitIfNew
change transition_state(:received)
end

Expand Down
41 changes: 41 additions & 0 deletions backend/lib/edgehog/containers/image/changes/emit_if_new.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Image.Changes.EmitIfNew do
@moduledoc false
use Ash.Resource.Change

alias Edgehog.PubSub

@impl Ash.Resource.Change
def change(changeset, _opts, _context) do
image_deploy = changeset.data

if image_deploy.state == :sent,
do:
Ash.Changeset.after_transaction(changeset, fn _changeset, result ->
with {:ok, _} <- result do
# publish the available image event for the deployment only if the
# transaction succeds
PubSub.publish!(:image_ready, image_deploy)
end
end)
end
end
29 changes: 29 additions & 0 deletions backend/lib/edgehog/containers/release/deployment/checker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Containers.Release.Deployment.Checker do
@moduledoc false

use GenStateMachine, callback_mode: [:handle_event_function, :state_enter]

@impl GenStateMachine
def init(opts) do
end
end
6 changes: 6 additions & 0 deletions backend/lib/edgehog/pubsub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Edgehog.PubSub do
This module implements a PubSub system for events happening inside Edgehog
"""

alias Edgehog.Containers.Image
alias Edgehog.OSManagement.OTAOperation

@type event :: :ota_operation_created | :ota_operation_updated | :release_deployment_available
Expand Down Expand Up @@ -51,6 +52,10 @@ defmodule Edgehog.PubSub do
broadcast_many!(topics, payload)
end

def publish!(:image_ready = event, %Image.Deployment{} = deployment) do
broadcast_many!([topic_for_subject(deployment)], event)
end

def publish!(event, payload) do
broadcast_many!([topic_for_subject(event)], payload)
end
Expand All @@ -75,6 +80,7 @@ defmodule Edgehog.PubSub do

defp topic_for_subject(subject)
defp topic_for_subject(%OTAOperation{id: id}), do: "ota_operations:#{id}"
defp topic_for_subject(%Image.Deployment{id: id}), do: "image_deployment:#{id}"
defp topic_for_subject({:ota_operation, id}), do: "ota_operations:#{id}"
defp topic_for_subject(:ota_operations), do: "ota_operations:*"
defp topic_for_subject(subject) when is_atom(subject), do: Atom.to_string(subject)
Expand Down

0 comments on commit aa34bfe

Please sign in to comment.