1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-31 01:24:31 +00:00

Added stop function for errorKernel

This commit is contained in:
postmannen 2021-08-04 08:35:35 +02:00
parent 90d43bda7c
commit 95e2984c86
2 changed files with 18 additions and 8 deletions

View file

@ -23,12 +23,19 @@ type errorKernel struct {
// errorCh is used to report errors from a process
errorCh chan errProcess
ctx context.Context
cancel context.CancelFunc
}
// newErrorKernel will initialize and return a new error kernel
func newErrorKernel() *errorKernel {
func newErrorKernel(ctx context.Context) *errorKernel {
ctxC, cancel := context.WithCancel(ctx)
return &errorKernel{
errorCh: make(chan errProcess, 2),
ctx: ctxC,
cancel: cancel,
}
}
@ -42,7 +49,7 @@ func newErrorKernel() *errorKernel {
// process if it should continue or not based not based on how severe
// the error where. This should be right after sending the error
// sending in the process.
func (e *errorKernel) start(ctx context.Context, newMessagesCh chan<- []subjectAndMessage) error {
func (e *errorKernel) start(newMessagesCh chan<- []subjectAndMessage) error {
// NOTE: For now it will just print the error messages to the
// console.
@ -50,7 +57,7 @@ func (e *errorKernel) start(ctx context.Context, newMessagesCh chan<- []subjectA
var er errProcess
select {
case er = <-e.errorCh:
case <-ctx.Done():
case <-e.ctx.Done():
return fmt.Errorf("info: stopping errorKernel")
}
@ -89,7 +96,7 @@ func (e *errorKernel) start(ctx context.Context, newMessagesCh chan<- []subjectA
select {
case er.errorActionCh <- errActionContinue:
case <-ctx.Done():
case <-e.ctx.Done():
log.Printf("info: errorKernel: got ctx.Done, will stop waiting for errAction\n")
return
}
@ -97,6 +104,10 @@ func (e *errorKernel) start(ctx context.Context, newMessagesCh chan<- []subjectA
}
}
func (e *errorKernel) stop() {
e.cancel()
}
type errorAction int
const (

View file

@ -247,16 +247,15 @@ func (s *server) Start() {
// Start the error kernel that will do all the error handling
// that is not done within a process.
{
s.errorKernel = newErrorKernel()
ctx, cancel := context.WithCancel(s.ctx)
s.errorKernel = newErrorKernel(s.ctx)
go func() {
err := s.errorKernel.start(ctx, s.toRingbufferCh)
err := s.errorKernel.start(s.toRingbufferCh)
if err != nil {
log.Printf("%v\n", err)
}
}()
defer cancel()
defer s.errorKernel.stop()
}
// Start collecting the metrics