Skip to content

Commit

Permalink
fixes ticker error when all configs render once
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhang93 committed May 10, 2024
1 parent 5ea1175 commit 4f631ff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ func (p *Process) initTemplate(sc *sinkExecConfig) error {
}

func (p *Process) startRenderLoop(ctx context.Context, cfg sinkExecConfig) error {
ticker := time.NewTicker(cfg.refreshInterval)
tick := ticker.C
defer ticker.Stop()

p.Logger.Info("starting refresh loop", slog.String("templ", cfg.name))
sink := render.Sink{
Expand All @@ -198,15 +195,21 @@ func (p *Process) startRenderLoop(ctx context.Context, cfg sinkExecConfig) error
}
}

defer cfg.parsed.CloseActions()

if cfg.renderOnce {
if err := p.TickFunc(ctx, &sink, execer, cfg.staticData); err != nil && !errors.Is(err, render.ContentsIdentical) {
p.Logger.Error("RenderAndExec error", slog.String("error", err.Error()), slog.String("loop", cfg.name), slog.Bool("once", true))
return err
}
p.Logger.Info("refresh complete", slog.Bool("once", true), slog.String("templ", cfg.name))
return nil
}

ticker := time.NewTicker(cfg.refreshInterval)
tick := ticker.C
defer ticker.Stop()

defer cfg.parsed.CloseActions()

consecutiveFailures := 0
for consecutiveFailures < p.maxConsecFailures {
select {
Expand Down
30 changes: 30 additions & 0 deletions internal/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,36 @@ func Test_renderLoop(t *testing.T) {
return
}
})

t.Run("all configs render once", func(t *testing.T) {
tmp := t.TempDir()
p := Process{
Logger: newLogger(),
TickFunc: RenderAndExec,
configs: []sinkExecConfig{{
sinkConfig: sinkConfig{
name: "test-render",
dest: tmp + "/test.render",
raw: "hello foo",
renderOnce: true,
},
execConfig: nil,
}, {
sinkConfig: sinkConfig{
name: "test2-render",
dest: tmp + "/test2.render",
raw: "hello bar",
renderOnce: true,
},
execConfig: nil,
}},
maxConsecFailures: 0,
}
if err := p.startTickLoops(context.Background()); err != nil {
t.Error(err)

}
})
}

func newLogger() *slog.Logger {
Expand Down

0 comments on commit 4f631ff

Please sign in to comment.