Skip to content

Commit f89e100

Browse files
authored
Handle cases where, upon connection, we know the device is already updating (#1869)
This fixes issues where socket reconnections cause us to mark a device as having updated several times. Thankfully we have been receiving this information from Link for around 4 years (thanks to @jjcarstens), we just haven't been using it.
1 parent a8537b1 commit f89e100

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/nerves_hub_web/channels/device_channel.ex

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ defmodule NervesHubWeb.DeviceChannel do
2727
{:ok, device} ->
2828
send(self(), {:after_join, params})
2929

30-
{:ok, assign(socket, :device, device)}
30+
socket =
31+
socket
32+
|> assign(:currently_downloading_uuid, params["currently_downloading_uuid"])
33+
|> assign(:update_started?, !!params["currently_downloading_uuid"])
34+
|> assign(:device, device)
35+
36+
{:ok, socket}
3137

3238
err ->
3339
Logger.warning("[DeviceChannel] failure to connect - #{inspect(err)}")
@@ -95,7 +101,7 @@ defmodule NervesHubWeb.DeviceChannel do
95101
deployment_id: device.deployment_id,
96102
firmware_uuid: get_in(device, [Access.key(:firmware_metadata), Access.key(:uuid)]),
97103
updates_enabled: device.updates_enabled && !Devices.device_in_penalty_box?(device),
98-
updating: false
104+
updating: socket.assigns.update_started?
99105
}
100106

101107
case Registry.register(NervesHub.Devices.Registry, device.id, payload) do
@@ -305,10 +311,13 @@ defmodule NervesHubWeb.DeviceChannel do
305311
percent: percent
306312
})
307313

308-
# if this is the first fwup we see, then mark it as an update attempt
309-
if socket.assigns[:update_started?] do
314+
# if we know the update has already started, we can move on
315+
if socket.assigns.update_started? do
310316
{:noreply, socket}
311317
else
318+
# if this is the first fwup we see, and we didn't know the update had already started,
319+
# then mark it as an update attempt
320+
#
312321
# reload update attempts because they might have been cleared
313322
# and we have a cached stale version
314323
updated_device = Repo.reload(device)

0 commit comments

Comments
 (0)