-
Notifications
You must be signed in to change notification settings - Fork 143
💥 feat(sdk): Support Standalone Activities #1402
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
Open
maciejdudko
wants to merge
1
commit into
temporalio:main
Choose a base branch
from
maciejdudko:standalone-activities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| mod activity_execution_info; | ||
| mod activity_handle; | ||
|
|
||
| use crate::errors::ClientError; | ||
| pub use activity_execution_info::{ | ||
| ActivityExecutionDescription, ActivityExecutionInfo, ActivityExecutionInfoLike, | ||
| ActivityExecutionStatus, PendingActivityState, | ||
| }; | ||
| pub use activity_handle::ActivityHandle; | ||
| use futures_util::{Stream, StreamExt}; | ||
| use std::{ | ||
| pin::Pin, | ||
| task::{Context, Poll}, | ||
| }; | ||
| use temporalio_common::{ | ||
| protos::temporal::api::{ | ||
| activity::v1::ActivityExecutionListInfo, | ||
| workflowservice::v1::{ | ||
| CountActivityExecutionsResponse, count_activity_executions_response, | ||
| }, | ||
| }, | ||
| search_attributes::{SearchAttributeError, SearchAttributeValue}, | ||
| }; | ||
|
|
||
| /// A stream of activity executions from a list query. | ||
| /// Internally paginates through results from the server. | ||
| pub struct ListActivitiesStream { | ||
| inner: Pin<Box<dyn Stream<Item = Result<Vec<ActivityExecutionListInfo>, ClientError>> + Send>>, | ||
| buffer: <Vec<ActivityExecutionListInfo> as IntoIterator>::IntoIter, | ||
| } | ||
|
|
||
| impl ListActivitiesStream { | ||
| pub(crate) fn new( | ||
| stream: impl Stream<Item = Result<Vec<ActivityExecutionListInfo>, ClientError>> + Send + 'static, | ||
| ) -> Self { | ||
| Self { | ||
| inner: Box::pin(stream), | ||
| buffer: vec![].into_iter(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Stream for ListActivitiesStream { | ||
| type Item = Result<ActivityExecutionInfo, ClientError>; | ||
|
|
||
| fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| loop { | ||
| if let Some(info) = self.buffer.next() { | ||
| return Poll::Ready(Some(Ok(info.into()))); | ||
| } | ||
| match self.inner.poll_next_unpin(cx) { | ||
| Poll::Ready(Some(Ok(items))) => { | ||
| self.buffer = items.into_iter(); | ||
| } | ||
| Poll::Ready(Some(Err(e))) => { | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| Poll::Ready(None) => { | ||
| return Poll::Ready(None); | ||
| } | ||
| Poll::Pending => { | ||
| return Poll::Pending; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Result of an activity count operation. | ||
| /// | ||
| /// If the query includes a group-by clause, `groups` will contain the aggregated | ||
| /// counts and `count` will be the sum of all group counts. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ActivityExecutionCount { | ||
| count: usize, | ||
| groups: Vec<ActivityExecutionCountAggregationGroup>, | ||
| } | ||
|
|
||
| impl ActivityExecutionCount { | ||
| pub(crate) fn from_response(resp: CountActivityExecutionsResponse) -> Self { | ||
| Self { | ||
| count: resp.count as usize, | ||
| groups: resp | ||
| .groups | ||
| .into_iter() | ||
| .map(ActivityExecutionCountAggregationGroup::from_proto) | ||
| .collect(), | ||
| } | ||
| } | ||
|
|
||
| /// The approximate number of activities matching the query. | ||
| /// If grouping was applied, this is the sum of all group counts. | ||
| pub fn count(&self) -> usize { | ||
| self.count | ||
| } | ||
|
|
||
| /// The groups if the query had a group-by clause, or empty if not. | ||
| pub fn groups(&self) -> &[ActivityExecutionCountAggregationGroup] { | ||
| &self.groups | ||
| } | ||
| } | ||
|
|
||
| /// Aggregation group from an activity count query with a group-by clause. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ActivityExecutionCountAggregationGroup { | ||
| raw: count_activity_executions_response::AggregationGroup, | ||
| } | ||
|
|
||
| impl ActivityExecutionCountAggregationGroup { | ||
| fn from_proto(proto: count_activity_executions_response::AggregationGroup) -> Self { | ||
| Self { raw: proto } | ||
| } | ||
|
|
||
| /// Retrieve a typed group value at `index`. | ||
| /// | ||
| /// Returns `None` if the index is out of bounds or deserialization fails. | ||
| /// Use [`Self::try_get`] for explicit error handling. | ||
| pub fn get<T: SearchAttributeValue>(&self, index: usize) -> Option<T> { | ||
| self.try_get(index).ok().flatten() | ||
| } | ||
|
|
||
| /// Retrieve a typed group value at `index`, preserving deserialization | ||
| /// errors. | ||
| /// | ||
| /// Returns `Ok(None)` if the index is out of bounds and `Err` if the | ||
| /// payload cannot be deserialized. | ||
| pub fn try_get<T: SearchAttributeValue>( | ||
| &self, | ||
| index: usize, | ||
| ) -> Result<Option<T>, SearchAttributeError> { | ||
| match self.raw.group_values.get(index) { | ||
| Some(payload) => T::from_search_attribute_payload(payload).map(Some), | ||
| None => Ok(None), | ||
| } | ||
| } | ||
|
|
||
| /// The approximate number of workflows matching for this group. | ||
| pub fn count(&self) -> usize { | ||
| self.raw.count as usize | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 could just be stored as a Vec more simply?
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.
bufferis stored as an iterator so thatpoll_nextcan take the next element efficiently. Storing as a reversed Vec or a VecDeque would require copying out of the original Vec inside the response - usinginto_iteravoids it.Uh oh!
There was an error while loading. Please reload this page.
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 don't think that's right. You just
pop_front()theVecDequeinpoll_next(). No copy involved. Next stream element just becomesself.buffer = items.into();(which is constant time and doesn't reallocate)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.
Didn't know the last part. Going to change to VecDeque.