-
Notifications
You must be signed in to change notification settings - Fork 43
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-1069: Document API methods for Session
and SessionManager
#1752
Changes from all commits
3457d32
ef609d7
3400631
0b8fa0d
836243f
5526005
54290b3
8a3387e
c80d2a0
1fb10ff
881ed1d
1300794
2b26818
ed2837a
729654c
efc91ad
15a64a8
6ec24e2
42345e9
d0d917b
e951270
1c1d9f1
dadae24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
title: "Monitor resource sessions with the Session API" | ||
linkTitle: "Session API" | ||
weight: 20 | ||
type: "docs" | ||
description: "Safety monitor resources and attach contexts to sessions with the Session API." | ||
tags: ["client", "sdk", "viam-server", "networking", "apis", "robot api", "session", "sessions", "session management"] | ||
--- | ||
|
||
When controlling a robot or fleet with Viam, you want a way to understand the presence of the clients that are communicating with and authenticated to each robot's `viam-server`. | ||
The period of time during which these clients are present is called a *session*. | ||
|
||
## API | ||
|
||
The `Session` API supports the following methods: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question for @dgottlieb or any SME who has a better idea of what's going on in the RDK than I do: is this correct? Or should this all be replaced with https://pkg.go.dev/go.viam.com/[email protected]/session#Session as these are just the methods the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, for posterity, these are internal data types that are not client facing. |
||
|
||
{{< readfile "/static/include/program/apis/session.md" >}} | ||
|
||
{{% alert title="Tip" color="tip" %}} | ||
|
||
The following code examples assume that you have a robot configured with a [base component](/components/base/), and that you add the required code to connect to your robot and import any required packages, including the `sessions` package, at the top of your client file. | ||
Go to your robot's **Code Sample** tab on the [Viam app](https://app.viam.com) for boilerplate code to connect to your robot. | ||
|
||
{{% /alert %}} | ||
|
||
### SafetyMonitor | ||
|
||
Safety monitor this target {{< glossary_tooltip term_id="resource" text="resource" >}} so that, if it exists, if this session ends as the last session to monitor it, the `SessionManager` attempts to stop the resource by calling the `Stop()` method of the resource API. | ||
|
||
Do not call this method from the resource being monitored itself, but instead from another client, like another resource, that controls the monitored resource on behalf of some request or routine. | ||
For example, it would be appropriate for a remote [input controller](/components/input-controller/) resource to call a `SafetyMonitor()` on a base that it is remotely controlling the motion of. | ||
|
||
In the context of a gRPC handled request, this function can only be called before the first response is sent back. | ||
In the case of unary, it can only be called before the handler returns. | ||
|
||
**Parameters:** | ||
|
||
- `ctx` [(Context)](https://pkg.go.dev/context): A Context carries a deadline, a cancellation signal, and other values across API boundaries. | ||
- `target` [(resource.Resource)](https://pkg.go.dev/go.viam.com/rdk/resource#Resource): The target resource. | ||
|
||
**Returns:** | ||
|
||
- None | ||
|
||
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/session#SafetyMonitor). | ||
|
||
```go | ||
myBase, err := base.FromRobot(robot, "my_base") | ||
|
||
// Signal to the session that the given target resource should be safety monitered. | ||
session = session.SafetyMonitor(ctx, myBase) | ||
``` | ||
|
||
### SafetyMonitorResourceName | ||
|
||
Safety monitor this target {{< glossary_tooltip term_id="resource" text="resource" >}} so that, if it exists, if this session ends as the last session to monitor it, the `SessionManager` attempts to stop the resource by calling the `Stop()` method of the resource API. | ||
|
||
Do not call this method from the resource being monitored itself, but instead from another client, like another resource, that controls the monitored resource on behalf of some request or routine. | ||
For example, it would be appropriate for a remote [input controller](/components/input-controller/) resource to call a `SafetyMonitor()` on a base that it is remotely controlling the motion of. | ||
|
||
In the context of a gRPC handled request, this function can only be called before the first response is sent back. | ||
In the case of unary, it can only be called before the handler returns. | ||
|
||
**Parameters:** | ||
|
||
- `ctx` [(Context)](https://pkg.go.dev/context): A Context carries a deadline, a cancellation signal, and other values across API boundaries. | ||
- `targetName` [(resource.Name)](https://pkg.go.dev/go.viam.com/rdk/resource#Name): The `name` you have set for the resource in configuration. | ||
|
||
**Returns:** | ||
|
||
- None | ||
|
||
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/session#SafetyMonitorResourceName). | ||
|
||
```go | ||
myBase, err := base.FromRobot(robot, "my_base") | ||
|
||
// Signal to the session that the given target resource should be safety monitered. | ||
session = session.SafetyMonitor(ctx, "my_base") | ||
``` | ||
|
||
### ToContext | ||
|
||
Attach a session to the given context. | ||
|
||
**Parameters:** | ||
|
||
- `ctx` [(Context)](https://pkg.go.dev/context): A Context carries a deadline, a cancellation signal, and other values across API boundaries. | ||
- `sess` [(*Session)](https://pkg.go.dev/go.viam.com/rdk/session#Session): The session object that you wish to attach to this context. | ||
|
||
**Returns:** | ||
|
||
- `ctx` [(Context)](https://pkg.go.dev/context): A Context carries a deadline, a cancellation signal, and other values across API boundaries. | ||
|
||
For more information, see the [Go SDK Docs](https://pkg.go.dev/go.viam.com/rdk/session#ToContext). | ||
|
||
```go | ||
// Attach session "my session" to the given Context | ||
session = session.ToContext(context.Background(), my_session) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Method Name | Description | ||
----------- | ----------- | ||
[`Start`](/program/apis/sessions/#start) | Create a new session that expects at least one heartbeat within the configured window. | ||
[`All`](/program/apis/sessions/#all) | Get all active sessions. | ||
[`FindByID`](/program/apis/sessions/#findbyid) | Find a session by the given ID. If found, trigger a heartbeat and extend the lifetime of the session. | ||
[`AssociateResource`](/program/apis/sessions/#associateresource) | Associate a session ID to a monitored resource. All associated resources will be stopped with this session is expired. | ||
[`Close`](/program/apis/sessions/#close) | Stop the session manager without directing any sessions to expire. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Method Name | Description | ||
----------- | ----------- | ||
[`SafetyMonitor`](/program/apis/sessions/#safetymonitor) | Safety monitor this target {{< glossary_tooltip term_id="resource" text="resource" >}} so that if this session ends as the last session to monitor it, the `SessionManager` attempts to stop the resource by calling the `Stop()` method of the resource API. | ||
[`SafetyMonitorResourceName`](/program/apis/sessions/#safetymonitorresourcename) | Safety monitor this target {{< glossary_tooltip term_id="resource" text="resource" >}} so that if this session ends as the last session to monitor it, the `SessionManager` attempts to stop the resource by calling the `Stop()` method of the resource API. | ||
[`ToContext`](/program/apis/sessions/#tocontext) | Attach a session to the given context. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs documentation on
NewSessionManager()
in robot API to be functional-- sme review may help clear up some confusion on my endThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, then removed,
NewSessionManager()
instantiation ofmySessionManager
-- unsure whether that should be in there or not. How would a user most simply request to the default session manager API with what we have set up in the RDK? @dgottliebThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, this
SessionManager
structure is used by theviam-server
running on a robot to track and time out/callStop
robot parts when heartbeats stop coming in.Client code using the Go SDK wouldn't see anything happen if they were to try using this object.
That said, the documentation for these methods is 100% correct!