-
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(dotnet-sdk): create worker context method to access from task ex…
…ecution (#1173)
- Loading branch information
1 parent
db18236
commit b0adf3c
Showing
16 changed files
with
331 additions
and
38 deletions.
There are no files selected for viewing
File renamed without changes.
1 change: 1 addition & 0 deletions
1
sdk-dotnet/Examples/MaskedFieldsExample/MaskedFieldsExample.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
File renamed without changes.
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,21 @@ | ||
using LittleHorse.Sdk.Worker; | ||
|
||
namespace WorkerContextExample; | ||
|
||
public class MyWorker | ||
{ | ||
[LHTaskMethod("task")] | ||
public void ProcessTask(long requestTime, LHWorkerContext context) | ||
{ | ||
context.Log("ProcessPayment"); | ||
Console.WriteLine($"Processing request time: {requestTime}"); | ||
Console.WriteLine($"The Workflow Run Id is: {context.GetWfRunId()}"); | ||
Console.WriteLine($"The Node Run Id is: {context.GetNodeRunId()}"); | ||
Console.WriteLine($"The Task Run Id is: {context.GetTaskRunId()}"); | ||
Console.WriteLine($"The Idempotency Key is: {context.GetIdempotencyKey()}"); | ||
Console.WriteLine($"The Attempt Number is: {context.GetAttemptNumber()}"); | ||
Console.WriteLine($"The Scheduled Time is: {context.GetScheduledTime()}"); | ||
Console.WriteLine($"The User Group is: {context.GetUserGroup()}"); | ||
Console.WriteLine($"The User Id is: {context.GetUserId()}"); | ||
} | ||
} |
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,65 @@ | ||
using LittleHorse.Sdk; | ||
using LittleHorse.Sdk.Worker; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WorkerContextExample; | ||
|
||
public abstract class Program | ||
{ | ||
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 List<LHTaskWorker<MyWorker>> GetTaskWorkers(LHConfig config) | ||
{ | ||
MyWorker executableExceptionHandling = new MyWorker(); | ||
var workers = new List<LHTaskWorker<MyWorker>> | ||
{ | ||
new(executableExceptionHandling, "task", config) | ||
}; | ||
|
||
return workers; | ||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
SetupApplication(); | ||
if (_serviceProvider != null) | ||
{ | ||
var loggerFactory = _serviceProvider.GetRequiredService<ILoggerFactory>(); | ||
var config = GetLHConfig(args, loggerFactory); | ||
var workers = GetTaskWorkers(config); | ||
foreach (var worker in workers) | ||
{ | ||
worker.RegisterTaskDef(); | ||
} | ||
|
||
Thread.Sleep(300); | ||
|
||
foreach (var worker in workers) | ||
{ | ||
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,45 @@ | ||
## Running WorkerContextExample | ||
|
||
This example shows how to get access to the context when executing a task. | ||
Go to the class `MyWorker`. | ||
|
||
Let's run the example in `WorkerContextExample` | ||
|
||
``` | ||
dotnet build | ||
dotnet run | ||
``` | ||
|
||
In another terminal, use `lhctl` to run the workflow: | ||
|
||
``` | ||
lhctl run example-worker-context | ||
``` | ||
|
||
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 tha run | ||
lhctl list nodeRun <wf_run_id> | ||
# This shows the task run information | ||
lhctl get taskRun <wf_run_id> <task_run_global_id> | ||
``` | ||
|
||
## Considerations | ||
|
||
If you need get access to the context you have to add a `LHWorkerContext` | ||
parameter to the signature of your task: | ||
|
||
``` | ||
[LHTaskMethod("task")] | ||
public void ProcessTask(long requestTime, LHWorkerContext context) | ||
{ | ||
... | ||
} | ||
``` | ||
|
||
The `WorkerContext` should be the last parameter. |
14 changes: 14 additions & 0 deletions
14
sdk-dotnet/Examples/WorkerContextExample/WorkerContextExample.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,41 @@ | ||
using System.Text; | ||
using LittleHorse.Common.Proto; | ||
|
||
namespace LittleHorse.Sdk.Helper | ||
{ | ||
public static class LHHelper | ||
{ | ||
public static WfRunId GetWFRunId(TaskRunSource taskRunSource) | ||
{ | ||
switch (taskRunSource.TaskRunSourceCase) | ||
{ | ||
case TaskRunSource.TaskRunSourceOneofCase.TaskNode: | ||
return taskRunSource.TaskNode.NodeRunId.WfRunId; | ||
case TaskRunSource.TaskRunSourceOneofCase.UserTaskTrigger: | ||
return taskRunSource.UserTaskTrigger.NodeRunId.WfRunId; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
public static string TaskRunIdToString(TaskRunId taskRunId) | ||
{ | ||
return $"{taskRunId.WfRunId}/{taskRunId.TaskGuid}"; | ||
} | ||
|
||
private static string ParseWfRunIdToString(WfRunId wfRunId) { | ||
var output = new StringBuilder(); | ||
if (wfRunId.ParentWfRunId != null) { | ||
output.Append(ParseWfRunIdToString(wfRunId.ParentWfRunId)); | ||
output.Append("_"); | ||
} | ||
output.Append(wfRunId.Id); | ||
|
||
return output.ToString(); | ||
} | ||
|
||
public static String ParseTaskRunIdToString(TaskRunId taskRunId) { | ||
return ParseWfRunIdToString(taskRunId.WfRunId) + "/" + taskRunId.TaskGuid; | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.