Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print script output in case of failure #79

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ package processor
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"time"

"github.com/canonical/athena-core/pkg/common"
"github.com/canonical/athena-core/pkg/common/db"
"github.com/canonical/athena-core/pkg/config"
Expand All @@ -11,13 +20,6 @@ import (
"github.com/lileio/pubsub/v2"
"github.com/lileio/pubsub/v2/middleware/defaults"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"sync"
"time"
)

type Processor struct {
Expand Down Expand Up @@ -65,11 +67,9 @@ func RunWithTimeout(baseDir string, timeout time.Duration, command string) ([]by
defer cancel()
cmd := exec.CommandContext(ctx, "bash", "-c", command)
cmd.Dir = baseDir
//cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: task.Pgid}

output, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
//log.Warnf("Collector: %s, timed out after %f secs (cancelled)", report.Name, report.Timeout.Seconds())
return nil, nil
}
return output, err
Expand All @@ -90,16 +90,15 @@ func RunReport(report *ReportToExecute) (map[string][]byte, error) {
var err error
if report.Timeout > 0 {
ret, err = RunWithTimeout(report.BaseDir, report.Timeout, script)
if err != nil {
log.Errorf("Error occured while running script: %s", err)
return nil, err
}
} else {
ret, err = RunWithoutTimeout(report.BaseDir, script)
if err != nil {
log.Errorf("Error occured while running script: %s", err)
return nil, err
}
if err != nil {
log.Errorf("Error occurred (test) while running script: %s", err)
for _, line := range strings.Split(string(ret), "\n") {
log.Error(line)
}
return nil, err
}
output[scriptName] = ret
}
Expand Down
Loading