From 86cf22159eaf0f0848a063189ee10c66c46f609d Mon Sep 17 00:00:00 2001 From: legalles Date: Mon, 21 Oct 2024 09:21:24 +0200 Subject: [PATCH] style: use file-scope namespace and primary constructor --- .../Impl/Builder/CamundaCloudClientBuilder.cs | 168 +++++++++-------- .../Impl/Builder/CamundaCloudTokenProvider.cs | 175 +++++++++--------- Client/Impl/Commands/ActivateJobsCommand.cs | 164 ++++++++-------- .../Commands/CancelProcessInstanceCommand.cs | 54 +++--- Client/Impl/Commands/CompleteJobCommand.cs | 61 +++--- .../Commands/CreateProcessInstanceCommand.cs | 122 ++++++------ .../CreateProcessInstanceCommandWithResult.cs | 92 +++++---- Client/Impl/Commands/DeployResourceCommand.cs | 126 ++++++------- .../Impl/Commands/EvaluateDecisionCommand.cs | 16 +- Client/Impl/Commands/FailJobCommand.cs | 91 ++++----- Client/Impl/Commands/JobActivator.cs | 64 +++---- .../Commands/ModifyProcessInstanceCommand.cs | 27 +-- Client/Impl/Commands/PublishMessageCommand.cs | 97 +++++----- .../Impl/Commands/ResolveIncidentCommand.cs | 52 +++--- Client/Impl/Commands/SetVariablesCommand.cs | 72 ++++--- Client/Impl/Commands/ThrowErrorCommand.cs | 81 ++++---- Client/Impl/Commands/TimeSpanExtensions.cs | 11 +- .../Impl/Commands/TopologyRequestCommand.cs | 46 ++--- .../Impl/Commands/UpdateJobTimeoutCommand.cs | 18 +- Client/Impl/Commands/UpdateRetriesCommand.cs | 59 +++--- 20 files changed, 728 insertions(+), 868 deletions(-) diff --git a/Client/Impl/Builder/CamundaCloudClientBuilder.cs b/Client/Impl/Builder/CamundaCloudClientBuilder.cs index f0990f39..db25f1b2 100644 --- a/Client/Impl/Builder/CamundaCloudClientBuilder.cs +++ b/Client/Impl/Builder/CamundaCloudClientBuilder.cs @@ -1,115 +1,113 @@ using System; using Microsoft.Extensions.Logging; using Zeebe.Client.Api.Builder; -using Zeebe.Client.Impl.Builder; -namespace Zeebe.Client.Impl.Builder +namespace Zeebe.Client.Impl.Builder; + +public class CamundaCloudClientBuilder : ICamundaCloudClientBuilder, ICamundaCloudClientBuilderStep1, ICamundaCloudClientBuilderStep2, ICamundaCloudClientBuilderFinalStep { - public class CamundaCloudClientBuilder : ICamundaCloudClientBuilder, ICamundaCloudClientBuilderStep1, ICamundaCloudClientBuilderStep2, ICamundaCloudClientBuilderFinalStep + private const string ZeebeAddressEnvVar = "ZEEBE_ADDRESS"; + private const string ZeebeClientIdEnvVar = "ZEEBE_CLIENT_ID"; + private const string ZeebeClientSecretEnvVar = "ZEEBE_CLIENT_SECRET"; + private const string ZeebeAuthServerEnvVar = "ZEEBE_AUTHORIZATION_SERVER_URL"; + + private readonly CamundaCloudTokenProviderBuilder camundaCloudTokenProviderBuilder; + private string gatewayAddress; + private ILoggerFactory loggerFactory; + + private CamundaCloudClientBuilder() { - private const string ZeebeAddressEnvVar = "ZEEBE_ADDRESS"; - private const string ZeebeClientIdEnvVar = "ZEEBE_CLIENT_ID"; - private const string ZeebeClientSecretEnvVar = "ZEEBE_CLIENT_SECRET"; - private const string ZeebeAuthServerEnvVar = "ZEEBE_AUTHORIZATION_SERVER_URL"; + camundaCloudTokenProviderBuilder = CamundaCloudTokenProvider.Builder(); + } - private readonly CamundaCloudTokenProviderBuilder camundaCloudTokenProviderBuilder; - private string gatewayAddress; - private ILoggerFactory loggerFactory; + public static ICamundaCloudClientBuilder Builder() + { + return new CamundaCloudClientBuilder(); + } - private CamundaCloudClientBuilder() - { - camundaCloudTokenProviderBuilder = CamundaCloudTokenProvider.Builder(); - } + public ICamundaCloudClientBuilderStep1 UseClientId(string clientId) + { + camundaCloudTokenProviderBuilder.UseClientId(clientId); + return this; + } - public static ICamundaCloudClientBuilder Builder() - { - return new CamundaCloudClientBuilder(); - } + public ICamundaCloudClientBuilderStep2 UseClientSecret(string clientSecret) + { + camundaCloudTokenProviderBuilder.UseClientSecret(clientSecret); + return this; + } + + public ICamundaCloudClientBuilderFinalStep UseContactPoint(string contactPoint) + { + _ = contactPoint ?? throw new ArgumentNullException(nameof(contactPoint)); - public ICamundaCloudClientBuilderStep1 UseClientId(string clientId) + if (!contactPoint.EndsWith(":443")) { - camundaCloudTokenProviderBuilder.UseClientId(clientId); - return this; + gatewayAddress = contactPoint + ":443"; + camundaCloudTokenProviderBuilder.UseAudience(contactPoint); } - - public ICamundaCloudClientBuilderStep2 UseClientSecret(string clientSecret) + else { - camundaCloudTokenProviderBuilder.UseClientSecret(clientSecret); - return this; + gatewayAddress = contactPoint; + camundaCloudTokenProviderBuilder.UseAudience(contactPoint.Replace(":443", "")); } - public ICamundaCloudClientBuilderFinalStep UseContactPoint(string contactPoint) - { - _ = contactPoint ?? throw new ArgumentNullException(nameof(contactPoint)); - - if (!contactPoint.EndsWith(":443")) - { - gatewayAddress = contactPoint + ":443"; - camundaCloudTokenProviderBuilder.UseAudience(contactPoint); - } - else - { - gatewayAddress = contactPoint; - camundaCloudTokenProviderBuilder.UseAudience(contactPoint.Replace(":443", "")); - } + return this; + } - return this; - } + public ICamundaCloudClientBuilderFinalStep UseLoggerFactory(ILoggerFactory loggerFactory) + { + this.loggerFactory = loggerFactory; + camundaCloudTokenProviderBuilder.UseLoggerFactory(this.loggerFactory); + return this; + } - public ICamundaCloudClientBuilderFinalStep UseLoggerFactory(ILoggerFactory loggerFactory) + public ICamundaCloudClientBuilderFinalStep UseAuthServer(string url) + { + if (url is null) { - this.loggerFactory = loggerFactory; - camundaCloudTokenProviderBuilder.UseLoggerFactory(this.loggerFactory); + // use default return this; } - public ICamundaCloudClientBuilderFinalStep UseAuthServer(string url) - { - if (url is null) - { - // use default - return this; - } - - camundaCloudTokenProviderBuilder.UseAuthServer(url); - return this; - } + camundaCloudTokenProviderBuilder.UseAuthServer(url); + return this; + } - public ICamundaCloudClientBuilderFinalStep UsePersistedStoragePath(string path) + public ICamundaCloudClientBuilderFinalStep UsePersistedStoragePath(string path) + { + if (path is null) { - if (path is null) - { - // use default - return this; - } - - camundaCloudTokenProviderBuilder.UsePath(path); + // use default return this; } - private string GetFromEnv(string key) - { - char[] charsToTrim = { ' ', '\'' }; - return Environment.GetEnvironmentVariable(key)?.Trim(charsToTrim); - } + camundaCloudTokenProviderBuilder.UsePath(path); + return this; + } - public ICamundaCloudClientBuilderFinalStep FromEnv() - { - this.UseClientId(GetFromEnv(ZeebeClientIdEnvVar)) - .UseClientSecret(GetFromEnv(ZeebeClientSecretEnvVar)) - .UseContactPoint(GetFromEnv(ZeebeAddressEnvVar)) - .UseAuthServer(GetFromEnv(ZeebeAuthServerEnvVar)); - return this; - } + private string GetFromEnv(string key) + { + char[] charsToTrim = [' ', '\'']; + return Environment.GetEnvironmentVariable(key)?.Trim(charsToTrim); + } - public IZeebeClient Build() - { - return ZeebeClient.Builder() - .UseLoggerFactory(loggerFactory) - .UseGatewayAddress(gatewayAddress) - .UseTransportEncryption() - .UseAccessTokenSupplier(camundaCloudTokenProviderBuilder.Build()) - .Build(); - } + public ICamundaCloudClientBuilderFinalStep FromEnv() + { + UseClientId(GetFromEnv(ZeebeClientIdEnvVar)) + .UseClientSecret(GetFromEnv(ZeebeClientSecretEnvVar)) + .UseContactPoint(GetFromEnv(ZeebeAddressEnvVar)) + .UseAuthServer(GetFromEnv(ZeebeAuthServerEnvVar)); + return this; + } + + public IZeebeClient Build() + { + return ZeebeClient.Builder() + .UseLoggerFactory(loggerFactory) + .UseGatewayAddress(gatewayAddress) + .UseTransportEncryption() + .UseAccessTokenSupplier(camundaCloudTokenProviderBuilder.Build()) + .Build(); } } \ No newline at end of file diff --git a/Client/Impl/Builder/CamundaCloudTokenProvider.cs b/Client/Impl/Builder/CamundaCloudTokenProvider.cs index d30352f7..bf5905f4 100644 --- a/Client/Impl/Builder/CamundaCloudTokenProvider.cs +++ b/Client/Impl/Builder/CamundaCloudTokenProvider.cs @@ -8,105 +8,104 @@ using Zeebe.Client.Api.Builder; using Zeebe.Client.Impl.Misc; -namespace Zeebe.Client.Impl.Builder +namespace Zeebe.Client.Impl.Builder; + +public class CamundaCloudTokenProvider : IAccessTokenSupplier, IDisposable { - public class CamundaCloudTokenProvider : IAccessTokenSupplier, IDisposable - { - private static readonly string ZeebeRootPath = - Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".zeebe"); + private static readonly string ZeebeRootPath = + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".zeebe"); - private readonly ILogger logger; - private readonly string authServer; - private readonly string clientId; - private readonly string clientSecret; - private readonly string audience; - private HttpClient httpClient; - private HttpMessageHandler httpMessageHandler; - private readonly PersistedAccessTokenCache persistedAccessTokenCache; + private readonly ILogger logger; + private readonly string authServer; + private readonly string clientId; + private readonly string clientSecret; + private readonly string audience; + private HttpClient httpClient; + private HttpMessageHandler httpMessageHandler; + private readonly PersistedAccessTokenCache persistedAccessTokenCache; - internal CamundaCloudTokenProvider( - string authServer, - string clientId, - string clientSecret, - string audience, - string path = null, - ILoggerFactory loggerFactory = null) - { - persistedAccessTokenCache = new PersistedAccessTokenCache(path ?? ZeebeRootPath, FetchAccessToken, loggerFactory?.CreateLogger()); - this.logger = loggerFactory?.CreateLogger(); - this.authServer = authServer; - this.clientId = clientId; - this.clientSecret = clientSecret; - this.audience = audience; - httpClient = new HttpClient(new HttpClientHandler(), disposeHandler: false); - } + internal CamundaCloudTokenProvider( + string authServer, + string clientId, + string clientSecret, + string audience, + string path = null, + ILoggerFactory loggerFactory = null) + { + persistedAccessTokenCache = new PersistedAccessTokenCache(path ?? ZeebeRootPath, FetchAccessToken, loggerFactory?.CreateLogger()); + logger = loggerFactory?.CreateLogger(); + this.authServer = authServer; + this.clientId = clientId; + this.clientSecret = clientSecret; + this.audience = audience; + httpClient = new HttpClient(new HttpClientHandler(), disposeHandler: false); + } - public static CamundaCloudTokenProviderBuilder Builder() - { - return new CamundaCloudTokenProviderBuilder(); - } + public static CamundaCloudTokenProviderBuilder Builder() + { + return new CamundaCloudTokenProviderBuilder(); + } - internal void SetHttpMessageHandler(HttpMessageHandler handler) - { - httpMessageHandler = handler; - httpClient = new HttpClient(handler); - } + internal void SetHttpMessageHandler(HttpMessageHandler handler) + { + httpMessageHandler = handler; + httpClient = new HttpClient(handler); + } - private async Task FetchAccessToken() - { - // Requesting the token is similar to this: - // curl -X POST https://login.cloud.ultrawombat.com/oauth/token \ - // -H "Content-Type: application/x-www-form-urlencoded" \ - // -d "client_id=213131&client_secret=12-23~oU.321&audience=zeebe.ultrawombat.com&grant_type=client_credentials" - // - // alternative is json - // curl --request POST \ - // --url https://login.cloud.[ultrawombat.com | camunda.io]/oauth/token \ - // --header 'content-type: application/json' \ - // --data '{"client_id":"${clientId}","client_secret":"${clientSecret}","audience":"${audience}","grant_type":"client_credentials"}' + private async Task FetchAccessToken() + { + // Requesting the token is similar to this: + // curl -X POST https://login.cloud.ultrawombat.com/oauth/token \ + // -H "Content-Type: application/x-www-form-urlencoded" \ + // -d "client_id=213131&client_secret=12-23~oU.321&audience=zeebe.ultrawombat.com&grant_type=client_credentials" + // + // alternative is json + // curl --request POST \ + // --url https://login.cloud.[ultrawombat.com | camunda.io]/oauth/token \ + // --header 'content-type: application/json' \ + // --data '{"client_id":"${clientId}","client_secret":"${clientSecret}","audience":"${audience}","grant_type":"client_credentials"}' - var formContent = BuildRequestAccessTokenContent(); - var httpResponseMessage = await httpClient.PostAsync(authServer, formContent); + var formContent = BuildRequestAccessTokenContent(); + var httpResponseMessage = await httpClient.PostAsync(authServer, formContent); - // Code expects the following result: - // - // { - // "access_token":"MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3", - // "token_type":"bearer", - // "expires_in":3600, - // "refresh_token":"IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk", - // "scope":"create" - // } - // - // Defined here https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/ - var result = await httpResponseMessage.Content.ReadAsStringAsync(); - var token = AccessToken.FromJson(result); - logger?.LogDebug("Received access token for {Audience}", audience); - return token; - } + // Code expects the following result: + // + // { + // "access_token":"MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3", + // "token_type":"bearer", + // "expires_in":3600, + // "refresh_token":"IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk", + // "scope":"create" + // } + // + // Defined here https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/ + var result = await httpResponseMessage.Content.ReadAsStringAsync(); + var token = AccessToken.FromJson(result); + logger?.LogDebug("Received access token for {Audience}", audience); + return token; + } - private FormUrlEncodedContent BuildRequestAccessTokenContent() + private FormUrlEncodedContent BuildRequestAccessTokenContent() + { + var formContent = new FormUrlEncodedContent(new[] { - var formContent = new FormUrlEncodedContent(new[] - { - new KeyValuePair("client_id", clientId), - new KeyValuePair("client_secret", clientSecret), - new KeyValuePair("audience", audience), - new KeyValuePair("grant_type", "client_credentials") - }); - return formContent; - } + new KeyValuePair("client_id", clientId), + new KeyValuePair("client_secret", clientSecret), + new KeyValuePair("audience", audience), + new KeyValuePair("grant_type", "client_credentials") + }); + return formContent; + } - public void Dispose() - { - httpClient.Dispose(); - httpMessageHandler.Dispose(); - } + public void Dispose() + { + httpClient.Dispose(); + httpMessageHandler.Dispose(); + } - public async Task GetAccessTokenForRequestAsync(string authUri = null, - CancellationToken cancellationToken = default(CancellationToken)) - { - return await persistedAccessTokenCache.Get(audience); - } + public async Task GetAccessTokenForRequestAsync(string authUri = null, + CancellationToken cancellationToken = default(CancellationToken)) + { + return await persistedAccessTokenCache.Get(audience); } } \ No newline at end of file diff --git a/Client/Impl/Commands/ActivateJobsCommand.cs b/Client/Impl/Commands/ActivateJobsCommand.cs index 6890aa64..6985042c 100644 --- a/Client/Impl/Commands/ActivateJobsCommand.cs +++ b/Client/Impl/Commands/ActivateJobsCommand.cs @@ -8,92 +8,84 @@ using Zeebe.Client.Api.Responses; using static GatewayProtocol.Gateway; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +internal class ActivateJobsCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) + : IActivateJobsCommandStep1, IActivateJobsCommandStep2, IActivateJobsCommandStep3 { - internal class ActivateJobsCommand : IActivateJobsCommandStep1, IActivateJobsCommandStep2, IActivateJobsCommandStep3 + private readonly JobActivator activator = new (client); + public ActivateJobsRequest Request { get; } = new (); + + public IActivateJobsCommandStep2 JobType(string jobType) + { + Request.Type = jobType; + return this; + } + + public IActivateJobsCommandStep3 MaxJobsToActivate(int maxJobsToActivate) + { + Request.MaxJobsToActivate = maxJobsToActivate; + return this; + } + + public IActivateJobsCommandStep3 FetchVariables(IList fetchVariables) + { + Request.FetchVariable.AddRange(fetchVariables); + return this; + } + + public IActivateJobsCommandStep3 FetchVariables(params string[] fetchVariables) + { + Request.FetchVariable.AddRange(fetchVariables); + return this; + } + + public IActivateJobsCommandStep3 Timeout(TimeSpan timeout) + { + Request.Timeout = (long)timeout.TotalMilliseconds; + return this; + } + + public IActivateJobsCommandStep3 PollingTimeout(TimeSpan pollingTimeout) + { + Request.RequestTimeout = (long)pollingTimeout.TotalMilliseconds; + return this; + } + + public IActivateJobsCommandStep3 WorkerName(string workerName) + { + Request.Worker = workerName; + return this; + } + + public IActivateJobsCommandStep3 TenantIds(IList tenantIds) + { + Request.TenantIds.AddRange(tenantIds); + + return this; + } + + public IActivateJobsCommandStep3 TenantIds(params string[] tenantIds) + { + Request.TenantIds.AddRange(tenantIds); + + return this; + } + + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var activateJobsResponses = new Responses.ActivateJobsResponses(); + await activator.SendActivateRequest(Request, response => Task.Run(() => activateJobsResponses.Add(response), token), timeout?.FromUtcNow(), token); + return activateJobsResponses; + } + + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } + + public async Task SendWithRetry(TimeSpan? timespan, CancellationToken cancellationToken = default) { - private readonly JobActivator activator; - public ActivateJobsRequest Request { get; } - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public ActivateJobsCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) - { - this.asyncRetryStrategy = asyncRetryStrategy; - activator = new JobActivator(client); - Request = new ActivateJobsRequest(); - } - - public IActivateJobsCommandStep2 JobType(string jobType) - { - Request.Type = jobType; - return this; - } - - public IActivateJobsCommandStep3 MaxJobsToActivate(int maxJobsToActivate) - { - Request.MaxJobsToActivate = maxJobsToActivate; - return this; - } - - public IActivateJobsCommandStep3 FetchVariables(IList fetchVariables) - { - Request.FetchVariable.AddRange(fetchVariables); - return this; - } - - public IActivateJobsCommandStep3 FetchVariables(params string[] fetchVariables) - { - Request.FetchVariable.AddRange(fetchVariables); - return this; - } - - public IActivateJobsCommandStep3 Timeout(TimeSpan timeout) - { - Request.Timeout = (long)timeout.TotalMilliseconds; - return this; - } - - public IActivateJobsCommandStep3 PollingTimeout(TimeSpan pollingTimeout) - { - Request.RequestTimeout = (long)pollingTimeout.TotalMilliseconds; - return this; - } - - public IActivateJobsCommandStep3 WorkerName(string workerName) - { - Request.Worker = workerName; - return this; - } - - public IActivateJobsCommandStep3 TenantIds(IList tenantIds) - { - Request.TenantIds.AddRange(tenantIds); - - return this; - } - - public IActivateJobsCommandStep3 TenantIds(params string[] tenantIds) - { - Request.TenantIds.AddRange(tenantIds); - - return this; - } - - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var activateJobsResponses = new Responses.ActivateJobsResponses(); - await activator.SendActivateRequest(Request, response => Task.Run(() => activateJobsResponses.Add(response), token), timeout?.FromUtcNow(), token); - return activateJobsResponses; - } - - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } - - public async Task SendWithRetry(TimeSpan? timespan, CancellationToken cancellationToken = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, cancellationToken)); - } + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, cancellationToken)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/CancelProcessInstanceCommand.cs b/Client/Impl/Commands/CancelProcessInstanceCommand.cs index e450db56..cd4af04f 100644 --- a/Client/Impl/Commands/CancelProcessInstanceCommand.cs +++ b/Client/Impl/Commands/CancelProcessInstanceCommand.cs @@ -7,39 +7,33 @@ using Zeebe.Client.Api.Responses; using CancelProcessInstanceResponse = Zeebe.Client.Impl.Responses.CancelProcessInstanceResponse; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public class CancelProcessInstanceCommand( + Gateway.GatewayClient client, + IAsyncRetryStrategy asyncRetryStrategy, + long processInstanceKey) + : ICancelProcessInstanceCommandStep1 { - public class CancelProcessInstanceCommand : ICancelProcessInstanceCommandStep1 + private readonly CancelProcessInstanceRequest request = new() { - private readonly CancelProcessInstanceRequest request; - private readonly Gateway.GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public CancelProcessInstanceCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long processInstanceKey) - { - request = new CancelProcessInstanceRequest - { - ProcessInstanceKey = processInstanceKey - }; - this.client = client; - this.asyncRetryStrategy = asyncRetryStrategy; - } + ProcessInstanceKey = processInstanceKey + }; - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = client.CancelProcessInstanceAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - await asyncReply.ResponseAsync; - return new CancelProcessInstanceResponse(); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.CancelProcessInstanceAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + await asyncReply.ResponseAsync; + return new CancelProcessInstanceResponse(); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/CompleteJobCommand.cs b/Client/Impl/Commands/CompleteJobCommand.cs index ac29e5e9..36f122d6 100644 --- a/Client/Impl/Commands/CompleteJobCommand.cs +++ b/Client/Impl/Commands/CompleteJobCommand.cs @@ -22,45 +22,36 @@ using Zeebe.Client.Api.Responses; using static GatewayProtocol.Gateway; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +internal class CompleteJobCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) + : ICompleteJobCommandStep1 { - internal class CompleteJobCommand : ICompleteJobCommandStep1 + private readonly CompleteJobRequest request = new() { - private readonly CompleteJobRequest request; - private readonly GatewayClient gatewayClient; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public CompleteJobCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) - { - gatewayClient = client; - request = new CompleteJobRequest - { - JobKey = jobKey - }; - this.asyncRetryStrategy = asyncRetryStrategy; - } + JobKey = jobKey + }; - public ICompleteJobCommandStep1 Variables(string variables) - { - request.Variables = variables; - return this; - } + public ICompleteJobCommandStep1 Variables(string variables) + { + request.Variables = variables; + return this; + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = gatewayClient.CompleteJobAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - await asyncReply.ResponseAsync; - return new Responses.CompleteJobResponse(); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.CompleteJobAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + await asyncReply.ResponseAsync; + return new Responses.CompleteJobResponse(); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/CreateProcessInstanceCommand.cs b/Client/Impl/Commands/CreateProcessInstanceCommand.cs index f0a30bc0..ce8a272e 100644 --- a/Client/Impl/Commands/CreateProcessInstanceCommand.cs +++ b/Client/Impl/Commands/CreateProcessInstanceCommand.cs @@ -7,82 +7,72 @@ using Zeebe.Client.Api.Responses; using Zeebe.Client.Impl.Responses; -namespace Zeebe.Client.Impl.Commands -{ - public class CreateProcessInstanceCommand - : ICreateProcessInstanceCommandStep1, - ICreateProcessInstanceCommandStep2, - ICreateProcessInstanceCommandStep3 - { - private const int LatestVersionValue = -1; +namespace Zeebe.Client.Impl.Commands; - private readonly CreateProcessInstanceRequest request; - private readonly Gateway.GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; +public class CreateProcessInstanceCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) + : ICreateProcessInstanceCommandStep1, + ICreateProcessInstanceCommandStep2, + ICreateProcessInstanceCommandStep3 +{ + private const int LatestVersionValue = -1; - public CreateProcessInstanceCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) - { - this.client = client; - request = new CreateProcessInstanceRequest(); - this.asyncRetryStrategy = asyncRetryStrategy; - } + private readonly CreateProcessInstanceRequest request = new (); - public ICreateProcessInstanceCommandStep2 BpmnProcessId(string bpmnProcessId) - { - request.BpmnProcessId = bpmnProcessId; - return this; - } + public ICreateProcessInstanceCommandStep2 BpmnProcessId(string bpmnProcessId) + { + request.BpmnProcessId = bpmnProcessId; + return this; + } - public ICreateProcessInstanceCommandStep3 ProcessDefinitionKey(long processDefinitionKey) - { - request.ProcessDefinitionKey = processDefinitionKey; - return this; - } + public ICreateProcessInstanceCommandStep3 ProcessDefinitionKey(long processDefinitionKey) + { + request.ProcessDefinitionKey = processDefinitionKey; + return this; + } - public ICreateProcessInstanceCommandStep3 Version(int version) - { - request.Version = version; - return this; - } + public ICreateProcessInstanceCommandStep3 Version(int version) + { + request.Version = version; + return this; + } - public ICreateProcessInstanceCommandStep3 LatestVersion() - { - request.Version = LatestVersionValue; - return this; - } + public ICreateProcessInstanceCommandStep3 LatestVersion() + { + request.Version = LatestVersionValue; + return this; + } - public ICreateProcessInstanceCommandStep3 Variables(string variables) - { - request.Variables = variables; - return this; - } + public ICreateProcessInstanceCommandStep3 Variables(string variables) + { + request.Variables = variables; + return this; + } - public ICreateProcessInstanceCommandStep3 AddTenantId(string tenantId) - { - request.TenantId = tenantId; - return this; - } + public ICreateProcessInstanceCommandStep3 AddTenantId(string tenantId) + { + request.TenantId = tenantId; + return this; + } - public ICreateProcessInstanceWithResultCommandStep1 WithResult() - { - return new CreateProcessInstanceCommandWithResult(client, asyncRetryStrategy, request); - } + public ICreateProcessInstanceWithResultCommandStep1 WithResult() + { + return new CreateProcessInstanceCommandWithResult(client, asyncRetryStrategy, request); + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = client.CreateProcessInstanceAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - var response = await asyncReply.ResponseAsync; - return new ProcessInstanceResponse(response); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.CreateProcessInstanceAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + var response = await asyncReply.ResponseAsync; + return new ProcessInstanceResponse(response); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/CreateProcessInstanceCommandWithResult.cs b/Client/Impl/Commands/CreateProcessInstanceCommandWithResult.cs index a6cc122d..c8b53af4 100644 --- a/Client/Impl/Commands/CreateProcessInstanceCommandWithResult.cs +++ b/Client/Impl/Commands/CreateProcessInstanceCommandWithResult.cs @@ -8,62 +8,56 @@ using Zeebe.Client.Api.Responses; using Zeebe.Client.Impl.Responses; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +/// +public class CreateProcessInstanceCommandWithResult( + Gateway.GatewayClient client, + IAsyncRetryStrategy asyncRetryStrategy, + CreateProcessInstanceRequest createRequest) + : ICreateProcessInstanceWithResultCommandStep1 { - /// - public class CreateProcessInstanceCommandWithResult : ICreateProcessInstanceWithResultCommandStep1 - { - private static readonly long DefaultGatewayBrokerTimeoutMillisecond = 20 * 1000; - private static readonly long DefaultTimeoutAdditionMillisecond = 10 * 1000; - - private readonly CreateProcessInstanceWithResultRequest createWithResultRequest; - private readonly Gateway.GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; + private static readonly long DefaultGatewayBrokerTimeoutMillisecond = 20 * 1000; + private static readonly long DefaultTimeoutAdditionMillisecond = 10 * 1000; - public CreateProcessInstanceCommandWithResult(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, CreateProcessInstanceRequest createRequest) - { - this.client = client; - createWithResultRequest = new CreateProcessInstanceWithResultRequest { Request = createRequest }; - this.asyncRetryStrategy = asyncRetryStrategy; - } + private readonly CreateProcessInstanceWithResultRequest createWithResultRequest = new() { Request = createRequest }; - /// - public ICreateProcessInstanceWithResultCommandStep1 FetchVariables(IList fetchVariables) - { - createWithResultRequest.FetchVariables.AddRange(fetchVariables); - return this; - } + /// + public ICreateProcessInstanceWithResultCommandStep1 FetchVariables(IList fetchVariables) + { + createWithResultRequest.FetchVariables.AddRange(fetchVariables); + return this; + } - /// - public ICreateProcessInstanceWithResultCommandStep1 FetchVariables(params string[] fetchVariables) - { - createWithResultRequest.FetchVariables.AddRange(fetchVariables); - return this; - } + /// + public ICreateProcessInstanceWithResultCommandStep1 FetchVariables(params string[] fetchVariables) + { + createWithResultRequest.FetchVariables.AddRange(fetchVariables); + return this; + } - /// - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - // this timeout will be used for the Gateway-Broker communication - createWithResultRequest.RequestTimeout = (long)(timeout?.TotalMilliseconds ?? DefaultGatewayBrokerTimeoutMillisecond); + /// + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + // this timeout will be used for the Gateway-Broker communication + createWithResultRequest.RequestTimeout = (long)(timeout?.TotalMilliseconds ?? DefaultGatewayBrokerTimeoutMillisecond); - // this is the timeout between client and gateway - var clientDeadline = TimeSpan.FromMilliseconds(createWithResultRequest.RequestTimeout + - DefaultTimeoutAdditionMillisecond).FromUtcNow(); + // this is the timeout between client and gateway + var clientDeadline = TimeSpan.FromMilliseconds(createWithResultRequest.RequestTimeout + + DefaultTimeoutAdditionMillisecond).FromUtcNow(); - var asyncReply = client.CreateProcessInstanceWithResultAsync(createWithResultRequest, deadline: clientDeadline, cancellationToken: token); - var response = await asyncReply.ResponseAsync; - return new ProcessInstanceResultResponse(response); - } + var asyncReply = client.CreateProcessInstanceWithResultAsync(createWithResultRequest, deadline: clientDeadline, cancellationToken: token); + var response = await asyncReply.ResponseAsync; + return new ProcessInstanceResultResponse(response); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/DeployResourceCommand.cs b/Client/Impl/Commands/DeployResourceCommand.cs index d947efdb..fd6b40d7 100644 --- a/Client/Impl/Commands/DeployResourceCommand.cs +++ b/Client/Impl/Commands/DeployResourceCommand.cs @@ -23,88 +23,78 @@ using Zeebe.Client.Api.Commands; using Zeebe.Client.Api.Misc; using Zeebe.Client.Api.Responses; -using Zeebe.Client.Impl.Responses; using DeployResourceResponse = Zeebe.Client.Impl.Responses.DeployResourceResponse; -namespace Zeebe.Client.Impl.Commands -{ - public class DeployResourceCommand : IDeployResourceCommandBuilderStep2 - { - private readonly Gateway.GatewayClient gatewayClient; - private readonly DeployResourceRequest request; - private readonly IAsyncRetryStrategy asyncRetryStrategy; +namespace Zeebe.Client.Impl.Commands; - public DeployResourceCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) - { - gatewayClient = client; - this.asyncRetryStrategy = asyncRetryStrategy; - request = new DeployResourceRequest(); - } +public class DeployResourceCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) + : IDeployResourceCommandBuilderStep2 +{ + private readonly DeployResourceRequest request = new (); - public IDeployResourceCommandBuilderStep2 AddResourceBytes(byte[] resourceBytes, string resourceName) - { - AddResource(ByteString.CopyFrom(resourceBytes), resourceName); + public IDeployResourceCommandBuilderStep2 AddResourceBytes(byte[] resourceBytes, string resourceName) + { + AddResource(ByteString.CopyFrom(resourceBytes), resourceName); - return this; - } + return this; + } - public IDeployResourceCommandBuilderStep2 AddResourceFile(string filename) - { - var text = File.ReadAllText(filename); - AddResourceStringUtf8(text, filename); - return this; - } + public IDeployResourceCommandBuilderStep2 AddResourceFile(string filename) + { + var text = File.ReadAllText(filename); + AddResourceStringUtf8(text, filename); + return this; + } - public IDeployResourceCommandBuilderStep2 AddResourceStream(Stream resourceStream, string resourceName) - { - AddResource(ByteString.FromStream(resourceStream), resourceName); - return this; - } + public IDeployResourceCommandBuilderStep2 AddResourceStream(Stream resourceStream, string resourceName) + { + AddResource(ByteString.FromStream(resourceStream), resourceName); + return this; + } - public IDeployResourceCommandBuilderStep2 AddResourceString(string resourceString, Encoding encoding, string resourceName) - { - AddResource(ByteString.CopyFrom(resourceString, encoding), resourceName); - return this; - } + public IDeployResourceCommandBuilderStep2 AddResourceString(string resourceString, Encoding encoding, string resourceName) + { + AddResource(ByteString.CopyFrom(resourceString, encoding), resourceName); + return this; + } - public IDeployResourceCommandBuilderStep2 AddResourceStringUtf8(string resourceString, string resourceName) - { - AddResource(ByteString.CopyFromUtf8(resourceString), resourceName); - return this; - } + public IDeployResourceCommandBuilderStep2 AddResourceStringUtf8(string resourceString, string resourceName) + { + AddResource(ByteString.CopyFromUtf8(resourceString), resourceName); + return this; + } - public IDeployResourceCommandBuilderStep2 AddTenantId(string tenantId) - { - request.TenantId = tenantId; - return this; - } + public IDeployResourceCommandBuilderStep2 AddTenantId(string tenantId) + { + request.TenantId = tenantId; + return this; + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = gatewayClient.DeployResourceAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - var response = await asyncReply.ResponseAsync; - return new DeployResourceResponse(response); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.DeployResourceAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + var response = await asyncReply.ResponseAsync; + return new DeployResourceResponse(response); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); + } - private void AddResource(ByteString resource, string resourceName) + private void AddResource(ByteString resource, string resourceName) + { + var requestObject = new Resource { - var requestObject = new Resource - { - Name = resourceName, - Content = resource - }; + Name = resourceName, + Content = resource + }; - request.Resources.Add(requestObject); - } + request.Resources.Add(requestObject); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/EvaluateDecisionCommand.cs b/Client/Impl/Commands/EvaluateDecisionCommand.cs index 332964db..120ab56e 100644 --- a/Client/Impl/Commands/EvaluateDecisionCommand.cs +++ b/Client/Impl/Commands/EvaluateDecisionCommand.cs @@ -24,18 +24,10 @@ namespace Zeebe.Client.Impl.Commands; -public class EvaluateDecisionCommand : IEvaluateDecisionCommandStep1, IEvaluateDecisionCommandStep1.IEvaluateDecisionCommandStep2 +public class EvaluateDecisionCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) + : IEvaluateDecisionCommandStep1, IEvaluateDecisionCommandStep1.IEvaluateDecisionCommandStep2 { - private readonly EvaluateDecisionRequest request; - private readonly Gateway.GatewayClient gatewayClient; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public EvaluateDecisionCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) - { - gatewayClient = client; - request = new EvaluateDecisionRequest(); - this.asyncRetryStrategy = asyncRetryStrategy; - } + private readonly EvaluateDecisionRequest request = new (); public IEvaluateDecisionCommandStep1.IEvaluateDecisionCommandStep2 DecisionId(string decisionId) { @@ -51,7 +43,7 @@ public IEvaluateDecisionCommandStep1.IEvaluateDecisionCommandStep2 DecisionKey(l public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) { - var asyncReply = gatewayClient.EvaluateDecisionAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + var asyncReply = client.EvaluateDecisionAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); var response = await asyncReply.ResponseAsync; return new EvaluatedDecisionResponse(response); } diff --git a/Client/Impl/Commands/FailJobCommand.cs b/Client/Impl/Commands/FailJobCommand.cs index 9e859467..9e8cb672 100644 --- a/Client/Impl/Commands/FailJobCommand.cs +++ b/Client/Impl/Commands/FailJobCommand.cs @@ -23,63 +23,54 @@ using static GatewayProtocol.Gateway; using FailJobResponse = Zeebe.Client.Impl.Responses.FailJobResponse; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public class FailJobCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) + : IFailJobCommandStep1, IFailJobCommandStep2 { - public class FailJobCommand : IFailJobCommandStep1, IFailJobCommandStep2 + private readonly FailJobRequest request = new() { - private readonly FailJobRequest request; - private readonly GatewayClient gatewayClient; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public FailJobCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) - { - gatewayClient = client; - this.asyncRetryStrategy = asyncRetryStrategy; - request = new FailJobRequest - { - JobKey = jobKey - }; - } + JobKey = jobKey + }; - public IFailJobCommandStep2 Retries(int remainingRetries) - { - request.Retries = remainingRetries; - return this; - } + public IFailJobCommandStep2 Retries(int remainingRetries) + { + request.Retries = remainingRetries; + return this; + } - public IFailJobCommandStep2 ErrorMessage(string errorMsg) - { - request.ErrorMessage = errorMsg; - return this; - } + public IFailJobCommandStep2 ErrorMessage(string errorMsg) + { + request.ErrorMessage = errorMsg; + return this; + } - public IFailJobCommandStep2 RetryBackOff(TimeSpan retryBackOff) - { - request.RetryBackOff = (long)retryBackOff.TotalMilliseconds; - return this; - } + public IFailJobCommandStep2 RetryBackOff(TimeSpan retryBackOff) + { + request.RetryBackOff = (long)retryBackOff.TotalMilliseconds; + return this; + } - public IFailJobCommandStep2 Variables(string variables) - { - request.Variables = variables; - return this; - } + public IFailJobCommandStep2 Variables(string variables) + { + request.Variables = variables; + return this; + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = gatewayClient.FailJobAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - await asyncReply.ResponseAsync; - return new FailJobResponse(); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.FailJobAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + await asyncReply.ResponseAsync; + return new FailJobResponse(); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/JobActivator.cs b/Client/Impl/Commands/JobActivator.cs index d459b8dd..f77a88b8 100644 --- a/Client/Impl/Commands/JobActivator.cs +++ b/Client/Impl/Commands/JobActivator.cs @@ -7,51 +7,43 @@ using Zeebe.Client.Impl.Responses; using static GatewayProtocol.Gateway; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public delegate Task ConsumeJob(IActivateJobsResponse response); +internal class JobActivator(GatewayClient client) { - public delegate Task ConsumeJob(IActivateJobsResponse response); - internal class JobActivator + public async Task SendActivateRequest(ActivateJobsRequest request, ConsumeJob consumer, DateTime? requestTimeout = null, CancellationToken? cancellationToken = null) { - private readonly GatewayClient client; - - public JobActivator(GatewayClient client) + var activateRequestTimeout = requestTimeout ?? CalculateRequestTimeout(request); + using (var stream = client.ActivateJobs(request, deadline: activateRequestTimeout)) { - this.client = client; - } + var responseStream = stream.ResponseStream; - public async Task SendActivateRequest(ActivateJobsRequest request, ConsumeJob consumer, DateTime? requestTimeout = null, CancellationToken? cancellationToken = null) - { - var activateRequestTimeout = requestTimeout ?? CalculateRequestTimeout(request); - using (var stream = client.ActivateJobs(request, deadline: activateRequestTimeout)) + while (await MoveNext(responseStream, cancellationToken)) { - var responseStream = stream.ResponseStream; - - while (await MoveNext(responseStream, cancellationToken)) - { - var currentResponse = responseStream.Current; - var response = new ActivateJobsResponses(currentResponse); - await consumer.Invoke(response); - } + var currentResponse = responseStream.Current; + var response = new ActivateJobsResponses(currentResponse); + await consumer.Invoke(response); } } + } - private static DateTime CalculateRequestTimeout(ActivateJobsRequest request) - { - // we need a higher request deadline then the long polling request timeout - var longPollingTimeout = request.RequestTimeout; - return longPollingTimeout <= 0 - ? TimeSpan.FromSeconds(10).FromUtcNow() - : TimeSpan.FromSeconds((longPollingTimeout / 1000f) + 10).FromUtcNow(); - } + private static DateTime CalculateRequestTimeout(ActivateJobsRequest request) + { + // we need a higher request deadline then the long polling request timeout + var longPollingTimeout = request.RequestTimeout; + return longPollingTimeout <= 0 + ? TimeSpan.FromSeconds(10).FromUtcNow() + : TimeSpan.FromSeconds((longPollingTimeout / 1000f) + 10).FromUtcNow(); + } - private static async Task MoveNext(IAsyncStreamReader stream, CancellationToken? cancellationToken = null) + private static async Task MoveNext(IAsyncStreamReader stream, CancellationToken? cancellationToken = null) + { + if (cancellationToken.HasValue) { - if (cancellationToken.HasValue) - { - return await stream.MoveNext(cancellationToken.Value); - } - - return await stream.MoveNext(); + return await stream.MoveNext(cancellationToken.Value); } + + return await stream.MoveNext(); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/ModifyProcessInstanceCommand.cs b/Client/Impl/Commands/ModifyProcessInstanceCommand.cs index b500cd4c..47eb369e 100644 --- a/Client/Impl/Commands/ModifyProcessInstanceCommand.cs +++ b/Client/Impl/Commands/ModifyProcessInstanceCommand.cs @@ -10,26 +10,19 @@ namespace Zeebe.Client.Impl.Commands; -internal class ModifyProcessInstanceCommand : IModifyProcessInstanceCommandStep1, IModifyProcessInstanceCommandStep2, - IModifyProcessInstanceCommandStep3 +internal class ModifyProcessInstanceCommand( + GatewayClient client, + IAsyncRetryStrategy asyncRetryStrategy, + long processInstanceKey) + : IModifyProcessInstanceCommandStep1, + IModifyProcessInstanceCommandStep3 { - private readonly ModifyProcessInstanceRequest request; - private readonly GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - private ModifyProcessInstanceRequest.Types.ActivateInstruction currentActivateInstruction; - - public ModifyProcessInstanceCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, - long processInstanceKey) + private readonly ModifyProcessInstanceRequest request = new() { - this.asyncRetryStrategy = asyncRetryStrategy; - this.client = client; + ProcessInstanceKey = processInstanceKey + }; - request = new ModifyProcessInstanceRequest - { - ProcessInstanceKey = processInstanceKey - }; - } + private ModifyProcessInstanceRequest.Types.ActivateInstruction currentActivateInstruction; public IModifyProcessInstanceCommandStep3 ActivateElement(string elementId) { diff --git a/Client/Impl/Commands/PublishMessageCommand.cs b/Client/Impl/Commands/PublishMessageCommand.cs index 56b3a36a..eaed3db7 100644 --- a/Client/Impl/Commands/PublishMessageCommand.cs +++ b/Client/Impl/Commands/PublishMessageCommand.cs @@ -23,66 +23,57 @@ using static GatewayProtocol.Gateway; using PublishMessageResponse = Zeebe.Client.Impl.Responses.PublishMessageResponse; -namespace Zeebe.Client.Impl.Commands -{ - public class PublishMessageCommand : IPublishMessageCommandStep1, IPublishMessageCommandStep2, IPublishMessageCommandStep3 - { - private readonly PublishMessageRequest request; - private readonly GatewayClient gatewayClient; - private readonly IAsyncRetryStrategy asyncRetryStrategy; +namespace Zeebe.Client.Impl.Commands; - public PublishMessageCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) - { - gatewayClient = client; - request = new PublishMessageRequest(); - this.asyncRetryStrategy = asyncRetryStrategy; - } +public class PublishMessageCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) + : IPublishMessageCommandStep1, IPublishMessageCommandStep2, IPublishMessageCommandStep3 +{ + private readonly PublishMessageRequest request = new (); - public IPublishMessageCommandStep3 CorrelationKey(string correlationKey) - { - request.CorrelationKey = correlationKey; - return this; - } + public IPublishMessageCommandStep3 CorrelationKey(string correlationKey) + { + request.CorrelationKey = correlationKey; + return this; + } - public IPublishMessageCommandStep3 MessageId(string messageId) - { - request.MessageId = messageId; - return this; - } + public IPublishMessageCommandStep3 MessageId(string messageId) + { + request.MessageId = messageId; + return this; + } - public IPublishMessageCommandStep2 MessageName(string messageName) - { - request.Name = messageName; - return this; - } + public IPublishMessageCommandStep2 MessageName(string messageName) + { + request.Name = messageName; + return this; + } - public IPublishMessageCommandStep3 Variables(string variables) - { - request.Variables = variables; - return this; - } + public IPublishMessageCommandStep3 Variables(string variables) + { + request.Variables = variables; + return this; + } - public IPublishMessageCommandStep3 TimeToLive(TimeSpan timeToLive) - { - request.TimeToLive = (long)timeToLive.TotalMilliseconds; - return this; - } + public IPublishMessageCommandStep3 TimeToLive(TimeSpan timeToLive) + { + request.TimeToLive = (long)timeToLive.TotalMilliseconds; + return this; + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = gatewayClient.PublishMessageAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - await asyncReply.ResponseAsync; - return new PublishMessageResponse(); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.PublishMessageAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + await asyncReply.ResponseAsync; + return new PublishMessageResponse(); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/ResolveIncidentCommand.cs b/Client/Impl/Commands/ResolveIncidentCommand.cs index 205b8c2e..eee367fc 100644 --- a/Client/Impl/Commands/ResolveIncidentCommand.cs +++ b/Client/Impl/Commands/ResolveIncidentCommand.cs @@ -7,39 +7,33 @@ using Zeebe.Client.Api.Responses; using ResolveIncidentResponse = Zeebe.Client.Impl.Responses.ResolveIncidentResponse; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public class ResolveIncidentCommand( + Gateway.GatewayClient client, + IAsyncRetryStrategy asyncRetryStrategy, + long incidentKey) + : IResolveIncidentCommandStep1 { - public class ResolveIncidentCommand : IResolveIncidentCommandStep1 + private readonly ResolveIncidentRequest request = new() { - private readonly ResolveIncidentRequest request; - private readonly Gateway.GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public ResolveIncidentCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long incidentKey) - { - request = new ResolveIncidentRequest - { - IncidentKey = incidentKey - }; - this.client = client; - this.asyncRetryStrategy = asyncRetryStrategy; - } + IncidentKey = incidentKey + }; - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = client.ResolveIncidentAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - await asyncReply.ResponseAsync; - return new ResolveIncidentResponse(); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.ResolveIncidentAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + await asyncReply.ResponseAsync; + return new ResolveIncidentResponse(); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } } \ No newline at end of file diff --git a/Client/Impl/Commands/SetVariablesCommand.cs b/Client/Impl/Commands/SetVariablesCommand.cs index 2db893fc..512a4766 100644 --- a/Client/Impl/Commands/SetVariablesCommand.cs +++ b/Client/Impl/Commands/SetVariablesCommand.cs @@ -7,51 +7,45 @@ using Zeebe.Client.Api.Responses; using SetVariablesResponse = Zeebe.Client.Impl.Responses.SetVariablesResponse; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public class SetVariablesCommand( + Gateway.GatewayClient client, + IAsyncRetryStrategy asyncRetryStrategy, + long elementInstanceKey) + : ISetVariablesCommandStep1, ISetVariablesCommandStep2 { - public class SetVariablesCommand : ISetVariablesCommandStep1, ISetVariablesCommandStep2 + private readonly SetVariablesRequest request = new() { - private readonly SetVariablesRequest request; - private readonly Gateway.GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public SetVariablesCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long elementInstanceKey) - { - request = new SetVariablesRequest - { - ElementInstanceKey = elementInstanceKey - }; - this.client = client; - this.asyncRetryStrategy = asyncRetryStrategy; - } + ElementInstanceKey = elementInstanceKey + }; - public ISetVariablesCommandStep2 Variables(string variables) - { - request.Variables = variables; - return this; - } + public ISetVariablesCommandStep2 Variables(string variables) + { + request.Variables = variables; + return this; + } - public ISetVariablesCommandStep2 Local() - { - request.Local = true; - return this; - } + public ISetVariablesCommandStep2 Local() + { + request.Local = true; + return this; + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = client.SetVariablesAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - var response = await asyncReply.ResponseAsync; - return new SetVariablesResponse(response); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.SetVariablesAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + var response = await asyncReply.ResponseAsync; + return new SetVariablesResponse(response); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } } \ No newline at end of file diff --git a/Client/Impl/Commands/ThrowErrorCommand.cs b/Client/Impl/Commands/ThrowErrorCommand.cs index 434b1350..57650550 100644 --- a/Client/Impl/Commands/ThrowErrorCommand.cs +++ b/Client/Impl/Commands/ThrowErrorCommand.cs @@ -22,57 +22,48 @@ using Zeebe.Client.Api.Responses; using static GatewayProtocol.Gateway; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public class ThrowErrorCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) + : IThrowErrorCommandStep1, IThrowErrorCommandStep2 { - public class ThrowErrorCommand : IThrowErrorCommandStep1, IThrowErrorCommandStep2 + private readonly ThrowErrorRequest request = new() { - private readonly ThrowErrorRequest request; - private readonly GatewayClient gatewayClient; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public ThrowErrorCommand(GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) - { - gatewayClient = client; - this.asyncRetryStrategy = asyncRetryStrategy; - request = new ThrowErrorRequest - { - JobKey = jobKey - }; - } + JobKey = jobKey + }; - public IThrowErrorCommandStep2 ErrorCode(string errorCode) - { - request.ErrorCode = errorCode; - return this; - } + public IThrowErrorCommandStep2 ErrorCode(string errorCode) + { + request.ErrorCode = errorCode; + return this; + } - public IThrowErrorCommandStep2 ErrorMessage(string errorMessage) - { - request.ErrorMessage = errorMessage; - return this; - } + public IThrowErrorCommandStep2 ErrorMessage(string errorMessage) + { + request.ErrorMessage = errorMessage; + return this; + } - public IThrowErrorCommandStep2 Variables(string variables) - { - request.Variables = variables; - return this; - } + public IThrowErrorCommandStep2 Variables(string variables) + { + request.Variables = variables; + return this; + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = gatewayClient.ThrowErrorAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - await asyncReply.ResponseAsync; - return new Responses.ThrowErrorResponse(); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.ThrowErrorAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + await asyncReply.ResponseAsync; + return new Responses.ThrowErrorResponse(); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/TimeSpanExtensions.cs b/Client/Impl/Commands/TimeSpanExtensions.cs index b143e421..e3a53f2e 100644 --- a/Client/Impl/Commands/TimeSpanExtensions.cs +++ b/Client/Impl/Commands/TimeSpanExtensions.cs @@ -1,12 +1,11 @@ using System; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public static class TimeSpanExtensions { - public static class TimeSpanExtensions + public static DateTime FromUtcNow(this TimeSpan timeSpan) { - public static DateTime FromUtcNow(this TimeSpan timeSpan) - { - return DateTime.UtcNow + timeSpan; - } + return DateTime.UtcNow + timeSpan; } } \ No newline at end of file diff --git a/Client/Impl/Commands/TopologyRequestCommand.cs b/Client/Impl/Commands/TopologyRequestCommand.cs index 1f0294ae..9cfd9841 100644 --- a/Client/Impl/Commands/TopologyRequestCommand.cs +++ b/Client/Impl/Commands/TopologyRequestCommand.cs @@ -22,36 +22,28 @@ using Zeebe.Client.Api.Responses; using Zeebe.Client.Impl.Responses; -namespace Zeebe.Client.Impl.Commands -{ - public class TopologyRequestCommand : ITopologyRequestStep1 - { - private readonly Gateway.GatewayClient gatewayClient; - private readonly TopologyRequest request = new TopologyRequest(); - private readonly IAsyncRetryStrategy asyncRetryStrategy; +namespace Zeebe.Client.Impl.Commands; - public TopologyRequestCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) - { - gatewayClient = client; - this.asyncRetryStrategy = asyncRetryStrategy; - } +public class TopologyRequestCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy) + : ITopologyRequestStep1 +{ + private readonly TopologyRequest request = new (); - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = gatewayClient.TopologyAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - var response = await asyncReply.ResponseAsync; + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.TopologyAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + var response = await asyncReply.ResponseAsync; - return new Topology(response); - } + return new Topology(response); + } - public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); - } + public async Task SendWithRetry(TimeSpan? timespan = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timespan, token)); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); } -} +} \ No newline at end of file diff --git a/Client/Impl/Commands/UpdateJobTimeoutCommand.cs b/Client/Impl/Commands/UpdateJobTimeoutCommand.cs index 458350ac..4b2cd4dc 100644 --- a/Client/Impl/Commands/UpdateJobTimeoutCommand.cs +++ b/Client/Impl/Commands/UpdateJobTimeoutCommand.cs @@ -9,21 +9,13 @@ namespace Zeebe.Client.Impl.Commands; -public class UpdateJobTimeoutCommand : IUpdateJobTimeoutCommandStep1, IUpdateJobTimeoutCommandStep2 +public class UpdateJobTimeoutCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) + : IUpdateJobTimeoutCommandStep1, IUpdateJobTimeoutCommandStep2 { - private readonly UpdateJobTimeoutRequest request; - private readonly Gateway.GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public UpdateJobTimeoutCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) + private readonly UpdateJobTimeoutRequest request = new() { - request = new UpdateJobTimeoutRequest() - { - JobKey = jobKey - }; - this.client = client; - this.asyncRetryStrategy = asyncRetryStrategy; - } + JobKey = jobKey + }; public IUpdateJobTimeoutCommandStep2 Timeout(TimeSpan timeout) { diff --git a/Client/Impl/Commands/UpdateRetriesCommand.cs b/Client/Impl/Commands/UpdateRetriesCommand.cs index c336fb06..f559e1fb 100644 --- a/Client/Impl/Commands/UpdateRetriesCommand.cs +++ b/Client/Impl/Commands/UpdateRetriesCommand.cs @@ -7,45 +7,36 @@ using Zeebe.Client.Api.Responses; using Zeebe.Client.Impl.Responses; -namespace Zeebe.Client.Impl.Commands +namespace Zeebe.Client.Impl.Commands; + +public class UpdateRetriesCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) + : IUpdateRetriesCommandStep1, IUpdateRetriesCommandStep2 { - public class UpdateRetriesCommand : IUpdateRetriesCommandStep1, IUpdateRetriesCommandStep2 + private readonly UpdateJobRetriesRequest request = new() { - private readonly UpdateJobRetriesRequest request; - private readonly Gateway.GatewayClient client; - private readonly IAsyncRetryStrategy asyncRetryStrategy; - - public UpdateRetriesCommand(Gateway.GatewayClient client, IAsyncRetryStrategy asyncRetryStrategy, long jobKey) - { - request = new UpdateJobRetriesRequest - { - JobKey = jobKey - }; - this.client = client; - this.asyncRetryStrategy = asyncRetryStrategy; - } + JobKey = jobKey + }; - public IUpdateRetriesCommandStep2 Retries(int retries) - { - request.Retries = retries; - return this; - } + public IUpdateRetriesCommandStep2 Retries(int retries) + { + request.Retries = retries; + return this; + } - public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) - { - var asyncReply = client.UpdateJobRetriesAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); - await asyncReply.ResponseAsync; - return new UpdateRetriesResponse(); - } + public async Task Send(TimeSpan? timeout = null, CancellationToken token = default) + { + var asyncReply = client.UpdateJobRetriesAsync(request, deadline: timeout?.FromUtcNow(), cancellationToken: token); + await asyncReply.ResponseAsync; + return new UpdateRetriesResponse(); + } - public async Task Send(CancellationToken cancellationToken) - { - return await Send(token: cancellationToken); - } + public async Task Send(CancellationToken cancellationToken) + { + return await Send(token: cancellationToken); + } - public async Task SendWithRetry(TimeSpan? timeout = null, CancellationToken token = default) - { - return await asyncRetryStrategy.DoWithRetry(() => Send(timeout, token)); - } + public async Task SendWithRetry(TimeSpan? timeout = null, CancellationToken token = default) + { + return await asyncRetryStrategy.DoWithRetry(() => Send(timeout, token)); } } \ No newline at end of file