-
Notifications
You must be signed in to change notification settings - Fork 79
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
Updated Activity/Workflow Function bounds to allow a more generic use… #543
Conversation
Hi @Sushisource, This is the last PR(relating to SDK changes) I will submit until further update from you on the proposal. Thanks for taking the time, I know this is not planned work for you. Ignore the function signature but my code currently looks a lot boilerplate free like I have written in this blog |
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.
Sorry it took me a bit to get around to this
/// Build a workflow function from a closure or function pointer which accepts a [WfContext] and takes one argument | ||
pub fn from<A, F, Fut, R, O>(f: F) -> Self |
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.
So ideally both this and the activity version are implemented generically (as actual From
impls) for anything that matches these bounds -- allowing us to avoid XXXFunction::from
calls at definition sites, since the registration functions already accept impl Into<XXXFunction>
. I think this is a really nice piece of ergonomics to maintain.
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.
Hi, sorry, did not understand this comment completely.
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.
The idea would be to do something like this:
impl< ... generics ... > From<F> where: bounds for ActivityFunction {
...
}
For both activity and wf functions
So there's no need to call ActivityFunction::from
at the same place you're defining or registering the function - everything just slots in.
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.
Haa,
I tried to do that, but the From
implementation cannot be implemented for functions with more generic arguments, it fails with 'unconstrained bounds'
So the only From implementation is the one existing. Activity/Workflow Function that takes only Context.
For functions that take an argument, We need the ActivityFunction::from/WorkflowFunction::new
wrapper like in the core tests.
But in the production SDK, we will actually not use these functions(I wish). There should be higher-level 'execute_activity' or 'execute_childworkflow' functions that exist in other SDKs which hide the 'Payload' processing(Converter etc), but those that check the types of the arguments like this
Essentially, the PR is to allow any function that can be converted into the below functions that the scheduling(currently) works with.
dyn Fn(WfContext) -> BoxFuture<'static, Result<WfExitValue<Payload>, anyhow::Error>>
+ Send
+ Sync
+ 'static
or
dyn Fn(ActContext) -> BoxFuture<'static, Result<ActExitValue<Payload>, anyhow::Error>>
+ Send
+ Sync,
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.
So this https://github.com/temporalio/sdk-core/blob/master/sdk/src/lib.rs#L840 implements an arg-accepting automatic implementation for activities - and, yeah, clearly we can't make that work for both 0 and 1 input-arity functions without some extra boilerplate or macros, etc. That said, there's nothing preventing implementing IntoActivityFunc
for 0 and 1 input-arity functions and having this all work properly. Same could be done with workflows.
I think that'd be the best place to leave things for now, no need to worry about higher-arity functions at the moment.
You can see a preview of what I'm thinking of for later here: #550 which will lean on procmacros for making this all seamless.
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 will look up the proposal and adapt this accordingly.
37d727d
to
3a395fa
Compare
3a395fa
to
e00fa49
Compare
Closed this as per Comments |
What was changed
Updated workflow and activity functions to take 0 or 1 arguments and accommodate generic functions as extensions (loosen up bounds)
Unfortunately, it touches activity tests, but they are only updating the function signature, all tests pass including load tests.
Why?
To enable creating a type-safe API using more generic user functions.
For example, a user function that does not return
anyhow::Error
With this, we can register
Checklist
Closes
How was this tested:
All tests passed.
Updated function documentation