MCPcopy Index your code
hub / github.com/coder/coder / Cleanup

Method Cleanup

provisionersdk/tfpath/tfpath.go:187–212  ·  view source on GitHub ↗

Cleanup removes the work directory and all of its contents.

(ctx context.Context, logger slog.Logger, fs afero.Fs)

Source from the content-addressed store, hash-verified

185
186// Cleanup removes the work directory and all of its contents.
187func (l Layout) Cleanup(ctx context.Context, logger slog.Logger, fs afero.Fs) {
188 var err error
189 path := l.WorkDirectory()
190
191 for attempt := 0; attempt < 5; attempt++ {
192 err := fs.RemoveAll(path)
193 if err != nil {
194 // On Windows, open files cannot be removed.
195 // When the provisioner daemon is shutting down,
196 // it may take a few milliseconds for processes to exit.
197 // See: https://github.com/golang/go/issues/50510
198 logger.Debug(ctx, "failed to clean work directory; trying again", slog.Error(err))
199 // TODO: Should we abort earlier if the context is done?
200 time.Sleep(250 * time.Millisecond)
201 continue
202 }
203 logger.Debug(ctx, "cleaned up work directory")
204 return
205 }
206
207 // Returning an error at this point cannot do any good. The caller cannot resolve
208 // this. There is a routine cleanup task that will remove old work directories
209 // when this fails.
210 logger.Error(ctx, "failed to clean up work directory after multiple attempts",
211 slog.F("path", path), slog.Error(err))
212}
213
214// CleanStaleSessions browses the work directory searching for stale session
215// directories. Coder provisioner is supposed to remove them once after finishing the provisioning,

Callers

nothing calls this directly

Calls 3

WorkDirectoryMethod · 0.95
RemoveAllMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected