adding PM2 processes details as response of ping endpoint if LOG_LEVEL=DEBUG

This commit is contained in:
Julien Neuhart
2019-08-20 15:23:02 +02:00
parent fc27f6b3f0
commit 95c6ecb724
4 changed files with 131 additions and 8 deletions

View File

@@ -8,8 +8,10 @@ import (
"github.com/labstack/echo/v4"
"github.com/thecodingmachine/gotenberg/internal/app/xhttp/pkg/context"
"github.com/thecodingmachine/gotenberg/internal/app/xhttp/pkg/resource"
"github.com/thecodingmachine/gotenberg/internal/pkg/pm2"
"github.com/thecodingmachine/gotenberg/internal/pkg/printer"
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
"github.com/thecodingmachine/gotenberg/internal/pkg/xrand"
"github.com/thecodingmachine/gotenberg/internal/pkg/xtime"
)
@@ -28,11 +30,24 @@ const (
func pingHandler(c echo.Context) error {
const op string = "xhttp.pingHandler"
ctx := context.MustCastFromEchoContext(c)
ctx.XLogger().DebugOp(op, "handling ping request...")
if err := ctx.ProcessesHealthcheck(); err != nil {
logger := ctx.XLogger()
logger.DebugOp(op, "handling ping request...")
resolver := func() error {
if err := ctx.ProcessesHealthcheck(); err != nil {
return err
}
if logger.Level() != xlog.DebugLevel {
return nil
}
list, err := pm2.List()
if err != nil {
return err
}
return ctx.JSON(http.StatusOK, list)
}
if err := resolver(); err != nil {
return xerror.New(op, err)
}
// TODO return processes info
return nil
}

View File

@@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/labstack/echo/v4"
@@ -15,7 +16,26 @@ import (
"github.com/thecodingmachine/gotenberg/test"
)
// TODO test ping handler
func TestPingHandler(t *testing.T) {
// should return 200.
config := conf.DefaultConfig()
srv := New(config)
srv = New(config)
req := httptest.NewRequest(http.MethodGet, pingEndpoint, nil)
test.AssertStatusCode(t, http.StatusOK, srv, req)
// should returns a JSON as
// LOG_LEVEL is "DEBUG".
os.Setenv(conf.LogLevelEnvVar, "DEBUG")
config, err := conf.FromEnv()
assert.Nil(t, err)
srv = New(config)
req = httptest.NewRequest(http.MethodGet, pingEndpoint, nil)
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, req)
assert.Equal(t, "application/json; charset=UTF-8", rec.Header().Get("Content-type"))
assert.Equal(t, http.StatusOK, rec.Code)
os.Unsetenv(conf.LogLevelEnvVar)
}
func TestMergeHandler(t *testing.T) {
config := conf.DefaultConfig()