-
Notifications
You must be signed in to change notification settings - Fork 375
Workflow Authoring: initial methods #981
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
Changes from 14 commits
47dea1d
1e649cc
6dbbe26
f69a314
266cefd
01c01e5
73b08a5
b832ab8
2515d01
fc1ee56
dd5a294
99285a9
38d2330
6d7f5ff
1bbaa0a
601f09e
b9c99cf
f49b648
ad7ade2
509656b
fb8589c
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,49 @@ | ||
| using Dapr.Workflow; | ||
|
|
||
| // The workflow host is a background service that connects to the sidecar over gRPC | ||
| WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Dapr workflows are registered as part of the service configuration | ||
| builder.Services.AddWorkflow(options => | ||
| { | ||
| // Example of registering a "Hello World" workflow function | ||
| options.RegisterWorkflow<string, string>("HelloWorld", implementation: async (context, input) => | ||
| { | ||
| return await context.CallActivityAsync<string>("SayHello", "World"); | ||
| }); | ||
|
|
||
| // Example of registering a "Say Hello" workflow activity function | ||
| options.RegisterActivity<string, string>("SayHello", implementation: (context, input) => | ||
| { | ||
| return Task.FromResult($"Hello, {input}!"); | ||
| }); | ||
| }); | ||
|
|
||
| WebApplication app = builder.Build(); | ||
|
|
||
| // POST starts new workflow instances | ||
| app.MapPost("/workflow", async (HttpContext context, WorkflowClient client) => | ||
| { | ||
| string id = Guid.NewGuid().ToString()[..8]; | ||
|
tmacam marked this conversation as resolved.
|
||
| await client.ScheduleNewWorkflowAsync("HelloWorld", id); | ||
|
|
||
| // return an HTTP 202 and a Location header to be used for status query | ||
| return Results.AcceptedAtRoute("GetWorkflowEndpoint", new { id }); | ||
| }); | ||
|
|
||
| // GET fetches metadata for specific workflow instances | ||
| app.MapGet("/workflow/{id}", async (string id, WorkflowClient client) => | ||
|
tmacam marked this conversation as resolved.
Outdated
|
||
| { | ||
| WorkflowMetadata metadata = await client.GetWorkflowMetadataAsync(id, getInputsAndOutputs: true); | ||
| if (metadata.Exists) | ||
| { | ||
| return Results.Ok(metadata); | ||
| } | ||
| else | ||
| { | ||
| return Results.NotFound($"No workflow with ID = '{id}' was found."); | ||
| } | ||
| }).WithName("GetWorkflowEndpoint"); | ||
|
|
||
| app.Run("http://0.0.0.0:10080"); | ||
|
tmacam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
|
tmacam marked this conversation as resolved.
|
||
| "profiles": { | ||
| "OrderingWebApi": { | ||
| "commandName": "Project", | ||
| "launchBrowser": true, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| }, | ||
| "applicationUrl": "https://localhost:57899;http://localhost:57900" | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
|
Contributor
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. Just to confirm, this is required due to underlying libraries, right? Generally, the SDK has been building to 3.1 though we'd like to move away from that now that support has ended.
Contributor
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. As you mentioned, .NET 3.1 is no longer supported, so I figured it didn't make sense to add new code that requires .NET 3.1. However, the dependencies are all .NET Standard 2.0, so it's fine if you'd rather make this
Contributor
Author
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. The current example code as it is requires net6 :/ |
||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <LangVersion>10.0</LangVersion> | ||
| </PropertyGroup> | ||
|
|
||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ### Create new order | ||
| POST http://localhost:8080/workflow | ||
| Content-Type: application/json | ||
|
|
||
| ### Query placeholder | ||
| GET http://localhost:8080/workflow/XXX |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <!-- NuGet configuration --> | ||
| <PropertyGroup> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <PackageId>Dapr.Workflow</PackageId> | ||
| <Title>Dapr Workflow Authoring SDK</Title> | ||
| <Description>Dapr Workflow SDK for building workflows as code with Dapr</Description> | ||
| <VersionPrefix>0.1.0</VersionPrefix> | ||
| <VersionSuffix>alpha</VersionSuffix> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.DurableTask.Client.Grpc" Version="1.0.0-rc.1" /> | ||
| <PackageReference Include="Microsoft.DurableTask.Worker.Grpc" Version="1.0.0-rc.1" /> | ||
| <PackageReference Include="Dapr.AspNetCore" Version="1.9.0" /> | ||
|
Contributor
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. SDK projects reference the other SDKs locally instead of their nuget packages. Ex: <ItemGroup>
<ProjectReference Include="..\Dapr.Client\Dapr.Client.csproj" />
</ItemGroup> |
||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2022 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| namespace Dapr.Workflow | ||
| { | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.DurableTask; | ||
|
|
||
| /// <summary> | ||
| /// Defines properties and methods for task activity context objects. | ||
| /// </summary> | ||
| public class WorkflowActivityContext | ||
| { | ||
| readonly TaskActivityContext innerContext; | ||
|
|
||
| internal WorkflowActivityContext(TaskActivityContext innerContext) | ||
| { | ||
| this.innerContext = innerContext ?? throw new ArgumentNullException(nameof(innerContext)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the name of the activity. | ||
| /// </summary> | ||
| public TaskName Name => this.innerContext.Name; | ||
|
|
||
| /// <summary> | ||
| /// Gets the unique ID of the current workflow instance. | ||
| /// </summary> | ||
| public string InstanceId => this.innerContext.InstanceId; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2022 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.DurableTask; | ||
| using Microsoft.DurableTask.Client; | ||
|
|
||
| namespace Dapr.Workflow | ||
| { | ||
| // TODO: This will be replaced by the official Dapr Workflow management client. | ||
| /// <summary> | ||
| /// Defines client operations for managing Dapr Workflow instances. | ||
| /// </summary> | ||
| public sealed class WorkflowClient : IAsyncDisposable | ||
| { | ||
| readonly DurableTaskClient innerClient; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="WorkflowClient"/> class. | ||
| /// </summary> | ||
| /// <param name="innerClient">The Durable Task client used to communicate with the Dapr sidecar.</param> | ||
| /// <exception cref="ArgumentNullException">Thrown if <paramref name="innerClient"/> is <c>null</c>.</exception> | ||
| public WorkflowClient(DurableTaskClient innerClient) | ||
| { | ||
| this.innerClient = innerClient ?? throw new ArgumentNullException(nameof(innerClient)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Schedules a new workflow instance for execution. | ||
| /// </summary> | ||
| public Task<string> ScheduleNewWorkflowAsync( | ||
|
tmacam marked this conversation as resolved.
|
||
| string name, | ||
| string? instanceId = null, | ||
| object? input = null, | ||
| DateTime? startTime = null) | ||
| { | ||
| StartOrchestrationOptions options = new(instanceId, startTime); | ||
| return this.innerClient.ScheduleNewOrchestrationInstanceAsync(name, input, options); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Fetches runtime metadata for the specified workflow instance. | ||
| /// </summary> | ||
| public async Task<WorkflowMetadata> GetWorkflowMetadataAsync(string instanceId, bool getInputsAndOutputs = false) | ||
| { | ||
| OrchestrationMetadata? metadata = await this.innerClient.GetInstanceMetadataAsync( | ||
| instanceId, | ||
| getInputsAndOutputs); | ||
| return new WorkflowMetadata(metadata); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Disposes any unmanaged resources associated with this client. | ||
| /// </summary> | ||
| public ValueTask DisposeAsync() | ||
| { | ||
| return ((IAsyncDisposable)this.innerClient).DisposeAsync(); | ||
| } | ||
| } | ||
| } | ||
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.
How do workflows and activities relate to each other? Should they be tied together in the API at all?
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 let other correct me but if you think of a workflow as defining a graph, activities are the nodes. Both concepts are linked. https://docs.temporal.io/activities
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 wouldn't say this is required, just musing since this is a preview/alpha feature:
That was my initial thought, but I guess my question was more do the activities need to be directly related to the workflow, or can they exist outside of one? Should we have some kind of validation that checks that "SayHello" is defiend? Or should we define the activity in the workflow registration/via actual object references?