Fix config validation edge cases

This commit is contained in:
2026-07-03 18:56:35 +00:00
parent 4477b13203
commit fd835b582c
6 changed files with 222 additions and 34 deletions

View File

@@ -12,7 +12,14 @@ import (
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
const defaultWorkDir = "/tmp/notarius"
const (
defaultWorkDir = "/tmp/notarius"
maxRunDirectoryCreateAttempts = 16
)
var utcNow = func() time.Time {
return time.Now().UTC()
}
// RunDirectory represents a per-run diagnostics directory.
type RunDirectory struct {
@@ -77,18 +84,27 @@ func NewRunDirectory(workDir string, retention RetentionMode) (*RunDirectory, er
return nil, fmt.Errorf("create diagnostics work directory %q: %w", workDir, err)
}
createdAt := time.Now().UTC()
runID := fmt.Sprintf("run-%d", createdAt.UnixNano())
runPath := filepath.Join(workDir, runID)
if err := os.Mkdir(runPath, 0o755); err != nil {
return nil, fmt.Errorf("create diagnostics run directory %q: %w", runPath, err)
var lastRunPath string
for attempt := 0; attempt < maxRunDirectoryCreateAttempts; attempt++ {
createdAt := utcNow()
runID := fmt.Sprintf("run-%d", createdAt.UnixNano())
runPath := filepath.Join(workDir, runID)
lastRunPath = runPath
if err := os.Mkdir(runPath, 0o755); err != nil {
if os.IsExist(err) {
continue
}
return nil, fmt.Errorf("create diagnostics run directory %q: %w", runPath, err)
}
return &RunDirectory{
path: runPath,
retention: retention,
createdAt: createdAt,
}, nil
}
return &RunDirectory{
path: runPath,
retention: retention,
createdAt: createdAt,
}, nil
return nil, fmt.Errorf("create diagnostics run directory %q: exhausted unique run ID attempts", lastRunPath)
}
func (r *RunDirectory) Path() string {