Skip to content

[REFACTORING] Add routines #32

@slashformotion

Description

@slashformotion

Badaas needs some to run some code periodically, such as the session cache refresh function.

I propose to create an interface

type Routine interface {
    // Start the routine
    Start() error
    // Stop the routines
   Stop() error
    // Return the name of the routine. 
    Name() string
}

since we use https://github.com/uber-go/fx it should be easy to create a lifecycle hook that manage collected routines.

func StartRoutine(lc fx.Lifecycle, routines []Routine)  {
  
  lc.Append(fx.Hook{
    OnStart: func(ctx context.Context) error {
      for routine := range routines {
        err := routine.Start()
        if err != nil {
          return fmt.Errorf("en error happened while starting routine %q. error=%w", routine.Name(), err)
        }
      }
      return nil
    },
    OnStop: func(ctx context.Context) error {
      for routine := range routines {
        err := routine.Stop()
        if err != nil {
          return fmt.Errorf("en error happened while stopping routine %q. error=%w", routine.Name(), err)
        }
      }
    },
  })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions