Skip to content

Commit

Permalink
Merge pull request #65 from conductor-sdk/fix-worker-host-extension
Browse files Browse the repository at this point in the history
Fix worker host extension
  • Loading branch information
gardusig authored May 9, 2023
2 parents 31ff28c + 6c9c957 commit 2ad581a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
1 change: 0 additions & 1 deletion Conductor/Client/Interfaces/IWorkflowTask.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Conductor.Client.Models;
using System.Threading;
using Conductor.Client.Worker;

namespace Conductor.Client.Interfaces
Expand Down
34 changes: 7 additions & 27 deletions Conductor/Client/Worker/WorkflowTaskHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,22 @@ namespace Conductor.Client.Extensions
{
public class WorkflowTaskHost
{
private static Dictionary<LogLevel, IHost> _hostByLogLevel;

static WorkflowTaskHost()
public static IHost CreateWorkerHost(LogLevel logLevel = LogLevel.Information)
{
_hostByLogLevel = new Dictionary<LogLevel, IHost>();
return CreateWorkerHost(ApiExtensions.GetConfiguration(), logLevel);
}

public static IHost GetWorkerHost(LogLevel logLevel = LogLevel.Information)
public static IHost CreateWorkerHost(Configuration configuration, LogLevel logLevel = LogLevel.Information)
{
if (!_hostByLogLevel.ContainsKey(logLevel))
{
var host = CreateWorkerHost(ApiExtensions.GetConfiguration(), logLevel);
_hostByLogLevel[logLevel] = host;
}
return _hostByLogLevel[logLevel];
return CreateWorkerHost(configuration, logLevel);
}

public static IHost CreateWorkerHost(Configuration configuration, LogLevel logLevel = LogLevel.Information)
public static IHost CreateWorkerHost<T>(LogLevel logLevel = LogLevel.Information, params T[] workers) where T : IWorkflowTask
{
return new HostBuilder()
.ConfigureServices(
(ctx, services) =>
{
services.AddConductorWorker(configuration);
services.WithHostedService();
}
).ConfigureLogging(
logging =>
{
logging.SetMinimumLevel(logLevel);
logging.AddConsole();
}
).Build();
return CreateWorkerHost(ApiExtensions.GetConfiguration(), logLevel, workers);
}

public static IHost CreateWorkerHost(Configuration configuration, LogLevel logLevel = LogLevel.Information, params IWorkflowTask[] workers)
public static IHost CreateWorkerHost<T>(Configuration configuration, LogLevel logLevel = LogLevel.Information, params T[] workers) where T : IWorkflowTask
{
return new HostBuilder()
.ConfigureServices(
Expand Down
2 changes: 1 addition & 1 deletion Tests/Worker/WorkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private async System.Threading.Tasks.Task<ConcurrentBag<string>> StartWorkflows(

private async System.Threading.Tasks.Task ExecuteWorkflowTasks(TimeSpan workflowCompletionTimeout)
{
var host = WorkflowTaskHost.GetWorkerHost(Microsoft.Extensions.Logging.LogLevel.Debug);
var host = WorkflowTaskHost.CreateWorkerHost(Microsoft.Extensions.Logging.LogLevel.Debug, new ClassWorker());
await host.StartAsync();
Thread.Sleep(workflowCompletionTimeout);
await host.StopAsync();
Expand Down
21 changes: 20 additions & 1 deletion Tests/Worker/Workers.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Conductor.Client.Extensions;
using Conductor.Client.Interfaces;
using Conductor.Client.Models;
using Conductor.Client.Worker;

namespace Tests.Worker
{
[WorkerTask]
public class Workers
public class FunctionalWorkers
{
// Polls for 1 task every 35ms
[WorkerTask("test-sdk-csharp-task", 1, "taskDomain", 35, "workerId")]
Expand All @@ -21,4 +22,22 @@ public TaskResult SimpleWorker(Task task)
return task.Completed();
}
}

public class ClassWorker : IWorkflowTask
{
public string TaskType { get; }

public WorkflowTaskExecutorConfiguration WorkerSettings { get; }

public ClassWorker(string taskType = "random_task_type")
{
TaskType = taskType;
WorkerSettings = new WorkflowTaskExecutorConfiguration();
}

public TaskResult Execute(Task task)
{
throw new System.Exception("random exception");
}
}
}

0 comments on commit 2ad581a

Please sign in to comment.