Skip to content

Commit

Permalink
Merge branch 'main' of github.com:conductor-sdk/conductor-csharp
Browse files Browse the repository at this point in the history
  • Loading branch information
v1r3n committed Jan 4, 2024
2 parents e72ac0a + 685b4fd commit ae86f42
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 28 deletions.
6 changes: 5 additions & 1 deletion Conductor/Client/Authentication/TokenHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using conductor.csharp.Client.Extensions;
using Conductor.Api;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System;

namespace Conductor.Client.Authentication
Expand All @@ -9,10 +11,12 @@ public class TokenHandler
private static int REFRESH_TOKEN_RETRY_COUNTER_LIMIT = 5;
private static readonly object _lockObject = new object();
private readonly MemoryCache _memoryCache;
private static ILogger _logger;

public TokenHandler()
{
_memoryCache = new MemoryCache(new MemoryCacheOptions());
_logger = ApplicationLogging.CreateLogger<TokenHandler>();
}

public string GetToken(OrkesAuthenticationSettings authenticationSettings, TokenResourceApi tokenClient)
Expand Down Expand Up @@ -50,7 +54,7 @@ private string GetTokenFromServer(OrkesAuthenticationSettings authenticationSett
}
catch (Exception e)
{
Console.WriteLine($"Failed to refresh authentication token, attempt = {attempt}, error = {e.Message}");
_logger.LogError($"Failed to refresh authentication token, attempt = {attempt}, error = {e.Message}");
}
}
throw new Exception("Failed to refresh authentication token");
Expand Down
11 changes: 11 additions & 0 deletions Conductor/Client/Extensions/ApplicationLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Extensions.Logging;

namespace conductor.csharp.Client.Extensions
{
public static class ApplicationLogging
{
public static ILoggerFactory LoggerFactory = new LoggerFactory();
public static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>();
public static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName);
}
}
11 changes: 7 additions & 4 deletions Conductor/Client/Extensions/WorkflowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using conductor.csharp.Client.Extensions;

namespace Conductor.Client.Extensions
{
public class WorkflowExtensions
{
private static int RETRY_ATTEMPT_LIMIT = 3;
private static ILogger _logger = ApplicationLogging.CreateLogger<WorkflowExtensions>();

public static async Task<ConcurrentBag<string>> StartWorkflows(WorkflowResourceApi workflowClient, Models.StartWorkflowRequest startWorkflowRequest, int maxAllowedInParallel, int total)
{
Expand All @@ -18,7 +21,7 @@ public static async Task<ConcurrentBag<string>> StartWorkflows(WorkflowResourceA
{
await StartWorkflowBatch(workflowClient, startWorkflowRequest, maxAllowedInParallel, workflowIds);
}
Console.WriteLine($"Started {workflowIds.Count} workflows");
_logger.LogInformation($"Started {workflowIds.Count} workflows");
return workflowIds;
}

Expand All @@ -29,7 +32,7 @@ public static async Task<ConcurrentBag<string>> StartWorkflows(WorkflowResourceA
{
await GetWorkflowStatusBatch(workflowClient, workflowStatusList, index, index + maxAllowedInParallel, workflowIds);
}
Console.WriteLine($"Got ${workflowStatusList.Count} workflow statuses");
_logger.LogInformation($"Got ${workflowStatusList.Count} workflow statuses");
return workflowStatusList;
}

Expand Down Expand Up @@ -65,7 +68,7 @@ private static void GetWorkflowStatus(WorkflowResourceApi workflowClient, Concur
}
catch (ApiException e)
{
Console.WriteLine($"Failed to get workflow status, reason: {e}");
_logger.LogError($"Failed to get workflow status, reason: {e}");
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1 << attempt));
}
}
Expand All @@ -82,7 +85,7 @@ private static void StartWorkflow(WorkflowResourceApi workflowClient, Models.Sta
}
catch (ApiException e)
{
Console.WriteLine($"Failed to start workflow, reason: {e}");
_logger.LogError($"Failed to start workflow, reason: {e}");
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1 << attempt));
}
}
Expand Down
6 changes: 5 additions & 1 deletion Tests/Worker/WorkerTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using conductor.csharp.Client.Extensions;
using Conductor.Api;
using Conductor.Client.Extensions;
using Conductor.Client.Models;
using Conductor.Definition;
using Conductor.Definition.TaskType;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -19,10 +21,12 @@ public class WorkerTests
private const string TASK_DOMAIN = "taskDomain";

private readonly WorkflowResourceApi _workflowClient;
private readonly ILogger _logger;

public WorkerTests()
{
_workflowClient = ApiExtensions.GetClient<WorkflowResourceApi>();
_logger = ApplicationLogging.CreateLogger<WorkerTests>();
}

[Fact]
Expand Down Expand Up @@ -77,7 +81,7 @@ private async System.Threading.Tasks.Task ValidateWorkflowCompletion(params stri
if (workflowStatus.Status.Value != WorkflowStatus.StatusEnum.COMPLETED)
{
incompleteWorkflowCounter += 1;
Console.WriteLine($"Workflow not completed, workflowId: {workflowStatus.WorkflowId}");
_logger.LogInformation($"Workflow not completed, workflowId: {workflowStatus.WorkflowId}");
}
}
Assert.Equal(0, incompleteWorkflowCounter);
Expand Down
61 changes: 39 additions & 22 deletions csharp-examples/Runner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using conductor.csharp.Client.Extensions;
using Conductor.Client;
using Conductor.Client.Authentication;
using Conductor.Client.Extensions;
Expand All @@ -9,37 +10,53 @@ namespace csharp_examples;

public class Runner
{
public readonly ILogger _logger;

public Runner()
{
_logger = ApplicationLogging.CreateLogger<Runner>();
}

/// <summary>
/// Running multiple task as background services
/// </summary>
public async void StartTasks()
{
var key = Environment.GetEnvironmentVariable("KEY");
var secret = Environment.GetEnvironmentVariable("SECRET");
var url = Environment.GetEnvironmentVariable("CONDUCTOR_SERVER_URL");

var configuration = new Configuration
{
BasePath = url,
AuthenticationSettings = new OrkesAuthenticationSettings(key, secret)
};
var num = 5;
for (var i = 1; i <= num; i++)
try
{
var host = WorkflowTaskHost.CreateWorkerHost(configuration, LogLevel.Information,
new TestWorker("csharp_task_" + i));
var ct = new CancellationTokenSource();
try
var key = Environment.GetEnvironmentVariable("KEY");
var secret = Environment.GetEnvironmentVariable("SECRET");
var url = Environment.GetEnvironmentVariable("CONDUCTOR_SERVER_URL");

var configuration = new Configuration
{
await host.StartAsync(ct.Token);
}
catch (Exception e)
BasePath = url,
AuthenticationSettings = new OrkesAuthenticationSettings(key, secret)
};
var num = 5;
for (var i = 1; i <= num; i++)
{
Console.WriteLine(e);
throw;
var host = WorkflowTaskHost.CreateWorkerHost(configuration, LogLevel.Information,
new TestWorker("csharp_task_" + i));
var ct = new CancellationTokenSource();
try
{
await host.StartAsync(ct.Token);
}
catch (Exception e)
{
_logger.LogError(e.Message);
throw;
}
}
}

while (true) Thread.Sleep(TimeSpan.FromDays(1));
while (true)
Thread.Sleep(TimeSpan.FromDays(1)); // after 1 year will stop the service

}
catch (Exception e)
{
_logger.LogError($"{e.Message}");
}
}
}

0 comments on commit ae86f42

Please sign in to comment.