Skip to content

Commit fc8fa40

Browse files
committed
Open otaFile for each worker
1 parent 8dba78f commit fc8fa40

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

command/ota/upload.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ func Upload(params *UploadParams) (*UploadResp, error) {
7878
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
7979
}
8080

81-
file, err := os.Open(otaFile)
82-
if err != nil {
83-
return nil, fmt.Errorf("%s: %w", "cannot open ota file", err)
84-
}
85-
8681
conf, err := config.Retrieve()
8782
if err != nil {
8883
return nil, err
@@ -110,7 +105,7 @@ func Upload(params *UploadParams) (*UploadResp, error) {
110105
expiration = otaDeferredExpirationMins
111106
}
112107

113-
good, fail, ers := run(iotClient, valid, file, expiration)
108+
good, fail, ers := run(iotClient, valid, otaFile, expiration)
114109
if err != nil {
115110
return nil, err
116111
}
@@ -167,24 +162,34 @@ func validateDevices(iotClient iot.Client, ids []string, fqbn string) (valid, in
167162
return valid, invalid, details, nil
168163
}
169164

170-
func run(iotClient iot.Client, ids []string, file *os.File, expiration int) (updated, failed, errors []string) {
171-
targets := make(chan string, len(ids))
165+
func run(iotClient iot.Client, ids []string, otaFile string, expiration int) (updated, failed, errors []string) {
166+
type job struct {
167+
id string
168+
file *os.File
169+
}
170+
jobs := make(chan job, len(ids))
171+
172172
type result struct {
173173
id string
174174
err error
175175
}
176176
results := make(chan result, len(ids))
177177

178178
for _, id := range ids {
179-
targets <- id
179+
file, err := os.Open(otaFile)
180+
if err != nil {
181+
failed = append(failed, id)
182+
errors = append(errors, fmt.Sprintf("%s: cannot open ota file", id))
183+
}
184+
jobs <- job{id: id, file: file}
180185
}
181-
close(targets)
186+
close(jobs)
182187

183188
for i := 0; i < numConcurrentUploads; i++ {
184189
go func() {
185-
for id := range targets {
186-
err := iotClient.DeviceOTA(id, file, expiration)
187-
results <- result{id: id, err: err}
190+
for job := range jobs {
191+
err := iotClient.DeviceOTA(job.id, job.file, expiration)
192+
results <- result{id: job.id, err: err}
188193
}
189194
}()
190195
}

0 commit comments

Comments
 (0)