Skip to content

Commit 11155e8

Browse files
committed
feat: Add API to increase max_in_progress_operations
Allow users to increase max_in_progress_operations while the campaign is running. Closes #278 Signed-off-by: Emina Muminovic <emina.muminovic@secomind.com>
1 parent 363e901 commit 11155e8

File tree

15 files changed

+680
-35
lines changed

15 files changed

+680
-35
lines changed

backend/lib/edgehog/campaigns/campaign/campaign.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ defmodule Edgehog.Campaigns.Campaign do
140140
change Changes.StartExecution
141141
end
142142

143+
update :increase_max_in_progress_operations do
144+
require_atomic? false
145+
argument :max_in_progress_operations, :integer
146+
147+
change Changes.IncreaseMaxInProgressOperations
148+
end
149+
143150
destroy :destroy do
144151
description "Deletes a Campaign"
145152
primary? true
@@ -243,4 +250,11 @@ defmodule Edgehog.Campaigns.Campaign do
243250
match_with: [tenant_id: :tenant_id]
244251
end
245252
end
253+
254+
pub_sub do
255+
prefix "campaigns"
256+
module EdgehogWeb.Endpoint
257+
258+
publish :increase_max_in_progress_operations, [[:id, "*"]]
259+
end
246260
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# This file is part of Edgehog.
3+
#
4+
# Copyright 2026 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule Edgehog.Campaigns.Campaign.Changes.IncreaseMaxInProgressOperations do
22+
@moduledoc """
23+
Increases max_in_progress_operations of a campaign mechanism.
24+
"""
25+
26+
use Ash.Resource.Change
27+
28+
@impl Ash.Resource.Change
29+
def change(changeset, _opts, _context) do
30+
max_in_progress_operations =
31+
Ash.Changeset.get_argument(changeset, :max_in_progress_operations)
32+
33+
campaign_mechanism = changeset.data.campaign_mechanism
34+
35+
mechanism = %{
36+
campaign_mechanism
37+
| value:
38+
Map.update(
39+
campaign_mechanism.value,
40+
:max_in_progress_operations,
41+
campaign_mechanism.value.max_in_progress_operations,
42+
fn _ -> max_in_progress_operations end
43+
)
44+
}
45+
46+
Ash.Changeset.change_attribute(changeset, :campaign_mechanism, mechanism)
47+
end
48+
end

backend/lib/edgehog/campaigns/campaign_mechanism/deployment_delete/executor.ex

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,17 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentDelete.Executor do
6161

6262
defp handle_update(notification, state, data) do
6363
case notification.payload.action.name do
64-
:mark_as_timed_out -> handle_mark_as_timed_out(notification, data)
65-
:pause -> handle_mark_as_paused(state, data)
66-
_ -> :keep_state_and_data
64+
:mark_as_timed_out ->
65+
handle_mark_as_timed_out(notification, data)
66+
67+
:pause ->
68+
handle_mark_as_paused(state, data)
69+
70+
:increase_max_in_progress_operations ->
71+
handle_increase_max_in_progress_operations(notification, data)
72+
73+
_ ->
74+
:keep_state_and_data
6775
end
6876
end
6977

@@ -81,6 +89,15 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentDelete.Executor do
8189
{:keep_state_and_data, actions}
8290
end
8391

92+
defp handle_increase_max_in_progress_operations(notification, data) do
93+
data = %{
94+
data
95+
| mechanism: notification.payload.data.campaign_mechanism.value
96+
}
97+
98+
{:keep_state, data}
99+
end
100+
84101
defp handle_destroy(notification, data) do
85102
case notification.payload.action.name do
86103
:destroy_and_gc -> handle_destroy_and_gc(notification, data)

backend/lib/edgehog/campaigns/campaign_mechanism/deployment_deploy/executor.ex

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,20 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentDeploy.Executor do
6060

