@@ -101,25 +101,25 @@ func (s *packagesSyncer) initStatuses() error {
101
101
func (s * packagesSyncer ) doSync (ctx context.Context ) {
102
102
hash , err := s .localState .AllPackagesHash ()
103
103
if err != nil {
104
- s .logger .Errorf ("Package syncing failed: %V" , err )
104
+ s .logger .Errorf (ctx , "Package syncing failed: %V" , err )
105
105
return
106
106
}
107
107
if bytes .Compare (hash , s .available .AllPackagesHash ) == 0 {
108
- s .logger .Debugf ("All packages are already up to date." )
108
+ s .logger .Debugf (ctx , "All packages are already up to date." )
109
109
return
110
110
}
111
111
112
112
failed := false
113
- if err := s .deleteUnneededLocalPackages (); err != nil {
114
- s .logger .Errorf ("Cannot delete unneeded packages: %v" , err )
113
+ if err := s .deleteUnneededLocalPackages (ctx ); err != nil {
114
+ s .logger .Errorf (ctx , "Cannot delete unneeded packages: %v" , err )
115
115
failed = true
116
116
}
117
117
118
118
// Iterate through offered packages and sync them all from server.
119
119
for name , pkg := range s .available .Packages {
120
120
err := s .syncPackage (ctx , name , pkg )
121
121
if err != nil {
122
- s .logger .Errorf ("Cannot sync package %s: %v" , name , err )
122
+ s .logger .Errorf (ctx , "Cannot sync package %s: %v" , name , err )
123
123
failed = true
124
124
}
125
125
}
@@ -128,15 +128,15 @@ func (s *packagesSyncer) doSync(ctx context.Context) {
128
128
// Update the "all" hash on success, so that next time Sync() does not thing,
129
129
// unless a new hash is received from the Server.
130
130
if err := s .localState .SetAllPackagesHash (s .available .AllPackagesHash ); err != nil {
131
- s .logger .Errorf ("SetAllPackagesHash failed: %v" , err )
131
+ s .logger .Errorf (ctx , "SetAllPackagesHash failed: %v" , err )
132
132
} else {
133
- s .logger .Debugf ("All packages are synced and up to date." )
133
+ s .logger .Debugf (ctx , "All packages are synced and up to date." )
134
134
}
135
135
} else {
136
- s .logger .Errorf ("Package syncing was not successful." )
136
+ s .logger .Errorf (ctx , "Package syncing was not successful." )
137
137
}
138
138
139
- _ = s .reportStatuses (true )
139
+ _ = s .reportStatuses (ctx , true )
140
140
}
141
141
142
142
// syncPackage downloads the package from the server and installs it.
@@ -165,7 +165,7 @@ func (s *packagesSyncer) syncPackage(
165
165
mustCreate := ! pkgLocal .Exists
166
166
if pkgLocal .Exists {
167
167
if bytes .Equal (pkgLocal .Hash , pkgAvail .Hash ) {
168
- s .logger .Debugf ("Package %s hash is unchanged, skipping" , pkgName )
168
+ s .logger .Debugf (ctx , "Package %s hash is unchanged, skipping" , pkgName )
169
169
return nil
170
170
}
171
171
if pkgLocal .Type != pkgAvail .Type {
@@ -183,7 +183,7 @@ func (s *packagesSyncer) syncPackage(
183
183
184
184
// Report that we are beginning to install it.
185
185
status .Status = protobufs .PackageStatusEnum_PackageStatusEnum_Installing
186
- _ = s .reportStatuses (true )
186
+ _ = s .reportStatuses (ctx , true )
187
187
188
188
if mustCreate {
189
189
// Make sure the package exists.
@@ -213,7 +213,7 @@ func (s *packagesSyncer) syncPackage(
213
213
status .Status = protobufs .PackageStatusEnum_PackageStatusEnum_InstallFailed
214
214
status .ErrorMessage = err .Error ()
215
215
}
216
- _ = s .reportStatuses (true )
216
+ _ = s .reportStatuses (ctx , true )
217
217
218
218
return err
219
219
}
@@ -224,7 +224,7 @@ func (s *packagesSyncer) syncPackage(
224
224
func (s * packagesSyncer ) syncPackageFile (
225
225
ctx context.Context , pkgName string , file * protobufs.DownloadableFile ,
226
226
) error {
227
- shouldDownload , err := s .shouldDownloadFile (pkgName , file )
227
+ shouldDownload , err := s .shouldDownloadFile (ctx , pkgName , file )
228
228
if err == nil && shouldDownload {
229
229
err = s .downloadFile (ctx , pkgName , file )
230
230
}
@@ -233,21 +233,18 @@ func (s *packagesSyncer) syncPackageFile(
233
233
}
234
234
235
235
// shouldDownloadFile returns true if the file should be downloaded.
236
- func (s * packagesSyncer ) shouldDownloadFile (
237
- packageName string ,
238
- file * protobufs.DownloadableFile ,
239
- ) (bool , error ) {
236
+ func (s * packagesSyncer ) shouldDownloadFile (ctx context.Context , packageName string , file * protobufs.DownloadableFile ) (bool , error ) {
240
237
fileContentHash , err := s .localState .FileContentHash (packageName )
241
238
242
239
if err != nil {
243
240
err := fmt .Errorf ("cannot calculate checksum of %s: %v" , packageName , err )
244
- s .logger .Errorf (err .Error ())
241
+ s .logger .Errorf (ctx , err .Error ())
245
242
return true , nil
246
243
} else {
247
244
// Compare the checksum of the file we have with what
248
245
// we are offered by the server.
249
246
if bytes .Compare (fileContentHash , file .ContentHash ) != 0 {
250
- s .logger .Debugf ("Package %s: file hash mismatch, will download." , packageName )
247
+ s .logger .Debugf (ctx , "Package %s: file hash mismatch, will download." , packageName )
251
248
return true , nil
252
249
}
253
250
}
@@ -256,7 +253,7 @@ func (s *packagesSyncer) shouldDownloadFile(
256
253
257
254
// downloadFile downloads the file from the server.
258
255
func (s * packagesSyncer ) downloadFile (ctx context.Context , pkgName string , file * protobufs.DownloadableFile ) error {
259
- s .logger .Debugf ("Downloading package %s file from %s" , pkgName , file .DownloadUrl )
256
+ s .logger .Debugf (ctx , "Downloading package %s file from %s" , pkgName , file .DownloadUrl )
260
257
261
258
req , err := http .NewRequestWithContext (ctx , "GET" , file .DownloadUrl , nil )
262
259
if err != nil {
@@ -286,7 +283,7 @@ func (s *packagesSyncer) downloadFile(ctx context.Context, pkgName string, file
286
283
// deleteUnneededLocalPackages deletes local packages that are not
287
284
// needed anymore. This is done by comparing the local package state
288
285
// with the server's package state.
289
- func (s * packagesSyncer ) deleteUnneededLocalPackages () error {
286
+ func (s * packagesSyncer ) deleteUnneededLocalPackages (ctx context. Context ) error {
290
287
// Read the list of packages we have locally.
291
288
localPackages , err := s .localState .Packages ()
292
289
if err != nil {
@@ -297,7 +294,7 @@ func (s *packagesSyncer) deleteUnneededLocalPackages() error {
297
294
for _ , localPkg := range localPackages {
298
295
// Do we have a package that is not offered?
299
296
if _ , offered := s .available .Packages [localPkg ]; ! offered {
300
- s .logger .Debugf ("Package %s is no longer needed, deleting." , localPkg )
297
+ s .logger .Debugf (ctx , "Package %s is no longer needed, deleting." , localPkg )
301
298
err := s .localState .DeletePackage (localPkg )
302
299
if err != nil {
303
300
lastErr = err
@@ -318,16 +315,16 @@ func (s *packagesSyncer) deleteUnneededLocalPackages() error {
318
315
// reportStatuses saves the last reported statuses to provider and client state.
319
316
// If sendImmediately is true, the statuses are scheduled to be
320
317
// sent to the server.
321
- func (s * packagesSyncer ) reportStatuses (sendImmediately bool ) error {
318
+ func (s * packagesSyncer ) reportStatuses (ctx context. Context , sendImmediately bool ) error {
322
319
// Save it in the user-supplied state provider.
323
320
if err := s .localState .SetLastReportedStatuses (s .statuses ); err != nil {
324
- s .logger .Errorf ("Cannot save last reported statuses: %v" , err )
321
+ s .logger .Errorf (ctx , "Cannot save last reported statuses: %v" , err )
325
322
return err
326
323
}
327
324
328
325
// Also save it in our internal state (will be needed if the Server asks for it).
329
326
if err := s .clientSyncedState .SetPackageStatuses (s .statuses ); err != nil {
330
- s .logger .Errorf ("Cannot save client state: %v" , err )
327
+ s .logger .Errorf (ctx , "Cannot save client state: %v" , err )
331
328
return err
332
329
}
333
330
s .sender .NextMessage ().Update (
0 commit comments