diff --git a/engine/worker/cmd_register.go b/engine/worker/cmd_register.go index 3a796bcd16..cab7d66df7 100644 --- a/engine/worker/cmd_register.go +++ b/engine/worker/cmd_register.go @@ -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" ) @@ -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) } } } diff --git a/engine/worker/cmd_run.go b/engine/worker/cmd_run.go index cb4c20ff29..fac3b721cf 100644 --- a/engine/worker/cmd_run.go +++ b/engine/worker/cmd_run.go @@ -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" @@ -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) @@ -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) diff --git a/engine/worker/internal/register.go b/engine/worker/internal/register.go index d170ef4c67..9acf00661b 100644 --- a/engine/worker/internal/register.go +++ b/engine/worker/internal/register.go @@ -3,6 +3,7 @@ package internal import ( "context" "errors" + "os" "github.com/rockbears/log" @@ -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 diff --git a/sdk/log/log.go b/sdk/log/log.go index 5581262522..b45dd70ac0 100644 --- a/sdk/log/log.go +++ b/sdk/log/log.go @@ -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() + } + } + } +}