mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-13 10:52:15 +01:00
adds configurable debugging of startup of processes and print out some info from the chrome viable() function for debugging purposes
This commit is contained in:
@@ -7,15 +7,19 @@ import (
|
||||
"github.com/mafredri/cdp/devtool"
|
||||
)
|
||||
|
||||
const (
|
||||
warmupTime = 10 * time.Second
|
||||
)
|
||||
|
||||
type chrome struct {
|
||||
manager *processManager
|
||||
}
|
||||
|
||||
// NewChrome retruns a Google Chrome
|
||||
// headless process.
|
||||
func NewChrome() Process {
|
||||
func NewChrome(debug bool) Process {
|
||||
return &chrome{
|
||||
manager: &processManager{},
|
||||
manager: &processManager{verbose: debug},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,12 +62,20 @@ func (p *chrome) viable() bool {
|
||||
// check if Google Chrome is correctly running.
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
_, err := devtool.New("http://localhost:9222").Version(ctx)
|
||||
return err == nil
|
||||
p.manager.notifyf(`%v: checking Chrome liveness via debug version endpoint
|
||||
'http://localhost:9222/json/version'`, p.name())
|
||||
v, err := devtool.New("http://localhost:9222").Version(ctx)
|
||||
if err != nil {
|
||||
p.manager.notifyf("%v: Chrome version endpoint returned error: %v", p.name(), err)
|
||||
return false
|
||||
}
|
||||
p.manager.notifyf("%v: Chrome returned version info: %+v", p.name(), *v)
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *chrome) warmup() {
|
||||
time.Sleep(5 * time.Second)
|
||||
p.manager.notifyf("%v: allowing Chrome %v to startup", p.name(), warmupTime)
|
||||
time.Sleep(warmupTime)
|
||||
}
|
||||
|
||||
// Compile-time checks to ensure type implements desired interfaces.
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
)
|
||||
|
||||
func TestChromeStart(t *testing.T) {
|
||||
p := NewChrome()
|
||||
p := NewChrome(false)
|
||||
err := p.Start()
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestChromeShutdown(t *testing.T) {
|
||||
p := NewChrome()
|
||||
p := NewChrome(false)
|
||||
err := p.Shutdown()
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package pm2
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/notify"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -25,6 +31,7 @@ type Process interface {
|
||||
|
||||
type processManager struct {
|
||||
heuristicState int32
|
||||
verbose bool
|
||||
}
|
||||
|
||||
func (m *processManager) start(p Process) error {
|
||||
@@ -76,8 +83,43 @@ func (m *processManager) pm2(p Process, cmdName string) error {
|
||||
"pm2",
|
||||
cmdArgs...,
|
||||
)
|
||||
m.notifyf("executing command '%v'", strings.Join(cmd.Args, " "))
|
||||
if m.verbose {
|
||||
chromeStdErr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting Chrome stderr: %v", err)
|
||||
}
|
||||
chromeStdOut, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed getting Chrome stdout: %v", err)
|
||||
}
|
||||
readFromPipe := func(name string, reader io.ReadCloser) {
|
||||
r := bufio.NewReader(reader)
|
||||
defer reader.Close()
|
||||
for {
|
||||
line, _, err := r.ReadLine()
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
m.notifyf("error reading from %v for process %v", name, p.name())
|
||||
}
|
||||
break
|
||||
}
|
||||
if len(line) != 0 {
|
||||
m.notifyf("%v %v: %s", p.name(), name, string(line))
|
||||
}
|
||||
}
|
||||
}
|
||||
go readFromPipe("stdout", chromeStdOut)
|
||||
go readFromPipe("stderr", chromeStdErr)
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("%s %s with PM2: %v", cmdName, p.Fullname(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *processManager) notifyf(format string, args ...interface{}) {
|
||||
if m.verbose {
|
||||
notify.Printf(fmt.Sprintf("%v: %s", time.Now().Format(time.RFC3339), format), args...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ type unoconv struct {
|
||||
|
||||
// NewUnoconv retruns a unoconv listener
|
||||
// process.
|
||||
func NewUnoconv() Process {
|
||||
func NewUnoconv(debug bool) Process {
|
||||
return &unoconv{
|
||||
manager: &processManager{},
|
||||
manager: &processManager{verbose: debug},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
)
|
||||
|
||||
func TestUnoconvStart(t *testing.T) {
|
||||
p := NewUnoconv()
|
||||
p := NewUnoconv(false)
|
||||
err := p.Start()
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestUnoconvShutdown(t *testing.T) {
|
||||
p := NewUnoconv()
|
||||
p := NewUnoconv(false)
|
||||
err := p.Shutdown()
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user