process load balancing: broken but in progress

This commit is contained in:
Julien Neuhart
2019-09-19 17:28:29 +02:00
parent c5429f7efa
commit 171f93662f
33 changed files with 1681 additions and 117 deletions

View File

@@ -13,7 +13,8 @@ import (
"github.com/thecodingmachine/gotenberg/internal/app/xhttp/pkg/resource"
"github.com/thecodingmachine/gotenberg/internal/pkg/conf"
"github.com/thecodingmachine/gotenberg/internal/pkg/normalize"
"github.com/thecodingmachine/gotenberg/internal/pkg/pm2"
"github.com/thecodingmachine/gotenberg/internal/pkg/prinery"
"github.com/thecodingmachine/gotenberg/internal/pkg/process"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
)
@@ -21,20 +22,31 @@ import (
// Context extends the default echo.Context.
type Context struct {
echo.Context
logger xlog.Logger
config conf.Config
processes []pm2.Process
resource resource.Resource
startTime time.Time
logger xlog.Logger
config conf.Config
manager process.Manager
chromePrinery *prinery.Prinery
sofficePrinery *prinery.Prinery
resource resource.Resource
startTime time.Time
}
// New creates a new Context.
func New(c echo.Context, logger xlog.Logger, config conf.Config, processes ...pm2.Process) Context {
func New(
c echo.Context,
logger xlog.Logger,
config conf.Config,
manager process.Manager,
chromePrinery *prinery.Prinery,
sofficePrinery *prinery.Prinery,
) Context {
return Context{
c,
logger,
config,
processes,
manager,
chromePrinery,
sofficePrinery,
resource.Resource{},
time.Now(),
}
@@ -77,17 +89,52 @@ func (ctx Context) Config() conf.Config {
// one of the processes is not viable.
func (ctx Context) ProcessesHealthcheck() error {
const op string = "context.Context.ProcessesHealthcheck"
for _, process := range ctx.processes {
if !process.IsViable() {
processes := ctx.manager.All()
for _, p := range processes {
if !ctx.manager.IsViable(p) {
return xerror.New(
op,
fmt.Errorf("'%s' is not viable", process.Fullname()),
fmt.Errorf("'%s' is not viable", p.ID()),
)
}
}
return nil
}
/*
MustChromePrinery returns the instance of
prinery.Prinery associated with the Context.
This prinery.Prinery handles Google Chrome
headless.
It panics if no instance of prinery.Prinery.
*/
func (ctx Context) MustChromePrinery() *prinery.Prinery {
const op string = "context.Context.MustChromePrinery"
if ctx.chromePrinery == nil {
panic(fmt.Sprintf("%s: unable to retrieve the instance of Google Chrome Headless prinery.Prinery from our custom context.Context", op))
}
return ctx.chromePrinery
}
/*
MustSofficePrinery returns the instance of
prinery.Prinery associated with the Context.
This prinery.Prinery handles LibreOffice
headless.
It panics if no instance of prinery.Prinery.
*/
func (ctx Context) MustSofficePrinery() *prinery.Prinery {
const op string = "context.Context.MustSofficePrinery"
if ctx.sofficePrinery == nil {
panic(fmt.Sprintf("%s: unable to retrieve the instance of LibreOffice Headless prinery.Prinery from our custom context.Context", op))
}
return ctx.sofficePrinery
}
// WithResource creates a resource.Resource and
// adds it to the Context.
func (ctx *Context) WithResource(directoryName string) error {

View File

@@ -76,6 +76,34 @@ func ArgKeys() []ArgKey {
}
}
/*
WaitTimeoutAndWaitDelayArg is a helper for retrieving
the sum of "waitTimeout" and "waitDelay" arguments
as float64.
It also validates them against the application
configuration.
*/
func WaitTimeoutAndWaitDelayArg(r Resource, config conf.Config) (float64, error) {
const op string = "resource.WaitTimeoutAndWaitDelayArg"
resolver := func() (float64, error) {
waitTimeout, err := WaitTimeoutArg(r, config)
if err != nil {
return waitTimeout, err
}
waitDelay, err := WaitDelayArg(r, config)
if err != nil {
return waitDelay, err
}
return waitTimeout + waitDelay, nil
}
combined, err := resolver()
if err != nil {
return combined, xerror.New(op, err)
}
return combined, nil
}
/*
WaitTimeoutArg is a helper for retrieving
the "waitTimeout" argument as float64.