-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Client.Examples): add cloud worker example
- Loading branch information
1 parent
2dfbaa5
commit fd0a22c
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
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,101 @@ | ||
## Camunda Cloud Worker Example | ||
In the following you see an example of how to use the Zeebe C# client job worker with the CamundaCloud. | ||
|
||
We have a separate thread which outputs how many jobs with worker has completed in a second. | ||
With that we can play around with the different job worker configuration and see how it affects the throughput. | ||
|
||
```csharp | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NLog; | ||
using NLog.Extensions.Logging; | ||
using Zeebe.Client; | ||
using Zeebe.Client.Api.Responses; | ||
using Zeebe.Client.Api.Worker; | ||
using Zeebe.Client.Impl.Builder; | ||
|
||
namespace Client.Examples | ||
{ | ||
internal class CloudWorkerExample | ||
{ | ||
private const string AuthServer = "https://login.cloud.ultrawombat.com/oauth/token"; | ||
private const string ClientId = "{clientId}"; | ||
private const string ClientSecret = "{clientSecret}"; | ||
private const string Audience = "{cluster-id}.zeebe.ultrawombat.com"; | ||
private const string ZeebeUrl = Audience + ":443"; | ||
|
||
private static volatile int lastCompleted; | ||
private static volatile int currentCompleted; | ||
private static readonly string DemoProcessPath = | ||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "ten_tasks.bpmn"); | ||
|
||
private static readonly string PayloadPath = | ||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "big_payload.json"); | ||
|
||
private static readonly string JobType = "benchmark-task"; | ||
private static readonly string WorkerName = Environment.MachineName; | ||
|
||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); | ||
|
||
public static async Task Main(string[] args) | ||
{ | ||
// create zeebe client | ||
var client = ZeebeClient.Builder() | ||
.UseLoggerFactory(new NLogLoggerFactory()) | ||
.UseGatewayAddress(ZeebeUrl) | ||
.UseTransportEncryption() | ||
.UseAccessTokenSupplier( | ||
CamundaCloudTokenProvider.Builder() | ||
.UseAuthServer(AuthServer) | ||
.UseClientId(ClientId) | ||
.UseClientSecret(ClientSecret) | ||
.UseAudience(Audience) | ||
.Build()) | ||
.Build(); | ||
|
||
var topology = await client.TopologyRequest().Send(); | ||
|
||
Logger.Info(topology.ToString); | ||
Console.WriteLine(topology); | ||
|
||
Task.Run(async () => | ||
{ | ||
while (true) | ||
{ | ||
await Task.Delay(1000); | ||
Logger.Info("Completed " + (currentCompleted - lastCompleted) + " in the last second."); | ||
lastCompleted = currentCompleted; | ||
} | ||
}); | ||
|
||
// open job worker | ||
using (var signal = new EventWaitHandle(false, EventResetMode.AutoReset)) | ||
{ | ||
client.NewWorker() | ||
.JobType(JobType) | ||
.Handler(HandleJob) | ||
.MaxJobsActive(120) | ||
.Name(WorkerName) | ||
.AutoCompletion() | ||
.PollInterval(TimeSpan.FromMilliseconds(100)) | ||
.Timeout(TimeSpan.FromSeconds(10)) | ||
.PollingTimeout(TimeSpan.FromSeconds(30)) | ||
.HandlerThreads(8) | ||
.Open(); | ||
|
||
// blocks main thread, so that worker can run | ||
signal.WaitOne(); | ||
} | ||
} | ||
|
||
private static async Task HandleJob(IJobClient jobClient, IJob job) | ||
{ | ||
await Task.Delay(150); | ||
jobClient.NewCompleteJobCommand(job).Send(); | ||
currentCompleted++; | ||
} | ||
} | ||
} | ||
``` |