Skip to content

Commit ab7b0b3

Browse files
authored
fix(worker): flush graylog hook logs when run or register ends (#5768)
Signed-off-by: richardlt <[email protected]>
1 parent f626cff commit ab7b0b3

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

engine/worker/cmd_register.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55

66
"github.com/ovh/cds/engine/worker/internal"
7+
cdslog "github.com/ovh/cds/sdk/log"
78
"github.com/rockbears/log"
9+
"github.com/sirupsen/logrus"
810

911
"github.com/spf13/cobra"
1012
)
@@ -23,13 +25,17 @@ func cmdRegister() *cobra.Command {
2325
func cmdRegisterRun() func(cmd *cobra.Command, args []string) {
2426
return func(cmd *cobra.Command, args []string) {
2527
var w = new(internal.CurrentWorker)
28+
29+
ctx := context.Background()
30+
2631
initFromFlags(cmd, w)
32+
defer cdslog.Flush(ctx, logrus.StandardLogger())
2733

28-
if err := w.Register(context.Background()); err != nil {
29-
log.Error(context.TODO(), "Unable to register worker %v", err)
34+
if err := w.Register(ctx); err != nil {
35+
log.Error(ctx, "Unable to register worker %v", err)
3036
}
31-
if err := w.Unregister(context.Background()); err != nil {
32-
log.Error(context.TODO(), "Unable to unregister worker %v", err)
37+
if err := w.Unregister(ctx); err != nil {
38+
log.Error(ctx, "Unable to unregister worker %v", err)
3339
}
3440
}
3541
}

engine/worker/cmd_run.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/rockbears/log"
11+
"github.com/sirupsen/logrus"
1112
"github.com/spf13/cobra"
1213

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

33-
//Initialize context
34-
ctx := context.Background()
34+
// Initialize context
35+
ctx, cancel := context.WithCancel(context.Background())
3536

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

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

46-
ctx, cancel := context.WithCancel(ctx)
4748
// Gracefully shutdown connections
4849
c := make(chan os.Signal, 1)
4950
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

engine/worker/internal/register.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package internal
33
import (
44
"context"
55
"errors"
6+
"os"
67

78
"github.com/rockbears/log"
89

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

23-
log.Debug(ctx, "Checking %d requirements", len(requirements))
24+
log.Debug(ctx, "Checking %d requirements for current PATH: %s", len(requirements), os.Getenv("PATH"))
2425
form.BinaryCapabilities = LoopPath(w, requirements)
2526
form.Version = sdk.VERSION
2627
form.OS = sdk.GOOS

sdk/log/log.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,15 @@ func ReplaceAllHooks(ctx context.Context, l *logrus.Logger, graylogcfg *hook.Con
172172
l.AddHook(hook)
173173
return nil
174174
}
175+
176+
// For given logrus logger, try to flush hooks
177+
func Flush(ctx context.Context, l *logrus.Logger) {
178+
for _, hs := range logrus.StandardLogger().Hooks {
179+
for _, h := range hs {
180+
if graylogHook, ok := h.(*hook.Hook); ok {
181+
log.Info(ctx, "Draining logs...")
182+
graylogHook.Flush()
183+
}
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)