Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove an extra Deployment preload during after_join #1618

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/nerves_hub/deployments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule NervesHub.Deployments do
alias NervesHub.AuditLogs
alias NervesHub.Deployments.Deployment
alias NervesHub.Deployments.InflightDeploymentCheck
alias NervesHub.Devices
alias NervesHub.Devices.Device
alias NervesHub.Products.Product
alias NervesHub.Repo
Expand Down Expand Up @@ -498,25 +499,25 @@ defmodule NervesHub.Deployments do

[deployment] ->
device
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:deployment_id, deployment.id)
|> Repo.update!()
|> Repo.preload([:deployment])
|> Devices.update_deployment(deployment)
|> preload_with_firmware_and_archive(true)

[deployment | _] ->
Logger.debug(
"More than one deployment matches for #{device.identifier}, setting to the first"
)

device
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:deployment_id, deployment.id)
|> Repo.update!()
|> Repo.preload([:deployment])
|> Devices.update_deployment(deployment)
|> preload_with_firmware_and_archive(true)
end
end

def set_deployment(device) do
Repo.preload(device, [:deployment])
preload_with_firmware_and_archive(device)
end

def preload_with_firmware_and_archive(device, force \\ false) do
Repo.preload(device, [deployment: [:archive, :firmware]], force: force)
end
end
7 changes: 7 additions & 0 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,13 @@ defmodule NervesHub.Devices do

def matches_deployment?(_, _), do: false

def update_deployment(device, deployment) do
device
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:deployment_id, deployment.id)
|> Repo.update!()
end

@spec failure_threshold_met?(Device.t(), Deployment.t()) :: boolean()
def failure_threshold_met?(%Device{} = device, %Deployment{} = deployment) do
Enum.count(device.update_attempts) >= deployment.device_failure_threshold
Expand Down
13 changes: 8 additions & 5 deletions lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ defmodule NervesHubWeb.DeviceChannel do
end

def handle_info({:after_join, params}, %{assigns: %{device: device}} = socket) do
device =
device
|> Devices.verify_deployment()
|> Deployments.set_deployment()
|> deployment_preload()
device = maybe_update_deployment(device)

maybe_send_public_keys(device, socket, params)

Expand Down Expand Up @@ -476,6 +472,13 @@ defmodule NervesHubWeb.DeviceChannel do
:ok
end

defp maybe_update_deployment(device) do
device
|> Deployments.preload_with_firmware_and_archive()
|> Devices.verify_deployment()
|> Deployments.set_deployment()
end

defp log_to_sentry(device, message, extra \\ %{}) do
Sentry.Context.set_tags_context(%{
device_identifier: device.identifier,
Expand Down
Loading