Skip to content

Commit

Permalink
Place results in subdirs
Browse files Browse the repository at this point in the history
Subdirs are simply named by taking the prefix of the input UUID.

This is a common technique to prevent the chance of overflowing filesystem
limits on the number of files allowed in a single directory.

e.g.

Before
 - /srv/gitea/custom/public/90fa9a89-5e50-4bd4-834d-d42655e1ee8e.html
 - http://127.0.0.1:3000/assets/90fa9a89-5e50-4bd4-834d-d42655e1ee8e.html

After:
 - /srv/gitea/custom/public/bids-validator/84/b1/84b10e1c-b188-41e2-a92d-fb6ded9c6889.html
 - http://127.0.0.1:3000/assets/bids-validator/84/b1/84b10e1c-b188-41e2-a92d-fb6ded9c6889.html
  • Loading branch information
kousu committed Feb 24, 2023
1 parent 8688742 commit 5c7289b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
29 changes: 11 additions & 18 deletions bids-hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,9 @@ type job struct {
uuid string
}

// web link to the results page for this job
// see also j.resultPath()
func (j job) resultUrl() string {
url := *giteaPublicUrl
url.Path = path.Join(url.Path, fmt.Sprintf("%s.html", j.uuid))
return url.String()
}

// file path to the results page for this job
// see also j.resultUrl()
// file path to the results page for this job, relative to the output directory
func (j job) resultPath() string {
return filepath.Join(giteaPublicPath, fmt.Sprintf("%s.html", j.uuid))
return filepath.Join("bids-validator", j.uuid[:2], j.uuid[2:4], fmt.Sprintf("%s.html", j.uuid))
}

// file path to the log file for this job
Expand All @@ -300,23 +291,21 @@ func (j job) logPath() string {
// postStatus posts a commit status to Gitea
// 'state' should be one of the constants defined at the top of this module
func (j job) postStatus(ctx context.Context, state string) error {
url := *giteaApiUrl
url.Path = path.Join(url.Path, "repos", j.user, j.repo, "statuses", j.commit)
url := giteaRootUrl.JoinPath("api", "v1", "repos", j.user, j.repo, "statuses", j.commit)
targetUrl := giteaRootUrl.JoinPath("assets", j.resultPath()).String()

var description string

var description, targetUrl string
switch state {
case statePending:
description = "waiting for results"
targetUrl = ""
case stateSuccess:
description = "validation passed"
targetUrl = j.resultUrl()
case stateFailure:
description = "validation failed"
targetUrl = j.resultUrl()
case stateWarning:
description = "validation passed with warnings"
targetUrl = j.resultUrl()
case stateError:
description = "internal error"
targetUrl = ""
Expand Down Expand Up @@ -365,7 +354,11 @@ func (j job) run() (state string, _ error) {
)

// redirect stdout to the result file
stdout, err := os.OpenFile(j.resultPath(), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
err := os.MkdirAll(path.Dir(path.Join(giteaPublicPath, j.resultPath())), 0755)
if err != nil {
return stateError, err
}
stdout, err := os.OpenFile(path.Join(giteaPublicPath, j.resultPath()), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return stateError, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/neuropoly/bids-hook

go 1.18
go 1.19

0 comments on commit 5c7289b

Please sign in to comment.