6161
defp handle_update(notification, state, data) do
6262
case notification.payload.action.name do
63-
:maybe_run_ready_actions -> handle_maybe_run_ready_actions(notification, data)
64-
:mark_as_timed_out -> handle_mark_as_timed_out(notification, data)
65-
:pause -> handle_mark_as_paused(state, data)
66-
_ -> :keep_state_and_data
63+
:maybe_run_ready_actions ->
64+
handle_maybe_run_ready_actions(notification, data)
65+
66+
:mark_as_timed_out ->
67+
handle_mark_as_timed_out(notification, data)
68+
69+
:pause ->
70+
handle_mark_as_paused(state, data)
71+
72+
:increase_max_in_progress_operations ->
73+
handle_increase_max_in_progress_operations(notification, data)
74+
75+
_ ->
76+
:keep_state_and_data
6777
end
6878
end
6979

@@ -112,4 +122,13 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentDeploy.Executor do
112122
# Ignore pause requests in non-pauseable states (terminal states, already pausing, etc.)
113123
:keep_state_and_data
114124
end
125+
126+
defp handle_increase_max_in_progress_operations(notification, data) do
127+
data = %{
128+
data
129+
| mechanism: notification.payload.data.campaign_mechanism.value
130+
}
131+
132+
{:keep_state, data}
133+
end
115134
end

backend/lib/edgehog/campaigns/campaign_mechanism/deployment_start/executor.ex

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,20 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentStart.Executor do
6060

6161
defp handle_update(notification, state, data) do
6262
case notification.payload.action.name do
63-
:maybe_run_ready_actions -> handle_maybe_run_ready_actions(notification, data)
64-
:mark_as_timed_out -> handle_mark_as_timed_out(notification, data)
65-
:pause -> handle_mark_as_paused(state, data)
66-
_ -> :keep_state_and_data
63+
:maybe_run_ready_actions ->
64+
handle_maybe_run_ready_actions(notification, data)
65+
66+
:mark_as_timed_out ->
67+
handle_mark_as_timed_out(notification, data)
68+
69+
:pause ->
70+
handle_mark_as_paused(state, data)
71+
72+
:increase_max_in_progress_operations ->
73+
handle_increase_max_in_progress_operations(notification, data)
74+
75+
_ ->
76+
:keep_state_and_data
6777
end
6878
end
6979

@@ -116,4 +126,13 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentStart.Executor do
116126
# Ignore pause requests in non-pauseable states (terminal states, already pausing, etc.)
117127
:keep_state_and_data
118128
end
129+
130+
defp handle_increase_max_in_progress_operations(notification, data) do
131+
data = %{
132+
data
133+
| mechanism: notification.payload.data.campaign_mechanism.value
134+
}
135+
136+
{:keep_state, data}
137+
end
119138
end

backend/lib/edgehog/campaigns/campaign_mechanism/deployment_stop/executor.ex

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,20 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentStop.Executor do
6161

6262
defp handle_update(notification, state, data) do
6363
case notification.payload.action.name do
64-
:maybe_run_ready_actions -> handle_maybe_run_ready_actions(notification, data)
65-
:mark_as_timed_out -> handle_mark_as_timed_out(notification, data)
66-
:pause -> handle_mark_as_paused(state, data)
67-
_ -> :keep_state_and_data
64+
:maybe_run_ready_actions ->
65+
handle_maybe_run_ready_actions(notification, data)
66+
67+
:mark_as_timed_out ->
68+
handle_mark_as_timed_out(notification, data)
69+
70+
:pause ->
71+
handle_mark_as_paused(state, data)
72+
73+
:increase_max_in_progress_operations ->
74+
handle_increase_max_in_progress_operations(notification, data)
75+
76+
_ ->
77+
:keep_state_and_data
6878
end
6979
end
7080

@@ -117,4 +127,13 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentStop.Executor do
117127
# Ignore pause requests in non-pauseable states (terminal states, already pausing, etc.)
118128
:keep_state_and_data
119129
end
130+
131+
defp handle_increase_max_in_progress_operations(notification, data) do
132+
data = %{
133+
data
134+
| mechanism: notification.payload.data.campaign_mechanism.value
135+
}
136+
137+
{:keep_state, data}
138+
end
120139
end

