Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
dyncfg: send reset on reload (#1374)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Oct 17, 2023
1 parent fedc97c commit e9ecc29
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ func serve(a *Agent) {
var wg sync.WaitGroup

var exit bool
var reload bool

for {
ctx, cancel := context.WithCancel(context.Background())
ctx = context.WithValue(ctx, "reload", reload)

wg.Add(1)
go func() { defer wg.Done(); a.run(ctx) }()
Expand All @@ -115,7 +117,7 @@ func serve(a *Agent) {
cancel()

func() {
timeout := time.Second * 15
timeout := time.Second * 10
t := time.NewTimer(timeout)
defer t.Stop()
done := make(chan struct{})
Expand All @@ -134,6 +136,7 @@ func serve(a *Agent) {
os.Exit(0)
}

reload = true
time.Sleep(time.Second)
}
}
Expand Down
1 change: 1 addition & 0 deletions agent/discovery/dyncfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {

type NetdataDyncfgAPI interface {
DynCfgEnable(string) error
DynCfgReset() error
DyncCfgRegisterModule(string) error
DynCfgRegisterJob(_, _, _ string) error
DynCfgReportJobStatus(_, _, _, _ string) error
Expand Down
4 changes: 4 additions & 0 deletions agent/discovery/dyncfg/dyncfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (d *Discovery) Run(ctx context.Context, in chan<- []*confgroup.Group) {

d.in = in

if reload, ok := ctx.Value("reload").(bool); ok && reload {
_ = d.API.DynCfgReset()
}

_ = d.API.DynCfgEnable(d.Plugin)

for k := range d.Modules {
Expand Down
4 changes: 4 additions & 0 deletions agent/discovery/dyncfg/dyncfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func (m *mockApi) DynCfgEnable(string) error {
return nil
}

func (m *mockApi) DynCfgReset() error {
return nil
}

func (m *mockApi) DyncCfgRegisterModule(string) error {
m.callsDyncCfgRegisterModule++
return nil
Expand Down
3 changes: 2 additions & 1 deletion agent/functions/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ func (m *Manager) Run(ctx context.Context) {
return
}

go func() { <-ctx.Done(); r.Cancel(); _ = r.Close() }()
go func() { <-ctx.Done(); r.Cancel() }()

wg.Add(1)
go func() { defer wg.Done(); m.run(r) }()

wg.Wait()
_ = r.Close()
}

<-ctx.Done()
Expand Down
5 changes: 5 additions & 0 deletions agent/netdataapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ func (a *API) DynCfgEnable(pluginName string) error {
return err
}

func (a *API) DynCfgReset() error {
_, err := a.Write([]byte("DYNCFG_RESET\n"))
return err
}

func (a *API) DyncCfgRegisterModule(moduleName string) error {
_, err := fmt.Fprintf(a, "DYNCFG_REGISTER_MODULE '%s' job_array\n\n", moduleName)
return err
Expand Down
13 changes: 13 additions & 0 deletions agent/netdataapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ func TestAPI_DynCfgEnable(t *testing.T) {
)
}

func TestAPI_DynCfgReset(t *testing.T) {
buf := &bytes.Buffer{}
a := API{Writer: buf}

_ = a.DynCfgReset()

assert.Equal(
t,
"DYNCFG_RESET\n",
buf.String(),
)
}

func TestAPI_DyncCfgRegisterModule(t *testing.T) {
buf := &bytes.Buffer{}
a := API{Writer: buf}
Expand Down

0 comments on commit e9ecc29

Please sign in to comment.