Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit b327855

Browse files
authored
Merge pull request #66 from divolgin/9281
[ch9281] Leave Daemon running, render "done" in the UI
2 parents 34b2131 + 9b3e812 commit b327855

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

pkg/lifecycle/render/config/daemon.go

+42-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ type Daemon struct {
3434
UI cli.Ui
3535

3636
sync.Mutex
37-
currentStep *api.Step
38-
currentStepName string
39-
pastSteps []api.Step
37+
currentStep *api.Step
38+
currentStepName string
39+
currentStepConfirmed bool
40+
allStepsDone bool
41+
pastSteps []api.Step
4042

4143
startOnce sync.Once
4244

@@ -59,9 +61,16 @@ func (d *Daemon) PushStep(ctx context.Context, stepName string, step api.Step) {
5961
}
6062
d.currentStepName = stepName
6163
d.currentStep = &step
64+
d.currentStepConfirmed = false
6265
d.NotifyStepChanged(stepName)
6366
}
6467

68+
func (d *Daemon) AllStepsDone(ctx context.Context) {
69+
d.Lock()
70+
defer d.Unlock()
71+
d.allStepsDone = true
72+
}
73+
6574
// "this is fine"
6675
func (d *Daemon) EnsureStarted(ctx context.Context, release *api.Release) chan error {
6776
errChan := make(chan error)
@@ -161,11 +170,24 @@ func (d *Daemon) getLoadingStep(c *gin.Context) {
161170
})
162171
}
163172

173+
func (d *Daemon) getDoneStep(c *gin.Context) {
174+
c.JSON(200, map[string]interface{}{
175+
"currentStep": map[string]interface{}{
176+
"done": map[string]interface{}{},
177+
},
178+
"phase": "done",
179+
})
180+
}
181+
164182
func (d *Daemon) getCurrentStep(c *gin.Context) {
165183
if d.currentStep == nil {
166184
d.getLoadingStep(c)
167185
return
168186
}
187+
if d.allStepsDone {
188+
d.getDoneStep(c)
189+
return
190+
}
169191

170192
c.JSON(200, map[string]interface{}{
171193
"currentStep": d.currentStep,
@@ -197,9 +219,23 @@ func (d *Daemon) postConfirmMessage(c *gin.Context) {
197219
return
198220
}
199221

200-
go func() {
201-
d.MessageConfirmed <- request.StepName
202-
}()
222+
if d.allStepsDone {
223+
c.JSON(400, map[string]interface{}{
224+
"error": "no more steps",
225+
})
226+
return
227+
}
228+
229+
debug.Log("event", "confirm.step", "step", d.currentStepName)
230+
231+
// Confirmation for each step will only be read once from the channel
232+
if d.currentStepConfirmed {
233+
c.String(200, "")
234+
return
235+
}
236+
237+
d.currentStepConfirmed = true
238+
d.MessageConfirmed <- request.StepName
203239

204240
c.String(200, "")
205241
}

pkg/lifecycle/runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func ExecutorFromViper(v *viper.Viper) *StepExecutor {
7474
}
7575
}
7676
func (e *StepExecutor) WithDaemon(d *config.Daemon) *StepExecutor {
77+
e.Daemon = d
7778
e.Renderer = e.Renderer.WithDaemon(d)
7879
e.Messenger = e.Messenger.WithDaemon(d)
7980
return e
@@ -91,5 +92,5 @@ func (r *Runner) Run(ctx context.Context, release *api.Release) error {
9192
}
9293
}
9394

94-
return nil
95+
return r.Executor.End(ctx)
9596
}

pkg/lifecycle/step.go

+7
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import (
99
"github.com/replicatedcom/ship/pkg/api"
1010
"github.com/replicatedcom/ship/pkg/lifecycle/message"
1111
"github.com/replicatedcom/ship/pkg/lifecycle/render"
12+
"github.com/replicatedcom/ship/pkg/lifecycle/render/config"
1213
)
1314

1415
type StepExecutor struct {
1516
Logger log.Logger
1617
Renderer *render.Renderer
1718
Messenger message.Messenger
19+
Daemon *config.Daemon
1820
}
1921

2022
func (s *StepExecutor) Execute(ctx context.Context, release *api.Release, step *api.Step) error {
@@ -34,3 +36,8 @@ func (s *StepExecutor) Execute(ctx context.Context, release *api.Release, step *
3436

3537
return nil
3638
}
39+
40+
func (s *StepExecutor) End(ctx context.Context) error {
41+
s.Daemon.AllStepsDone(ctx)
42+
return nil
43+
}

pkg/ship/ship.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,20 @@ func (s *Ship) Execute(ctx context.Context) error {
139139
return errors.Wrap(err, "resolve specs")
140140
}
141141

142-
errChan := make(chan error)
143142
go func() {
144-
err = s.Runner.Run(ctx, release)
145-
errChan <- errors.Wrap(err, "run lifecycle")
146-
143+
err := s.Runner.Run(ctx, release)
144+
if err != nil {
145+
level.Error(s.Logger).Log("event", "shutdown", "reason", "error", "err", err)
146+
} else {
147+
level.Info(s.Logger).Log("event", "shutdown", "reason", "complete with no errors")
148+
}
147149
}()
148150

149151
signalChan := make(chan os.Signal, 1)
150152
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
151-
select {
152-
case sig := <-signalChan:
153-
level.Info(s.Logger).Log("event", "shutdown", "reason", "signal", "signal", sig)
154-
return nil
155-
case err := <-errChan:
156-
if err != nil {
157-
level.Error(s.Logger).Log("event", "shutdown", "reason", "error", "err", err)
158-
}
159-
level.Info(s.Logger).Log("event", "shutdown", "reason", "complete with no errors")
160-
return err
161-
}
153+
sig := <-signalChan
154+
level.Info(s.Logger).Log("event", "shutdown", "reason", "signal", "signal", sig)
155+
return nil
162156

163157
// todo send shipRegisterInstall mutation to pg.
164158

0 commit comments

Comments
 (0)