removeall to remove non-empty temp dirs

This commit is contained in:
Michael Yang 2024-08-05 00:02:47 -07:00
parent 8b920f35a4
commit ed6c8bfe57

View file

@ -67,37 +67,44 @@ func PayloadsDir() (string, error) {
// Best effort to clean up prior tmpdirs // Best effort to clean up prior tmpdirs
func cleanupTmpDirs() { func cleanupTmpDirs() {
dirs, err := filepath.Glob(filepath.Join(os.TempDir(), "ollama*")) matches, err := filepath.Glob(filepath.Join(os.TempDir(), "ollama*", "ollama.pid"))
if err != nil { if err != nil {
return return
} }
for _, d := range dirs {
info, err := os.Stat(d) for _, match := range matches {
if err != nil || !info.IsDir() { raw, err := os.ReadFile(match)
if errors.Is(err, os.ErrNotExist) {
slog.Debug("not a ollama runtime directory, skipping", "path", match)
continue continue
} } else if err != nil {
raw, err := os.ReadFile(filepath.Join(d, "ollama.pid")) slog.Warn("could not read ollama.pid, skipping", "path", match, "error", err)
if err != nil {
slog.Warn("failed to read ollama.pid", "path", d, "error", err)
// No pid, ignore this tmpdir
continue continue
} }
pid, err := strconv.Atoi(string(raw)) pid, err := strconv.Atoi(string(raw))
if err != nil { if err != nil {
slog.Warn("failed to parse pid", "path", d, "error", err) slog.Warn("invalid pid, skipping", "path", match, "error", err)
continue continue
} }
proc, err := os.FindProcess(pid) p, err := os.FindProcess(pid)
if err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) { if err == nil && !errors.Is(p.Signal(syscall.Signal(0)), os.ErrProcessDone) {
slog.Warn("found running ollama", "pid", pid, "path", d) slog.Warn("process still running, skipping", "pid", pid, "path", match)
// Another running ollama, ignore this tmpdir
continue continue
} }
if err := os.Remove(d); err != nil { if err := os.Remove(match); err != nil {
slog.Warn("unable to cleanup stale tmpdir", "path", d, "error", err) slog.Warn("could not cleanup stale pidfile", "path", match, "error", err)
}
runners := filepath.Join(filepath.Dir(match), "runners")
if err := os.RemoveAll(runners); err != nil {
slog.Warn("could not cleanup stale runners", "path", runners, "error", err)
}
if err := os.Remove(filepath.Dir(match)); err != nil {
slog.Warn("could not cleanup stale tmpdir", "path", filepath.Dir(match), "error", err)
} }
} }
} }