Skip to content

Commit

Permalink
fix(worker): flush graylog hook logs when run or register ends (#5768)
Browse files Browse the repository at this point in the history
Signed-off-by: richardlt <[email protected]>
  • Loading branch information
richardlt authored Mar 17, 2021
1 parent f626cff commit ab7b0b3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
14 changes: 10 additions & 4 deletions engine/worker/cmd_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"

"github.com/ovh/cds/engine/worker/internal"
cdslog "github.com/ovh/cds/sdk/log"
"github.com/rockbears/log"
"github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)
Expand All @@ -23,13 +25,17 @@ func cmdRegister() *cobra.Command {
func cmdRegisterRun() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
var w = new(internal.CurrentWorker)

ctx := context.Background()

initFromFlags(cmd, w)
defer cdslog.Flush(ctx, logrus.StandardLogger())

if err := w.Register(context.Background()); err != nil {
log.Error(context.TODO(), "Unable to register worker %v", err)
if err := w.Register(ctx); err != nil {
log.Error(ctx, "Unable to register worker %v", err)
}
if err := w.Unregister(context.Background()); err != nil {
log.Error(context.TODO(), "Unable to unregister worker %v", err)
if err := w.Unregister(ctx); err != nil {
log.Error(ctx, "Unable to unregister worker %v", err)
}
}
}
7 changes: 4 additions & 3 deletions engine/worker/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/rockbears/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/ovh/cds/engine/worker/internal"
Expand All @@ -30,11 +31,12 @@ func runCmd() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
var w = new(internal.CurrentWorker)

//Initialize context
ctx := context.Background()
// Initialize context
ctx, cancel := context.WithCancel(context.Background())

// Setup workerfrom commandline flags or env variables
initFromFlags(cmd, w)
defer cdslog.Flush(ctx, logrus.StandardLogger())

// Get the booked job ID
bookedWJobID := FlagInt64(cmd, flagBookedWorkflowJobID)
Expand All @@ -43,7 +45,6 @@ func runCmd() func(cmd *cobra.Command, args []string) {
sdk.Exit("flag --booked-workflow-job-id is mandatory")
}

ctx, cancel := context.WithCancel(ctx)
// Gracefully shutdown connections
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
Expand Down
3 changes: 2 additions & 1 deletion engine/worker/internal/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"context"
"errors"
"os"

"github.com/rockbears/log"

Expand All @@ -20,7 +21,7 @@ func (w *CurrentWorker) Register(ctx context.Context) error {
return errR
}

log.Debug(ctx, "Checking %d requirements", len(requirements))
log.Debug(ctx, "Checking %d requirements for current PATH: %s", len(requirements), os.Getenv("PATH"))
form.BinaryCapabilities = LoopPath(w, requirements)
form.Version = sdk.VERSION
form.OS = sdk.GOOS
Expand Down
12 changes: 12 additions & 0 deletions sdk/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,15 @@ func ReplaceAllHooks(ctx context.Context, l *logrus.Logger, graylogcfg *hook.Con
l.AddHook(hook)
return nil
}

// For given logrus logger, try to flush hooks
func Flush(ctx context.Context, l *logrus.Logger) {
for _, hs := range logrus.StandardLogger().Hooks {
for _, h := range hs {
if graylogHook, ok := h.(*hook.Hook); ok {
log.Info(ctx, "Draining logs...")
graylogHook.Flush()
}
}
}
}

0 comments on commit ab7b0b3

Please sign in to comment.