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

[RSDK-2496] Directly refresh webservice subtype services before modular resource loading #2133

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment on lines +770 to 771
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I not be using this function? It felt like it fit in the paradigm, and my thinking was if MORE stuff needs to be refreshed here later, it can be added.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cheukt I think git said you were responsible for this line/func, so wanted to double check if you think this PR is the correct way to handle this or not. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense here

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