-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk-dotnet): add mutation expression in the workflow thread (#1283)
- Loading branch information
1 parent
5a8852a
commit e4daa3d
Showing
6 changed files
with
265 additions
and
1 deletion.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
sdk-dotnet/Examples/MutationExample/MutationExample.csproj
This file contains 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\LittleHorse.Sdk\LittleHorse.Sdk.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,22 @@ | ||
using LittleHorse.Sdk.Worker; | ||
|
||
namespace MutationExample; | ||
|
||
public class MyWorker | ||
{ | ||
[LHTaskMethod("spider-bite")] | ||
public string SpiderBite(string name) | ||
{ | ||
Console.WriteLine("Executing spider-bite"); | ||
var names = new List<string> { "Miles", "Peter" }; | ||
|
||
if (names.Contains(name)) | ||
{ | ||
Console.WriteLine($"{name} got bitten"); | ||
|
||
return "Spider-man"; | ||
} | ||
|
||
return $"The spider bite has no effect on {name}"; | ||
} | ||
} |
This file contains 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,71 @@ | ||
using LittleHorse.Sdk; | ||
using LittleHorse.Sdk.Common.Proto; | ||
using LittleHorse.Sdk.Worker; | ||
using LittleHorse.Sdk.Workflow.Spec; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using MutationExample; | ||
|
||
public abstract class Program | ||
{ | ||
const string TaskDefName = "spider-bite"; | ||
private static ServiceProvider? _serviceProvider; | ||
private static void SetupApplication() | ||
{ | ||
_serviceProvider = new ServiceCollection() | ||
.AddLogging(config => | ||
{ | ||
config.AddConsole(); | ||
config.SetMinimumLevel(LogLevel.Debug); | ||
}) | ||
.BuildServiceProvider(); | ||
} | ||
|
||
private static LHConfig GetLHConfig(string[] args, ILoggerFactory loggerFactory) | ||
{ | ||
var config = new LHConfig(loggerFactory); | ||
|
||
string filePath = Path.Combine(Directory.GetCurrentDirectory(), ".config/littlehorse.config"); | ||
if (File.Exists(filePath)) | ||
config = new LHConfig(filePath, loggerFactory); | ||
|
||
return config; | ||
} | ||
|
||
private static Workflow GetWorkflow() | ||
{ | ||
void MyEntryPoint(WorkflowThread wf) | ||
{ | ||
var theName = wf.DeclareStr("name"); | ||
// We pass the name of the person and receive if it is spider-man or not | ||
NodeOutput output = wf.Execute(TaskDefName, theName); | ||
|
||
// We save the output in the variable | ||
wf.Mutate(theName, VariableMutationType.Assign, output); | ||
} | ||
|
||
return new Workflow("example-mutation", MyEntryPoint); | ||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
SetupApplication(); | ||
if (_serviceProvider != null) | ||
{ | ||
var loggerFactory = _serviceProvider.GetRequiredService<ILoggerFactory>(); | ||
var config = GetLHConfig(args, loggerFactory); | ||
|
||
var executable = new MyWorker(); | ||
var worker = new LHTaskWorker<MyWorker>(executable, TaskDefName, config); | ||
|
||
worker.RegisterTaskDef(); | ||
|
||
var workflow = GetWorkflow(); | ||
workflow.RegisterWfSpec(config.GetGrpcClientInstance()); | ||
|
||
Thread.Sleep(300); | ||
|
||
worker.Start(); | ||
} | ||
} | ||
} |
This file contains 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,33 @@ | ||
## Running MutationExample | ||
|
||
In this example you will see how to mutate variables. | ||
|
||
Let's run the example in `MutationExample` | ||
|
||
``` | ||
dotnet build | ||
dotnet run | ||
``` | ||
|
||
In another terminal, use `lhctl` to run the workflow: | ||
|
||
``` | ||
# Execute with Peter or Miles | ||
lhctl run example-mutation name Peter | ||
# Execute with other names | ||
lhctl run example-mutation name Pepito | ||
``` | ||
|
||
In addition, you can check the result with: | ||
|
||
``` | ||
# This call shows the result | ||
lhctl get wfRun <wf_run_id> | ||
# This will show you all nodes in the run | ||
lhctl list nodeRun <wf_run_id> | ||
# This shows the task run information | ||
lhctl get taskRun <wf_run_id> <task_run_global_id> | ||
``` |
This file contains 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
This file contains 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