Skip to content

Commit

Permalink
fix(triggers): Updating playwright Engine lib execution
Browse files Browse the repository at this point in the history
  • Loading branch information
xoscar committed Jul 12, 2024
1 parent 54b5dbb commit a6fc6e4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions agent/workers/trigger/playwrightengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"time"
)

var (
Expand Down Expand Up @@ -45,7 +46,7 @@ func (te *playwrightTriggerer) Trigger(ctx context.Context, triggerConfig Trigge
return response, err
}

out, err := start(opts.TraceID.String(), opts.SpanID.String(), triggerConfig.PlaywrightEngine.Target, triggerConfig.PlaywrightEngine.Method, scriptPath)
out, err := start(ctx, opts.TraceID.String(), opts.SpanID.String(), triggerConfig.PlaywrightEngine.Target, triggerConfig.PlaywrightEngine.Method, scriptPath)
os.Remove(scriptPath)
if err != nil {
return response, err
Expand Down Expand Up @@ -76,14 +77,15 @@ func validate() error {
return nil
}

func start(traceId, spanId, url, method, scriptPath string) (string, error) {
func start(ctx context.Context, traceId, spanId, url, method, scriptPath string) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}

if os.Getenv("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD") != "1" {
res, err := execCommand(
ctx,
app,
"playwright",
"install",
Expand All @@ -100,6 +102,7 @@ func start(traceId, spanId, url, method, scriptPath string) (string, error) {
}

res, err := execCommand(
ctx,
app,
"--yes",
libName,
Expand All @@ -121,17 +124,20 @@ func start(traceId, spanId, url, method, scriptPath string) (string, error) {
return res, nil
}

func execCommand(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
func execCommand(ctx context.Context, name string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, time.Minute*3) //3 minutes
defer cancel()
cmd := exec.CommandContext(ctx, name, args...)

var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()

err := cmd.Run()
if err != nil {
return fmt.Sprint(err) + ": " + stderr.String(), err
res := fmt.Sprint(err) + ": " + out.String() + stderr.String()
return res, err
}

return out.String(), nil
Expand Down

0 comments on commit a6fc6e4

Please sign in to comment.