mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add more convenience methods for start/stopping modules on router
This commit is contained in:
40
router.go
40
router.go
@@ -50,12 +50,35 @@ func (r *Router) addModule(moduleDecl config.ModuleConfig) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) removeModule(moduleId string) error {
|
func (r *Router) removeModule(moduleId string) error {
|
||||||
moduleInstance, ok := r.ModuleInstances[moduleId]
|
err := r.stopModule(moduleId)
|
||||||
if !ok {
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
delete(r.ModuleInstances, moduleId)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Router) runModule(ctx context.Context, moduleId string) error {
|
||||||
|
moduleInstance := r.getModule(moduleId)
|
||||||
|
if moduleInstance == nil {
|
||||||
|
return errors.New("module id not found")
|
||||||
|
}
|
||||||
|
r.moduleWait.Go(func() {
|
||||||
|
err := moduleInstance.Run(ctx)
|
||||||
|
if err != nil {
|
||||||
|
// TODO(jwetzell): propagate module run errors better
|
||||||
|
r.logger.Error("error encountered running module", "moduleId", moduleId, "error", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Router) stopModule(moduleId string) error {
|
||||||
|
moduleInstance := r.getModule(moduleId)
|
||||||
|
if moduleInstance == nil {
|
||||||
return errors.New("module id not found")
|
return errors.New("module id not found")
|
||||||
}
|
}
|
||||||
moduleInstance.Stop()
|
moduleInstance.Stop()
|
||||||
delete(r.ModuleInstances, moduleId)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,14 +155,9 @@ func (r *Router) Run(ctx context.Context) {
|
|||||||
r.contextCancel = cancel
|
r.contextCancel = cancel
|
||||||
contextWithRouter := context.WithValue(routerContext, route.RouterContextKey, r)
|
contextWithRouter := context.WithValue(routerContext, route.RouterContextKey, r)
|
||||||
|
|
||||||
for _, moduleInstance := range r.ModuleInstances {
|
for moduleId := range r.ModuleInstances {
|
||||||
r.moduleWait.Go(func() {
|
// TODO(jwetzell): handle module run errors
|
||||||
err := moduleInstance.Run(contextWithRouter)
|
r.runModule(contextWithRouter, moduleId)
|
||||||
if err != nil {
|
|
||||||
// TODO(jwetzell): handle module run errors better
|
|
||||||
r.logger.Error("error encountered running module", "error", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
<-r.Context.Done()
|
<-r.Context.Done()
|
||||||
r.logger.Debug("waiting for modules to exit")
|
r.logger.Debug("waiting for modules to exit")
|
||||||
|
|||||||
Reference in New Issue
Block a user