Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTORING] Add routines #32

Open
slashformotion opened this issue Dec 13, 2022 · 0 comments
Open

[REFACTORING] Add routines #32

slashformotion opened this issue Dec 13, 2022 · 0 comments

Comments

@slashformotion
Copy link
Member

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)
        }
      }
    },
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant