Skip to content

Commit

Permalink
Show message when preview times out
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Oct 7, 2024
1 parent 2c020ea commit aa3c83c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
18 changes: 6 additions & 12 deletions tools/web-previewer/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ type Artifact struct {
CommitSHA string
}

type FirmwareLoadError string

func (e FirmwareLoadError) Error() string {
return string(e)
}

func init() {
if err := os.MkdirAll(firmwareCachePath, 0755); err != nil {
panic(err)
Expand All @@ -67,7 +61,7 @@ func getPullRequestArtifact(ctx context.Context, id int, noCache bool) (*Artifac
pr, _, err := gh.PullRequests.Get(ctx, repositoryOwner, repositoryName, id)
if err != nil {
if isNotFoundError(err) {
return nil, FirmwareLoadError("pull request not found")
return nil, PreviewError("pull request not found")
}

return nil, fmt.Errorf("list commits: %w", err)
Expand All @@ -88,7 +82,7 @@ func getRefArtifact(ctx context.Context, refName string, noCache bool) (*Artifac
ref, _, err := gh.Git.GetRef(ctx, repositoryOwner, repositoryName, refName)
if err != nil {
if isNotFoundError(err) {
return nil, FirmwareLoadError("ref not found")
return nil, PreviewError("ref not found")
}

return nil, fmt.Errorf("get ref: %w", err)
Expand All @@ -115,7 +109,7 @@ func getSHACommitArtifact(ctx context.Context, sha string, noCache bool) (*Artif
}

if len(runs.WorkflowRuns) == 0 {
return nil, FirmwareLoadError(fmt.Sprintf("no workflow runs found for commit '%s'", sha))
return nil, PreviewError(fmt.Sprintf("no workflow runs found for commit '%s'", sha))
}

latestID = *slices.MaxFunc(runs.WorkflowRuns, func(a, b *github.WorkflowRun) int {
Expand Down Expand Up @@ -160,7 +154,7 @@ func fetchRunArtifact(ctx context.Context, workflowRunID int64, noCache bool) (s
}

if len(artifacts.Artifacts) == 0 {
return "", FirmwareLoadError("no artifacts found")
return "", PreviewError("no artifacts found")
}

found := false
Expand All @@ -174,7 +168,7 @@ func fetchRunArtifact(ctx context.Context, workflowRunID int64, noCache bool) (s
}

if !found {
return "", FirmwareLoadError("no firmware artifact found")
return "", PreviewError("no firmware artifact found")
}

ghCache.Set(key, fmt.Sprintf("%d", artifactID), 24*time.Hour)
Expand Down Expand Up @@ -221,7 +215,7 @@ func downloadArtifactFirmware(ctx context.Context, url string, noCache bool) (st
}

if len(zr.File) != 1 || !strings.HasSuffix(zr.File[0].Name, ".out") {
return "", FirmwareLoadError("invalid artifact file contents")
return "", PreviewError("invalid artifact file contents")
}

f, err := zr.File[0].Open()
Expand Down
14 changes: 12 additions & 2 deletions tools/web-previewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ var (
previewerPath string
)

type PreviewError string

func (e PreviewError) Error() string {
return string(e)
}

type PreviewJob struct {
FirmwareSpec string
Script string
Expand All @@ -39,7 +45,7 @@ type PreviewJob struct {

func main() {
flag.StringVar(&previewerPath, "previewer", "", "Path to the previewer executable")
flag.DurationVar(&previewTimeout, "preview-timeout", 20*time.Second, "Timeout for the previewer")
flag.DurationVar(&previewTimeout, "preview-timeout", 2*time.Second, "Timeout for the previewer")
flag.Parse()

addr := os.Getenv("ADDR")
Expand Down Expand Up @@ -94,7 +100,7 @@ func main() {
if err := previewHandler(r.Context(), w, job, noCache); err != nil {
log.Err(err).Msg("failed to generate preview")

var fwErr FirmwareLoadError
var fwErr PreviewError

if errors.As(err, &fwErr) {
generateErrorImage(w, string(fwErr))
Expand Down Expand Up @@ -149,6 +155,10 @@ func previewHandler(ctx context.Context, rw io.Writer, job PreviewJob, noCache b
start := time.Now()

if err := cmd.Run(); err != nil {
if prctx.Err() != nil {
return PreviewError("preview timed out")
}

return fmt.Errorf("run previewer: %w", err)
}

Expand Down

0 comments on commit aa3c83c

Please sign in to comment.