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

Log more when resolving Python #4185

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 9 additions & 9 deletions pkg/plugin/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os/exec"
"strings"
"sync"

stashExec "github.com/stashapp/stash/pkg/exec"
Expand Down Expand Up @@ -45,21 +46,17 @@ func (t *rawPluginTask) Start() error {
var cmd *exec.Cmd
if python.IsPythonCommand(command[0]) {
pythonPath := t.serverConfig.GetPythonPath()
var p *python.Python
if pythonPath != "" {
p = python.New(pythonPath)
} else {
p, _ = python.Resolve()
}
p, err := python.Resolve(pythonPath)

if p != nil {
if err != nil {
logger.Warnf("%s", err)
} else {
cmd = p.Command(context.TODO(), command[1:])
}

// if could not find python, just use the command args as-is
}

if cmd == nil {
// if could not find python, just use the command args as-is
cmd = stashExec.Command(command[0], command[1:]...)
}

Expand Down Expand Up @@ -99,6 +96,8 @@ func (t *rawPluginTask) Start() error {
go t.handlePluginStderr(t.plugin.Name, stderr)
t.cmd = cmd

logger.Infof("Plugin %s started: %s", t.plugin.Name, strings.Join(cmd.Args, " "))

// send the stdout to the plugin output
go func() {
defer t.waitGroup.Done()
Expand All @@ -113,6 +112,7 @@ func (t *rawPluginTask) Start() error {
errStr := err.Error()
output.Error = &errStr
}
logger.Infof("Plugin %s finished", t.plugin.Name)

t.result = &output
}()
Expand Down
28 changes: 22 additions & 6 deletions pkg/python/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package python

import (
"context"
"fmt"
"os/exec"

stashExec "github.com/stashapp/stash/pkg/exec"
"github.com/stashapp/stash/pkg/fsutil"
"github.com/stashapp/stash/pkg/logger"
)

type Python string
Expand All @@ -22,19 +25,32 @@ func New(path string) *Python {
// Resolve tries to find the python executable in the system.
// It first checks for python3, then python.
// Returns nil and an exec.ErrNotFound error if not found.
func Resolve() (*Python, error) {
_, err := exec.LookPath("python3")
func Resolve(configuredPythonPath string) (*Python, error) {
if configuredPythonPath != "" {
isFile, err := fsutil.FileExists(configuredPythonPath)
switch {
case err == nil && isFile:
logger.Tracef("using configured python path: %s", configuredPythonPath)
return New(configuredPythonPath), nil
case err == nil && !isFile:
logger.Warnf("configured python path is not a file: %s", configuredPythonPath)
case err != nil:
logger.Warnf("unable to use configured python path: %s", err)
}
}

python3, err := exec.LookPath("python3")

if err != nil {
_, err = exec.LookPath("python")
python, err := exec.LookPath("python")
if err != nil {
return nil, err
return nil, fmt.Errorf("python executable not in PATH: %s", err)
}
ret := Python("python")
ret := Python(python)
return &ret, nil
}

ret := Python("python3")
ret := Python(python3)
return &ret, nil
}

Expand Down
20 changes: 8 additions & 12 deletions pkg/scraper/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,17 @@ func (s *scriptScraper) runScraperScript(ctx context.Context, inString string, o
var cmd *exec.Cmd
if python.IsPythonCommand(command[0]) {
pythonPath := s.globalConfig.GetPythonPath()
var p *python.Python
if pythonPath != "" {
p = python.New(pythonPath)
} else {
p, _ = python.Resolve()
}
p, err := python.Resolve(pythonPath)

if p != nil {
cmd = p.Command(ctx, command[1:])
if err != nil {
logger.Warnf("%s", err)
} else {
cmd = p.Command(context.TODO(), command[1:])
}

// if could not find python, just use the command args as-is
}

if cmd == nil {
// if could not find python, just use the command args as-is
cmd = stashExec.Command(command[0], command[1:]...)
}

Expand Down Expand Up @@ -88,7 +84,7 @@ func (s *scriptScraper) runScraperScript(ctx context.Context, inString string, o

go handleScraperStderr(s.config.Name, stderr)

logger.Debugf("Scraper script <%s> started", strings.Join(cmd.Args, " "))
logger.Infof("Scraper script <%s> started", strings.Join(cmd.Args, " "))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the level or logging that the scraper script is running? This was mostly for the support cases were we get "ran the scraper, nothing in the log" and I think it'd be helpful to at least see that Stash ran the scraper successfully even if it didn't produce correct output. We always ask users to turn their logging level to Debug and then there's usually some output 🙂


// TODO - add a timeout here
// Make a copy of stdout here. This allows us to decode it twice.
Expand Down Expand Up @@ -116,7 +112,7 @@ func (s *scriptScraper) runScraperScript(ctx context.Context, inString string, o
}

err = cmd.Wait()
logger.Debugf("Scraper script finished")
logger.Infof("Scraper script finished")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.


if err != nil {
return fmt.Errorf("%w: %v", ErrScraperScript, err)
Expand Down
Loading