@@ -29,6 +29,9 @@ const LockAcquireAttempts = 300
29
29
// LockStaleTime is the time after which the lock is considered stale
30
30
const LockStaleTime = 15 * time .Minute
31
31
32
+ // StaleEnvironmentTime is the time after which the virtual environment is considered stale
33
+ const StaleEnvironmentTime = 14 * 24 * time .Hour
34
+
32
35
// getFileHash calculates the SHA256 hash of the file
33
36
func getFileHash (filename string ) (string , error ) {
34
37
// Check that the file exists
@@ -361,3 +364,48 @@ func getRequirementsFileForScript(scriptPath string, requirementsOverride string
361
364
}
362
365
return "" , nil
363
366
}
367
+
368
+ // clearStaleEnvs removes stale virtual environments
369
+ func clearStaleEnvs () error {
370
+ homeDir , err := os .UserHomeDir ()
371
+ if err != nil {
372
+ return err
373
+ }
374
+
375
+ envsDir := path .Join (homeDir , EnvironmentsDir )
376
+
377
+ entries , err := os .ReadDir (envsDir )
378
+ if err != nil {
379
+ return err
380
+ }
381
+
382
+ for _ , entry := range entries {
383
+ if entry .IsDir () {
384
+ info , err := entry .Info ()
385
+ if err != nil {
386
+ if flagDebug {
387
+ loggerErr .Println (err )
388
+ }
389
+ continue
390
+ }
391
+ if time .Since (info .ModTime ()) > StaleEnvironmentTime {
392
+ staleEnvAbsPath := path .Join (envsDir , entry .Name ())
393
+ if ! isEnvLocked (staleEnvAbsPath ) {
394
+ _ , err := findProcessWithPrefix (staleEnvAbsPath )
395
+ if err == ErrNoProcessFound {
396
+ if flagDebug {
397
+ loggerErr .Printf ("Removing stale virtual environment %s...\n " , staleEnvAbsPath )
398
+ }
399
+ err = removeDir (staleEnvAbsPath )
400
+ if err != nil {
401
+ if flagDebug {
402
+ loggerErr .Println (err )
403
+ }
404
+ }
405
+ }
406
+ }
407
+ }
408
+ }
409
+ }
410
+ return err
411
+ }
0 commit comments