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

DOCS-3699, DOCS-2567: Add module lifecycle and dependencies guidance #4156

Merged
merged 14 commits into from
Apr 4, 2025

Conversation

JessamyT
Copy link
Collaborator

@JessamyT JessamyT commented Mar 27, 2025

@viambot viambot added the safe to build This pull request is marked safe to build from a trusted zone label Mar 27, 2025
Copy link

netlify bot commented Mar 27, 2025

Deploy Preview for viam-docs ready!

Name Link
🔨 Latest commit c114a25
🔍 Latest deploy log https://app.netlify.com/sites/viam-docs/deploys/67ed7b5a4fb394000853347e
😎 Deploy Preview https://deploy-preview-4156--viam-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 41 (🟢 up 15 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (🟢 up 8 from production)
PWA: 60 (🔴 down 10 from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -191,7 +191,7 @@ If you need to maintain the state of your resource, see [(Optional) Create and e
{{< tabs >}}
{{% tab name="Python" %}}

If you prefer to use explicit dependencies (for example, for an optional dependency), the steps are the same as for implicit dependencies above, except that you do not need to return the dependency from the `validate_config` method and can instead return an empty list:
If you prefer to use explicit dependencies (for example, for an optional dependency), the steps are the same as for implicit dependencies, except that you do not need to return the dependency from the `validate_config` method and can instead return an empty list:
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 that saying "for example, for an optional dependency" is wrong here. A resource will still wait for all resources declared in depends_on to be available before starting.

Copy link
Member

Choose a reason for hiding this comment

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

We have two mechanisms for declaring dependencies which are essentially the same result. Implicit is more common than explicit

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What I mean by "optional" here is that if the user doesn't configure a "camera_name" at all, either in depends_on or in attributes, that the modular resource should still work. But I'm realizing the example I have here doesn't indicate that--I should make this validate_config explicit dependency example show "camera_name" as an optional attribute, and keep it as a required attribute for the implicit dependency example.

Is it true that people should use implicit dependencies for required things and explicit dependencies for optional things?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess, is it currently possible for people to have an optional implicit dependency, or not until the upcoming work happens?

Copy link
Member

Choose a reason for hiding this comment

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

No way to have an optional explicit dependency. Yeah you could make an implicit dependency optional. Its a bit annoying right now:

  1. See in validate that its not defined and don't return it as a dependency.
  2. In your implementation you check to make sure the dependency is not null and gracefully skip using it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To illuminate other reviewers: Based on offline sync, we were using "optional" in different ways--If a user does put something in depends_on, the resource won't currently start if the dependency doesn't start. What I was meaning with "optional" is it's optional for the user to even configure it at all. But sounds like you can do that with either implicit or explicit deps--it's all about how you implement validate.

Based on offline convo I will remove explicit dependencies since they just confuse users and aren't actually necessary except in cases of backwards compatibility.

{{< /tabs >}}

{{% hiddencontent %}}
There is not currently an SDK method to access configuration attributes of dependencies in Python or Go, but in Python it is possible to use `get_robot_part` to return information including the whole configuration of a machine part, and then access the configuration attributes of the dependency from there.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe this isn't right--looking into this more

@JessamyT
Copy link
Collaborator Author

@michaellee1019 I changed it to optional vs required but LMK if you think that is still confusing or overkill and it'd be better to just have one heading with "use dependencies" in general, with the required deps examples, and omit the optional examples.

Copy link
Member

@cheukt cheukt left a comment

Choose a reason for hiding this comment

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

did a quick pass, I'll be out next week so no need for my approval - if we want an opinion from netcode we can add @benjirewis

{{< /tabs >}}

{{% hiddencontent %}}
There is not currently an SDK method to directly access configuration attributes of dependencies in Python or Go, but in Python it is possible to use `get_robot_part` to return information including the whole configuration of a machine part, and then access the configuration attributes of the dependency from there.
Copy link
Member

Choose a reason for hiding this comment

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

what does this mean? I think we would want to discourage users from looking at configs for other resources

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We got a chatbot question about this specifically (from Sean Yu) and the bot answer was incorrect so was hoping to fix it. This hidden content short code means it’s not visible on the page, only to the chatbot if someone specifically asks. But if we don’t want people to do this at all should I just change this to say “you can’t, period”?

Copy link
Member

Choose a reason for hiding this comment

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

ok, I guess in that case this is fine - we should definitely discourage it tho

Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

This is awesome! You guys do a great job of adding stuff around this somewhat opaque logic. Some comments and I can help further while Cheuk is on PTO.

if cfg.CameraName == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "camera_name")
}
if reflect.TypeOf(cfg.CameraName).Kind() != reflect.String {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need this reflection check. cfg.CameraName will definitely be a string at this point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Does it get checked by viam-server automatically because CameraName string json:"camera_name" specifies type?

Copy link
Member

Choose a reason for hiding this comment

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

Basically yep! It's not technically viam-server that does the conversion from proto to a golang string, but the Golang module library code does it and will error if the thing specified in JSON is not actually a string/cfg.CameraName will just "".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah okay ty!! Wait it'll error or it'll assign it an empty string or both?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for ambiguity! There will be an error during validation for the resource if camera_name is not a string in the JSON.

Copy link
Member

@michaellee1019 michaellee1019 left a comment

Choose a reason for hiding this comment

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

Read through it and LGTM! A great improvement. I would double check on that comment thread with net code about the exact shutdown behavior. It might warrant a quick explanation about the overall timing, and then a more detailed section with the caveats about the shutdown timing based on the modules behavior. For example:

When `viam-server` shuts down, it attempts to stop all running modules. This can take up to x seconds depending on the response time required by a module to shut down.

And then a section that describes the shutdown and killing behavior.

@JessamyT
Copy link
Collaborator Author

JessamyT commented Mar 31, 2025

Thanks!

Benji okayed the suggestion on that thread via slack so I went ahead and committed now

Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

All LGTM!

@JessamyT JessamyT requested a review from npentrel March 31, 2025 17:50
@JessamyT JessamyT requested a review from npentrel April 2, 2025 18:51
@JessamyT JessamyT merged commit 09d2837 into viamrobotics:main Apr 4, 2025
12 checks passed
@JessamyT JessamyT deleted the 3699modules branch April 4, 2025 16:57
Copy link

github-actions bot commented Apr 4, 2025

🔎💬 Inkeep AI search and chat service is syncing content for source 'Viam Docs (https://docs.viam.com)'

@JessamyT JessamyT changed the title DOCS-3699, DOCS-2540: Add module lifecycle and dependencies guidance DOCS-3699, DOCS-2567: Add module lifecycle and dependencies guidance Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to build This pull request is marked safe to build from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants