-
Notifications
You must be signed in to change notification settings - Fork 53
/
ZeebeClient.cs
190 lines (160 loc) · 7.58 KB
/
ZeebeClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// Copyright (c) 2018 camunda services GmbH ([email protected])
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using GatewayProtocol;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Zeebe.Client.Api.Builder;
using Zeebe.Client.Api.Commands;
using Zeebe.Client.Api.Misc;
using Zeebe.Client.Api.Responses;
using Zeebe.Client.Api.Worker;
using Zeebe.Client.Impl.Builder;
using Zeebe.Client.Impl.Commands;
using Zeebe.Client.Impl.Misc;
using Zeebe.Client.Impl.Worker;
namespace Zeebe.Client
{
/// <inheritdoc />
public class ZeebeClient : IZeebeClient
{
private static readonly int MaxWaitTimeInSeconds = 60;
private static readonly Func<int, TimeSpan> DefaultWaitTimeProvider =
retryAttempt => TimeSpan.FromSeconds(Math.Max(Math.Pow(2, retryAttempt), MaxWaitTimeInSeconds));
private static readonly TimeSpan DefaultKeepAlive = TimeSpan.FromSeconds(30);
private readonly Channel channelToGateway;
private readonly ILoggerFactory loggerFactory;
private Gateway.GatewayClient gatewayClient;
private readonly IAsyncRetryStrategy asyncRetryStrategy;
internal ZeebeClient(string address, TimeSpan? keepAlive, Func<int, TimeSpan> sleepDurationProvider, ILoggerFactory loggerFactory = null)
: this(address, ChannelCredentials.Insecure, keepAlive, sleepDurationProvider, loggerFactory)
{ }
internal ZeebeClient(string address,
ChannelCredentials credentials,
TimeSpan? keepAlive,
Func<int, TimeSpan> sleepDurationProvider,
ILoggerFactory loggerFactory = null)
{
this.loggerFactory = loggerFactory;
var channelOptions = new List<ChannelOption>();
var clientVersion = typeof(ZeebeClient).Assembly.GetName().Version;
var userAgentString = $"zeebe-client-csharp/{clientVersion}";
var userAgentOption = new ChannelOption(ChannelOptions.PrimaryUserAgentString, userAgentString);
channelOptions.Add(userAgentOption);
AddKeepAliveToChannelOptions(channelOptions, keepAlive);
channelToGateway =
new Channel(address, credentials, channelOptions);
gatewayClient = new Gateway.GatewayClient(channelToGateway);
asyncRetryStrategy =
new TransientGrpcErrorRetryStrategy(sleepDurationProvider ??
DefaultWaitTimeProvider);
}
/// <summary>
/// Adds keepAlive options to the channel options.
/// </summary>
/// <param name="channelOptions">the current existing channel options</param>
private void AddKeepAliveToChannelOptions(List<ChannelOption> channelOptions, TimeSpan? keepAlive)
{
// GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS
// This channel argument if set to 1 (0 : false; 1 : true), allows keepalive pings to be sent even if there are no calls in flight.
// channelOptions.Add(new ChannelOption("grpc.keepalive_permit_without_calls", "1"));
// this will increase load on the system and also increase used resources
// we should prefer idleTimeout setting
// https://stackoverflow.com/questions/57930529/grpc-connection-use-keepalive-or-idletimeout
// GRPC_ARG_KEEPALIVE_TIME_MS
// This channel argument controls the period (in milliseconds) after which a keepalive ping is sent on the transport.
var actualKeepAlive = keepAlive.GetValueOrDefault(DefaultKeepAlive);
channelOptions.Add(new ChannelOption("grpc.keepalive_time_ms", (int) actualKeepAlive.TotalMilliseconds));
}
////////////////////////////////////////////////////////////////////////
///////////////////////////// JOBS /////////////////////////////////////
////////////////////////////////////////////////////////////////////////
public IJobWorkerBuilderStep1 NewWorker()
{
return new JobWorkerBuilder(this, loggerFactory);
}
public IActivateJobsCommandStep1 NewActivateJobsCommand()
{
return new ActivateJobsCommand(gatewayClient, asyncRetryStrategy);
}
public ICompleteJobCommandStep1 NewCompleteJobCommand(long jobKey)
{
return new CompleteJobCommand(gatewayClient, asyncRetryStrategy, jobKey);
}
public ICompleteJobCommandStep1 NewCompleteJobCommand(IJob activatedJob)
{
return new CompleteJobCommand(gatewayClient, asyncRetryStrategy, activatedJob.Key);
}
public IFailJobCommandStep1 NewFailCommand(long jobKey)
{
return new FailJobCommand(gatewayClient, jobKey);
}
public IUpdateRetriesCommandStep1 NewUpdateRetriesCommand(long jobKey)
{
return new UpdateRetriesCommand(gatewayClient, jobKey);
}
public IThrowErrorCommandStep1 NewThrowErrorCommand(long jobKey)
{
return new ThrowErrorCommand(gatewayClient, jobKey);
}
////////////////////////////////////////////////////////////////////////
///////////////////////////// Processes ////////////////////////////////
////////////////////////////////////////////////////////////////////////
public IDeployProcessCommandStep1 NewDeployCommand()
{
return new DeployProcessCommand(gatewayClient);
}
public ICreateProcessInstanceCommandStep1 NewCreateProcessInstanceCommand()
{
return new CreateProcessInstanceCommand(gatewayClient);
}
public ICancelProcessInstanceCommandStep1 NewCancelInstanceCommand(long processInstanceKey)
{
return new CancelProcessInstanceCommand(gatewayClient, asyncRetryStrategy, processInstanceKey);
}
public ISetVariablesCommandStep1 NewSetVariablesCommand(long elementInstanceKey)
{
return new SetVariablesCommand(gatewayClient, elementInstanceKey);
}
public IResolveIncidentCommandStep1 NewResolveIncidentCommand(long incidentKey)
{
return new ResolveIncidentCommand(gatewayClient, incidentKey);
}
public IPublishMessageCommandStep1 NewPublishMessageCommand()
{
return new PublishMessageCommand(gatewayClient);
}
public ITopologyRequestStep1 TopologyRequest() => new TopologyRequestCommand(gatewayClient);
public void Dispose()
{
if (gatewayClient is ClosedGatewayClient)
{
return;
}
gatewayClient = new ClosedGatewayClient();
channelToGateway.ShutdownAsync().Wait();
}
/// <summary>
/// Creates an new IZeebeClientBuilder. This builder need to be used to construct
/// a ZeebeClient.
/// </summary>
/// <returns>an builder to construct an ZeebeClient</returns>
public static IZeebeClientBuilder Builder()
{
return new ZeebeClientBuilder();
}
}
}