@@ -116,37 +116,42 @@ func idsGivenTags(iotClient iot.Client, tags map[string]string) ([]string, error
116
116
}
117
117
118
118
func run (iotClient iot.Client , ids []string , file * os.File , expiration int ) error {
119
- idsToProcess := make (chan string , 2000 )
120
- idsFailed := make (chan string , 2000 )
119
+ targets := make (chan string , len (ids ))
120
+ type result struct {
121
+ id string
122
+ err error
123
+ }
124
+ results := make (chan result , len (ids ))
125
+
121
126
for _ , id := range ids {
122
- idsToProcess <- id
127
+ targets <- id
123
128
}
124
- close (idsToProcess )
129
+ close (targets )
125
130
126
131
for i := 0 ; i < numConcurrentUploads ; i ++ {
127
132
go func () {
128
- for id := range idsToProcess {
133
+ for id := range targets {
129
134
err := iotClient .DeviceOTA (id , file , expiration )
130
- fail := ""
131
- if err != nil {
132
- fail = id
133
- }
134
- idsFailed <- fail
135
+ results <- result {id : id , err : err }
135
136
}
136
137
}()
137
138
}
138
139
139
- failMsg := ""
140
+ var fails []string
141
+ var details []string
140
142
for range ids {
141
- i := <- idsFailed
142
- if i != "" {
143
- failMsg = strings .Join ([]string {i , failMsg }, "," )
143
+ r := <- results
144
+ if r .err != nil {
145
+ fails = append (fails , r .id )
146
+ details = append (details , fmt .Sprintf ("%s: %s" , r .id , r .err .Error ()))
144
147
}
145
148
}
146
149
147
- if failMsg != "" {
148
- failMsg = strings .TrimRight (failMsg , "," )
149
- return fmt .Errorf ("failed to update these devices: %s" , failMsg )
150
+ if len (fails ) > 0 {
151
+ f := strings .Join (fails , "," )
152
+ f = strings .TrimRight (f , "," )
153
+ d := strings .Join (details , "\n " )
154
+ return fmt .Errorf ("failed to update these devices: %s\n reasons:\n %s" , f , d )
150
155
}
151
156
return nil
152
157
}
0 commit comments