Skip to content

Commit c19556a

Browse files
schmikeidpaasman00
authored andcommitted
use defer func to always try to remove the file
1 parent 8a37740 commit c19556a

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

updater/internal/updater/updater.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ func (u *Updater) generateLinuxServiceFiles() error {
152152

153153
// Update performs the update of the collector binary
154154
func (u *Updater) Update() error {
155+
// Ensure we clean up the package status artifact after any update state
156+
defer u.removePackageStatusArtifact()
157+
155158
// Generate service files before stopping the service. If
156159
// this fails, the collector will still be running.
157160
if runtime.GOOS == "linux" {
@@ -229,12 +232,7 @@ func (u *Updater) Update() error {
229232
return fmt.Errorf("failed while monitoring for success: %w", err)
230233
}
231234

232-
// Successful update
233235
u.logger.Info("Update Complete")
234-
// safely remove the package status artifact now that we've successfully updated
235-
if removeErr := os.Remove(packagestate.DefaultFileName); removeErr != nil {
236-
u.logger.Warn("Failed to remove package status artifact after successful update", zap.Error(removeErr))
237-
}
238236
return nil
239237
}
240238

@@ -245,3 +243,14 @@ func (u *Updater) removeTmpDir() {
245243
u.logger.Error("failed to remove temporary directory", zap.Error(err))
246244
}
247245
}
246+
247+
// removePackageStatusArtifact removes the default package status artifact.
248+
// if left on disk then the collectors will get confused about state and may think they've already updated
249+
func (u *Updater) removePackageStatusArtifact() {
250+
err := os.Remove(packagestate.DefaultFileName)
251+
if err != nil {
252+
u.logger.Error("failed to remove package status artifact", zap.Error(err))
253+
return
254+
}
255+
u.logger.Debug("package status artifact removed")
256+
}

0 commit comments

Comments
 (0)