huge refactoring

This commit is contained in:
Julien Neuhart
2019-07-23 13:57:18 +02:00
parent f6b357691c
commit 7bfbda4490
105 changed files with 4051 additions and 2667 deletions

View File

@@ -5,45 +5,72 @@ import (
"time"
"github.com/mafredri/cdp/devtool"
"github.com/thecodingmachine/gotenberg/internal/pkg/logger"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
)
const chromeWarmupTime time.Duration = 10 * time.Second
type chrome struct {
manager *processManager
type chromeProcess struct {
logger xlog.Logger
}
// NewChrome returns a Google Chrome
// NewChromeProcess returns a Google Chrome
// headless process.
func NewChrome(logger *logger.Logger) Process {
return &chrome{
manager: &processManager{logger: logger},
func NewChromeProcess(logger xlog.Logger) Process {
return chromeProcess{
logger: logger,
}
}
func (p *chrome) Fullname() string {
func (p chromeProcess) Fullname() string {
return "Google Chrome headless"
}
func (p *chrome) Start() error {
const op string = "pm2.chrome.Start"
if err := p.manager.start(p); err != nil {
return &standarderror.Error{Op: op, Err: err}
func (p chromeProcess) Start() error {
const op string = "pm2.chromeProcess.Start"
if err := start(p.logger, p); err != nil {
return xerror.New(op, err)
}
return nil
}
func (p *chrome) Shutdown() error {
const op string = "pm2.chrome.Shutdown"
if err := p.manager.shutdown(p); err != nil {
return &standarderror.Error{Op: op, Err: err}
func (p chromeProcess) IsViable() bool {
const op string = "pm2.chromeProcess.IsViable"
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
p.logger.DebugfOp(
op,
"checking '%s' viability via endpoint '%s'",
p.Fullname(),
"http://localhost:9222/json/version",
)
v, err := devtool.New("http://localhost:9222").Version(ctx)
if err != nil {
p.logger.ErrorfOp(
op,
"'%s' is not viable as endpoint returned '%v'",
p.Fullname(),
err,
)
return false
}
p.logger.DebugfOp(
op,
"'%s' is viable as endpoint returned '%v'",
p.Fullname(),
v,
)
return true
}
func (p chromeProcess) Stop() error {
const op string = "pm2.chromeProcess.Stop"
if err := stop(p.logger, p); err != nil {
return xerror.New(op, err)
}
return nil
}
func (p *chrome) args() []string {
func (p chromeProcess) args() []string {
return []string{
"--no-sandbox",
"--headless",
@@ -62,47 +89,25 @@ func (p *chrome) args() []string {
}
}
func (p *chrome) name() string {
func (p chromeProcess) binary() string {
return "google-chrome-stable"
}
func (p *chrome) viable() bool {
const op string = "pm2.chrome.viable"
// check if Google Chrome is correctly running.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
p.manager.logger.DebugfOp(
op,
"checking liveness via debug version endpoint http://localhost:9222/json/version",
func (p chromeProcess) warmup() {
const (
op string = "pm2.chromeProcess.warmup"
warmupTime time.Duration = 10 * time.Second
)
v, err := devtool.New("http://localhost:9222").Version(ctx)
if err != nil {
p.manager.logger.DebugfOp(
op,
"debug version endpoint returned error: %v",
err,
)
return false
}
p.manager.logger.DebugfOp(
p.logger.DebugfOp(
op,
"debug version endpoint returned version info: %+v",
*v,
"waiting '%v' for allowing '%s' to warmup",
warmupTime,
p.Fullname(),
)
return true
}
func (p *chrome) warmup() {
const op string = "pm2.chrome.warmup"
p.manager.logger.DebugfOp(
op,
"allowing %v to startup",
chromeWarmupTime,
)
time.Sleep(chromeWarmupTime)
time.Sleep(warmupTime)
}
// Compile-time checks to ensure type implements desired interfaces.
var (
_ = Process(new(chrome))
_ = Process(new(chromeProcess))
)