Skip to content

Commit

Permalink
update_campaigns: spawn executor when creating a campaign
Browse files Browse the repository at this point in the history
Also add relevant tests
  • Loading branch information
rbino committed Jul 10, 2023
1 parent c413d2f commit a060aa8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend/lib/edgehog/update_campaigns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Edgehog.UpdateCampaigns do
alias Ecto.Multi
alias Edgehog.BaseImages
alias Edgehog.Devices
alias Edgehog.UpdateCampaigns.ExecutorSupervisor
alias Edgehog.UpdateCampaigns.Target
alias Edgehog.UpdateCampaigns.UpdateCampaign
alias Edgehog.UpdateCampaigns.UpdateChannel
Expand Down Expand Up @@ -452,6 +453,9 @@ defmodule Edgehog.UpdateCampaigns do
end,
placeholders: placeholders
)
|> Multi.run(:executor, fn _repo, %{update_campaign: update_campaign} ->
ExecutorSupervisor.start_executor(update_campaign)
end)
|> Repo.transaction()
|> case do
{:ok, %{update_campaign: update_campaign}} ->
Expand Down
17 changes: 17 additions & 0 deletions backend/test/edgehog/update_campaigns_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule Edgehog.UpdateCampaignsTest do
alias Edgehog.DevicesFixtures
alias Edgehog.Groups
alias Edgehog.UpdateCampaigns
alias Edgehog.UpdateCampaigns.ExecutorRegistry
alias Edgehog.UpdateCampaigns.PushRollout
alias Edgehog.UpdateCampaigns.UpdateCampaign
alias Edgehog.UpdateCampaigns.UpdateChannel
Expand Down Expand Up @@ -338,6 +339,9 @@ defmodule Edgehog.UpdateCampaignsTest do
assert update_campaign.update_targets == []
assert update_campaign.status == :finished
assert update_campaign.outcome == :success

# Check that no executor got started
assert :error = fetch_update_campaign_executor_pid(update_campaign)
end

test "with some targets creates an :idle update_campaign" do
Expand All @@ -357,6 +361,10 @@ defmodule Edgehog.UpdateCampaignsTest do
assert target.device_id == device.id
assert update_campaign.status == :idle
assert update_campaign.outcome == nil

# Check that the executor got started
assert {:ok, pid} = fetch_update_campaign_executor_pid(update_campaign)
assert {:wait_for_start_execution, _data} = :sys.get_state(pid)
end

test "fails with invalid rollout mechanism" do
Expand Down Expand Up @@ -551,6 +559,15 @@ defmodule Edgehog.UpdateCampaignsTest do
UpdateCampaigns.create_update_campaign(update_channel, base_image, attrs)
end

defp fetch_update_campaign_executor_pid(update_campaign) do
key = {update_campaign.tenant_id, update_campaign.id}

case Registry.lookup(ExecutorRegistry, key) do
[] -> :error
[{pid, _}] -> {:ok, pid}
end
end

defp device_fixture do
# Helper to avoid having to manually create the cluster and realm
# TODO: this will be eliminated once we have proper lazy fixtures (see issue #267)
Expand Down

0 comments on commit a060aa8

Please sign in to comment.