Skip to content

Commit

Permalink
[RSDK-2496] Directly refresh webservice subtype services before modul…
Browse files Browse the repository at this point in the history
…ar resource loading (#2133)
  • Loading branch information
Otterverse authored Mar 31, 2023
1 parent 7e2b8fa commit 37871c7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
4 changes: 3 additions & 1 deletion examples/customresources/models/mybase/mybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (base *MyBase) Reconfigure(cfg config.Component, deps registry.Dependencies
if base.left == nil || base.right == nil {
return errors.Errorf(`mybase %q needs both "motorL" and "motorR"`, cfg.Name)
}
return nil

// Good practice to stop motors, but also this effectively tests https://viam.atlassian.net/browse/RSDK-2496
return multierr.Combine(base.left.Stop(context.Background(), nil), base.right.Stop(context.Background(), nil))
}

type MyBaseConfig struct {
Expand Down
9 changes: 9 additions & 0 deletions module/modmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ func (mgr *Manager) AddResource(ctx context.Context, cfg config.Component, deps
if err != nil {
return nil, err
}

svc, ok := mgr.r.(robot.Refresher)
if !ok {
return nil, errors.New("robot can't refresh resources")
}
if err = svc.Refresh(ctx); err != nil {
return nil, err
}

_, err = module.client.AddResource(ctx, &pb.AddResourceRequest{Config: cfgProto, Dependencies: deps})
if err != nil {
return nil, err
Expand Down
8 changes: 6 additions & 2 deletions robot/impl/local_robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,13 @@ func (r *localRobot) updateDefaultServices(ctx context.Context) {
}
}

// Refresh does nothing for now.
// Refresh causes the web service to reload it's subtype service maps from the actual robot resources.
func (r *localRobot) Refresh(ctx context.Context) error {
return nil
svc, err := r.webService()
if err != nil {
return err
}
return svc.RefreshResources()
}

// FrameSystemConfig returns the info of each individual part that makes up a robot's frame system.
Expand Down
16 changes: 7 additions & 9 deletions robot/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ type Service interface {
// StartModule starts the module server socket.
StartModule(context.Context) error

// RefreshResources reloads the grpc-service subtypes with current robot Resources
RefreshResources() error

// Returns the address and port the web service listens on.
Address() string

Expand Down Expand Up @@ -391,7 +394,7 @@ func (svc *webService) StartModule(ctx context.Context) error {
if err := svc.modServer.RegisterServiceServer(ctx, &pb.RobotService_ServiceDesc, grpcserver.New(svc.r)); err != nil {
return err
}
if err := svc.initResources(); err != nil {
if err := svc.RefreshResources(); err != nil {
return err
}
if err := svc.initSubtypeServices(ctx, true); err != nil {
Expand Down Expand Up @@ -732,7 +735,7 @@ func (svc *webService) runWeb(ctx context.Context, options weboptions.Options) (
return err
}

if err := svc.initResources(); err != nil {
if err := svc.RefreshResources(); err != nil {
return err
}

Expand Down Expand Up @@ -1000,21 +1003,16 @@ func (svc *webService) initAuthHandlers(listenerTCPAddr *net.TCPAddr, options we
}

// Populate subtype services with robot resources.
func (svc *webService) initResources() error {
func (svc *webService) RefreshResources() error {
resources := make(map[resource.Name]interface{})
for _, name := range svc.r.ResourceNames() {
resource, err := svc.r.ResourceByName(name)
if err != nil {
continue
}

resources[name] = resource
}
if err := svc.updateResources(resources); err != nil {
return err
}

return nil
return svc.updateResources(resources)
}

// Register every subtype resource grpc service here.
Expand Down

0 comments on commit 37871c7

Please sign in to comment.