-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from conductor-sdk/add-worker-attribute
Added worker attribute
- Loading branch information
Showing
14 changed files
with
312 additions
and
194 deletions.
There are no files selected for viewing
12 changes: 6 additions & 6 deletions
12
Tests/Util/ApiUtil.cs → Conductor/Client/Extensions/ApiExtensions.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using Conductor.Api; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Conductor.Client.Extensions | ||
{ | ||
public class WorkflowExtensions | ||
{ | ||
private static int RETRY_ATTEMPT_LIMIT = 3; | ||
|
||
public static async Task<ConcurrentBag<string>> StartWorkflows(WorkflowResourceApi workflowClient, Models.StartWorkflowRequest startWorkflowRequest, int maxAllowedInParallel, int total) | ||
{ | ||
var workflowIds = new ConcurrentBag<string>(); | ||
await StartWorkflowBatch(workflowClient, startWorkflowRequest, total % maxAllowedInParallel, workflowIds); | ||
for (int i = 1; i * maxAllowedInParallel <= total; i += 1) | ||
{ | ||
await StartWorkflowBatch(workflowClient, startWorkflowRequest, maxAllowedInParallel, workflowIds); | ||
} | ||
Console.WriteLine($"Started {workflowIds.Count} workflows"); | ||
return workflowIds; | ||
} | ||
|
||
public static async Task<ConcurrentBag<Models.WorkflowStatus>> GetWorkflowStatusList(WorkflowResourceApi workflowClient, int maxAllowedInParallel, params string[] workflowIds) | ||
{ | ||
var workflowStatusList = new ConcurrentBag<Models.WorkflowStatus>(); | ||
for (int index = 0; index < workflowIds.Length; index += maxAllowedInParallel) | ||
{ | ||
await GetWorkflowStatusBatch(workflowClient, workflowStatusList, index, index + maxAllowedInParallel, workflowIds); | ||
} | ||
Console.WriteLine($"Got ${workflowStatusList.Count} workflow statuses"); | ||
return workflowStatusList; | ||
} | ||
|
||
private static async Task GetWorkflowStatusBatch(WorkflowResourceApi workflowClient, ConcurrentBag<Models.WorkflowStatus> workflowStatusList, int startIndex, int finishIndex, params string[] workflowIds) | ||
{ | ||
var threads = new List<Task>(); | ||
for (int index = Math.Max(0, startIndex); index < Math.Min(workflowIds.Length, finishIndex); index += 1) | ||
{ | ||
var workflowId = workflowIds[index]; | ||
threads.Add(Task.Run(() => GetWorkflowStatus(workflowClient, workflowStatusList, workflowId))); | ||
} | ||
await Task.WhenAll(threads); | ||
} | ||
|
||
private static async Task StartWorkflowBatch(WorkflowResourceApi workflowClient, Models.StartWorkflowRequest startWorkflowRequest, int quantity, ConcurrentBag<string> workflowIds) | ||
{ | ||
var threads = new List<Task>(); | ||
for (int counter = 0; counter < quantity; counter += 1) | ||
{ | ||
threads.Add(Task.Run(() => StartWorkflow(workflowClient, startWorkflowRequest, workflowIds))); | ||
} | ||
await Task.WhenAll(threads); | ||
} | ||
|
||
private static void GetWorkflowStatus(WorkflowResourceApi workflowClient, ConcurrentBag<Models.WorkflowStatus> workflowStatusList, string workflowId) | ||
{ | ||
for (int attempt = 0; attempt < RETRY_ATTEMPT_LIMIT; attempt += 1) | ||
{ | ||
try | ||
{ | ||
workflowStatusList.Add(workflowClient.GetWorkflowStatusSummary(workflowId)); | ||
return; | ||
} | ||
catch (ApiException e) | ||
{ | ||
Console.WriteLine($"Failed to get workflow status, reason: {e}"); | ||
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1 << attempt)); | ||
} | ||
} | ||
} | ||
|
||
private static void StartWorkflow(WorkflowResourceApi workflowClient, Models.StartWorkflowRequest startWorkflowRequest, ConcurrentBag<string> workflowIds) | ||
{ | ||
for (int attempt = 0; attempt < RETRY_ATTEMPT_LIMIT; attempt += 1) | ||
{ | ||
try | ||
{ | ||
workflowIds.Add(workflowClient.StartWorkflow(startWorkflowRequest)); | ||
return; | ||
} | ||
catch (ApiException e) | ||
{ | ||
Console.WriteLine($"Failed to start workflow, reason: {e}"); | ||
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1 << attempt)); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,29 @@ | ||
using Conductor.Client.Interfaces; | ||
using Conductor.Client.Models; | ||
using System.Reflection; | ||
|
||
namespace Conductor.Client.Worker | ||
{ | ||
public class GenericWorker : IWorkflowTask | ||
{ | ||
public string TaskType { get; } | ||
public WorkflowTaskExecutorConfiguration WorkerSettings { get; } | ||
|
||
private readonly object _workerInstance; | ||
private readonly MethodInfo _executeTaskMethod; | ||
|
||
public GenericWorker(string taskType, WorkflowTaskExecutorConfiguration workerSettings, MethodInfo executeTaskMethod, object workerInstance = null) | ||
{ | ||
TaskType = taskType; | ||
WorkerSettings = workerSettings; | ||
_executeTaskMethod = executeTaskMethod; | ||
_workerInstance = workerInstance; | ||
} | ||
|
||
public TaskResult Execute(Task task) | ||
{ | ||
var taskResult = _executeTaskMethod.Invoke(_workerInstance, new object[] { task }); | ||
return (TaskResult)taskResult; | ||
} | ||
} | ||
} |
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,28 @@ | ||
using System; | ||
|
||
namespace Conductor.Client.Worker | ||
{ | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
public class WorkerTask : Attribute | ||
{ | ||
public string TaskType { get; } | ||
public WorkflowTaskExecutorConfiguration WorkerSettings { get; set; } | ||
|
||
public WorkerTask() | ||
{ | ||
WorkerSettings = new WorkflowTaskExecutorConfiguration(); | ||
} | ||
|
||
public WorkerTask(string taskType, int batchSize, string domain, int pollIntervalMs, string workerId) | ||
{ | ||
TaskType = taskType; | ||
WorkerSettings = new WorkflowTaskExecutorConfiguration | ||
{ | ||
BatchSize = batchSize, | ||
Domain = domain, | ||
PollInterval = TimeSpan.FromMilliseconds(pollIntervalMs), | ||
WorkerId = workerId, | ||
}; | ||
} | ||
} | ||
} |
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.