backend/lib/edgehog/campaigns/campaign_mechanism/deployment_upgrade/executor.ex

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,20 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentUpgrade.Executor do
6262

6363
defp handle_update(notification, state, data) do
6464
case notification.payload.action.name do
65-
:maybe_run_ready_actions -> handle_maybe_run_ready_actions(notification, data)
66-
:mark_as_timed_out -> handle_mark_as_timed_out(notification, data)
67-
:pause -> handle_mark_as_paused(state, data)
68-
_ -> :keep_state_and_data
65+
:maybe_run_ready_actions ->
66+
handle_maybe_run_ready_actions(notification, data)
67+
68+
:mark_as_timed_out ->
69+
handle_mark_as_timed_out(notification, data)
70+
71+
:pause ->
72+
handle_mark_as_paused(state, data)
73+
74+
:increase_max_in_progress_operations ->
75+
handle_increase_max_in_progress_operations(notification, data)
76+
77+
_ ->
78+
:keep_state_and_data
6979
end
7080
end
7181

@@ -136,4 +146,13 @@ defmodule Edgehog.Campaigns.CampaignMechanism.DeploymentUpgrade.Executor do
136146
# Ignore pause requests in non-pauseable states (terminal states, already pausing, etc.)
137147
:keep_state_and_data
138148
end
149+
150+
defp handle_increase_max_in_progress_operations(notification, data) do
151+
data = %{
152+
data
153+
| mechanism: notification.payload.data.campaign_mechanism.value
154+
}
155+
156+
{:keep_state, data}
157+
end
139158
end

backend/lib/edgehog/campaigns/campaign_mechanism/firmware_upgrade/executor.ex

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,17 @@ defmodule Edgehog.Campaigns.CampaignMechanism.FirmwareUpgrade.Executor do
6262

6363
defp handle_update(notification, state, data) do
6464
case notification.payload.action.name do
65-
:update_status -> handle_update_status(notification, data)
66-
:pause -> handle_mark_as_paused(state, data)
67-
_ -> :keep_state_and_data
65+
:update_status ->
66+
handle_update_status(notification, data)
67+
68+
:pause ->
69+
handle_mark_as_paused(state, data)
70+
71+
:increase_max_in_progress_operations ->
72+
handle_increase_max_in_progress_operations(notification, data)
73+
74+
_ ->
75+
:keep_state_and_data
6876
end
6977
end
7078

@@ -119,4 +127,13 @@ defmodule Edgehog.Campaigns.CampaignMechanism.FirmwareUpgrade.Executor do
119127
# Ignore pause requests in non-pauseable states (terminal states, already pausing, etc.)
120128
:keep_state_and_data
121129
end
130+
131+
defp handle_increase_max_in_progress_operations(notification, data) do
132+
data = %{
133+
data
134+
| mechanism: notification.payload.data.campaign_mechanism.value
135+
}
136+
137+
{:keep_state, data}
138+
end
122139
end

backend/lib/edgehog/campaigns/campaigns.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ defmodule Edgehog.Campaigns do
103103
relay_id_translations input: [target_group_ids: :device_group]
104104
end
105105

106+
update Campaign, :increase_max_in_progress_operations, :increase_max_in_progress_operations
107+
106108
destroy Channel, :delete_channel, :destroy
107109
end
108110
end
@@ -116,6 +118,7 @@ defmodule Edgehog.Campaigns do
116118
define :mark_campaign_successful, action: :mark_as_successful
117119
define :pause_campaign, action: :pause
118120
define :resume_campaign, action: :resume
121+
define :increase_max_in_progress_operations, action: :increase_max_in_progress_operations
119122
end
120123

121124
resource CampaignTarget do

0 commit comments

Comments
 (0)