@@ -9,11 +9,10 @@ defmodule NervesHub.Deployments.Orchestrator do
99 """
1010
1111 use GenServer
12+ use OpenTelemetryDecorator
1213
1314 require Logger
1415
15- require OpenTelemetry.Tracer , as: Tracer
16-
1716 alias NervesHub.Devices
1817 alias NervesHub.Devices.Device
1918 alias NervesHub.Repo
@@ -50,60 +49,59 @@ defmodule NervesHub.Deployments.Orchestrator do
5049 As devices update and reconnect, the new orchestrator is told that the update
5150 was successful, and the process is repeated.
5251 """
52+ @ decorate with_span ( "Deployments.Orchestrator.trigger_update" )
5353 def trigger_update ( deployment ) do
54- Tracer . with_span "NervesHub.Deployments.Orchestrator.trigger_update" do
55- :telemetry . execute ( [ :nerves_hub , :deployment , :trigger_update ] , % { count: 1 } )
56-
57- match_conditions = [
58- { :and , { :== , { :map_get , :deployment_id , :"$1" } , deployment . id } ,
59- { :== , { :map_get , :updating , :"$1" } , false } ,
60- { :== , { :map_get , :updates_enabled , :"$1" } , true } ,
61- { :"/=" , { :map_get , :firmware_uuid , :"$1" } , deployment . firmware . uuid } }
62- ]
63-
64- match_return = % {
65- device_id: { :element , 1 , :"$_" } ,
66- pid: { :element , 1 , { :element , 2 , :"$_" } } ,
67- firmware_uuid: { :map_get , :firmware_uuid , { :element , 2 , { :element , 2 , :"$_" } } }
68- }
69-
70- devices =
71- Registry . select ( NervesHub.Devices.Registry , [
72- { { :_ , :_ , :"$1" } , match_conditions , [ match_return ] }
73- ] )
74-
75- # Get a rough count of devices to update
76- count = deployment . concurrent_updates - Devices . count_inflight_updates_for ( deployment )
77- # Just in case inflight goes higher than concurrent, limit it to 0
78- count = max ( count , 0 )
79-
80- # use a reduce to bounce out early?
81- # limit the number of devices to 5 minutes / 500ms?
82-
83- devices
84- |> Enum . take ( count )
85- |> Enum . each ( fn % { device_id: device_id , pid: pid } ->
86- :telemetry . execute ( [ :nerves_hub , :deployment , :trigger_update , :device ] , % { count: 1 } )
87-
88- device = % Device { id: device_id }
89-
90- # Check again because other nodes are processing at the same time
91- if Devices . count_inflight_updates_for ( deployment ) < deployment . concurrent_updates do
92- case Devices . told_to_update ( device , deployment ) do
93- { :ok , inflight_update } ->
94- send ( pid , { "deployments/update" , inflight_update } )
95-
96- :error ->
97- Logger . error (
98- "An inflight update could not be created or found for the device #{ device . identifier } (#{ device . id } )"
99- )
100- end
54+ :telemetry . execute ( [ :nerves_hub , :deployment , :trigger_update ] , % { count: 1 } )
55+
56+ match_conditions = [
57+ { :and , { :== , { :map_get , :deployment_id , :"$1" } , deployment . id } ,
58+ { :== , { :map_get , :updating , :"$1" } , false } ,
59+ { :== , { :map_get , :updates_enabled , :"$1" } , true } ,
60+ { :"/=" , { :map_get , :firmware_uuid , :"$1" } , deployment . firmware . uuid } }
61+ ]
62+
63+ match_return = % {
64+ device_id: { :element , 1 , :"$_" } ,
65+ pid: { :element , 1 , { :element , 2 , :"$_" } } ,
66+ firmware_uuid: { :map_get , :firmware_uuid , { :element , 2 , { :element , 2 , :"$_" } } }
67+ }
68+
69+ devices =
70+ Registry . select ( NervesHub.Devices.Registry , [
71+ { { :_ , :_ , :"$1" } , match_conditions , [ match_return ] }
72+ ] )
73+
74+ # Get a rough count of devices to update
75+ count = deployment . concurrent_updates - Devices . count_inflight_updates_for ( deployment )
76+ # Just in case inflight goes higher than concurrent, limit it to 0
77+ count = max ( count , 0 )
78+
79+ # use a reduce to bounce out early?
80+ # limit the number of devices to 5 minutes / 500ms?
81+
82+ devices
83+ |> Enum . take ( count )
84+ |> Enum . each ( fn % { device_id: device_id , pid: pid } ->
85+ :telemetry . execute ( [ :nerves_hub , :deployment , :trigger_update , :device ] , % { count: 1 } )
86+
87+ device = % Device { id: device_id }
88+
89+ # Check again because other nodes are processing at the same time
90+ if Devices . count_inflight_updates_for ( deployment ) < deployment . concurrent_updates do
91+ case Devices . told_to_update ( device , deployment ) do
92+ { :ok , inflight_update } ->
93+ send ( pid , { "deployments/update" , inflight_update } )
94+
95+ :error ->
96+ Logger . error (
97+ "An inflight update could not be created or found for the device #{ device . identifier } (#{ device . id } )"
98+ )
10199 end
100+ end
102101
103- # Slow the update a bit to allow for concurrent nodes
104- Process . sleep ( 500 )
105- end )
106- end
102+ # Slow the update a bit to allow for concurrent nodes
103+ Process . sleep ( 500 )
104+ end )
107105 end
108106
109107 def init ( deployment ) do
0 commit comments