Skip to content

Commit

Permalink
update_campaigns: add supervision tree for executors
Browse files Browse the repository at this point in the history
Start a Registry and a DynamicSupervisor to allow spawning, supervising and
identifying the UpdateCampaign executor processes
  • Loading branch information
rbino committed Jul 10, 2023
1 parent a94fbf1 commit c413d2f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/lib/edgehog/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ defmodule Edgehog.Application do
{Phoenix.PubSub, name: Edgehog.PubSub},
# Start Finch
{Finch, name: EdgehogFinch},
# Start the UpdateCampaigns supervisor
Edgehog.UpdateCampaigns.Supervisor,
# Start the Endpoint (http/https)
EdgehogWeb.Endpoint
# Start a worker by calling: Edgehog.Worker.start_link(arg)
Expand Down
78 changes: 78 additions & 0 deletions backend/lib/edgehog/update_campaigns/executor_supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# This file is part of Edgehog.
#
# Copyright 2023 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.UpdateCampaigns.ExecutorSupervisor do
use DynamicSupervisor

alias Edgehog.UpdateCampaigns.{
ExecutorRegistry,
PushRollout,
UpdateCampaign
}

# Public API

def start_link(init_arg) do
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end

def start_executor(update_campaign) do
%UpdateCampaign{
id: update_campaign_id,
rollout_mechanism: rollout_mechanism,
tenant_id: tenant_id
} = update_campaign

executor_id = {tenant_id, update_campaign_id}
name = {:via, Registry, {ExecutorRegistry, executor_id}}

base_args = [
name: name,
update_campaign_id: update_campaign_id,
tenant_id: tenant_id
]

child_spec =
executor_child_spec(rollout_mechanism, base_args)
|> Supervisor.child_spec(id: executor_id)

DynamicSupervisor.start_child(__MODULE__, child_spec)
end

defp executor_child_spec(%PushRollout{} = _rollout_mechanism, base_args) do
# During tests we add `:wait_for_start_execution` to avoid having the executor running
# without us being ready to test it
args =
if Mix.env() == :test do
Keyword.put(base_args, :wait_for_start_execution, true)
else
base_args
end

{PushRollout.Executor, args}
end

# Callbacks

@impl DynamicSupervisor
def init(_init_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
end
end
40 changes: 40 additions & 0 deletions backend/lib/edgehog/update_campaigns/supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# This file is part of Edgehog.
#
# Copyright 2023 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.UpdateCampaigns.Supervisor do
use Supervisor

alias Edgehog.UpdateCampaigns.ExecutorRegistry
alias Edgehog.UpdateCampaigns.ExecutorSupervisor

def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end

@impl Supervisor
def init(_init_arg) do
children = [
{Registry, name: ExecutorRegistry, keys: :unique},
ExecutorSupervisor
]

Supervisor.init(children, strategy: :rest_for_one)
end
end

0 comments on commit c413d2f

Please sign in to comment.