-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathProgram.cs
99 lines (85 loc) · 3.77 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NLog.Extensions.Logging;
using Zeebe.Client;
using Zeebe.Client.Api.Responses;
using Zeebe.Client.Api.Worker;
using Zeebe.Client.Impl.Builder;
namespace csharp
{
class Program
{
private static readonly string ClientID = "YOUR_CLIENT_ID";
private static readonly string ClientSecret = "YOUR_CLIENT_SECRET";
private static readonly string ClusterAddress = "YOUR_CLUSTER_ID.bru-2.zeebe.camunda.io:443";
private static readonly ILoggerFactory LoggerFactory = new NLogLoggerFactory();
private static readonly ILogger<Program> Log = LoggerFactory.CreateLogger<Program>();
private const string LogMessage = "Started instance for" +
" processDefinitionKey='{processDefinitionKey}'," +
" bpmnProcessId='{bpmnProcessId}'," +
" version='{version}'" +
" with processInstanceKey='{processInstanceKey}'";
private const string VariablesKey = "message_content";
private const string VariablesValue = "Hello from the C# get started";
static async Task Main(string[] _)
{
using (var zeebeClient = CreateZeebeClient())
{
var topology = await zeebeClient.TopologyRequest().Send();
Console.WriteLine("Topology: " + topology);
await zeebeClient.NewDeployCommand().AddResourceFile("send-email.bpmn").Send();
var variables = $"{{\"{VariablesKey}\":\"{VariablesValue}\"}}";
var processInstanceResponse = await zeebeClient
.NewCreateProcessInstanceCommand()
.BpmnProcessId("send-email")
.LatestVersion()
.Variables(variables).Send();
Log.LogInformation(LogMessage,
processInstanceResponse.ProcessDefinitionKey,
processInstanceResponse.BpmnProcessId,
processInstanceResponse.Version,
processInstanceResponse.ProcessInstanceKey);
using (zeebeClient.NewWorker()
.JobType("email")
.Handler(JobHandler)
.MaxJobsActive(3)
.Timeout(TimeSpan.FromSeconds(10))
.PollInterval(TimeSpan.FromMinutes(1))
.PollingTimeout(TimeSpan.FromSeconds(30))
.Name("CsharpGetStartedWorker")
.Open())
{
AwaitExitUserCmd();
}
}
}
private static void JobHandler(IJobClient jobClient, IJob activatedJob)
{
var variables = JsonConvert.DeserializeObject<Dictionary<string, string>>(activatedJob.Variables);
Log.LogInformation($"Sending email with message content: {variables[VariablesKey]}");
jobClient.NewCompleteJobCommand(activatedJob).Send();
}
private static IZeebeClient CreateZeebeClient()
{
return CamundaCloudClientBuilder
.Builder()
.UseClientId(ClientID)
.UseClientSecret(ClientSecret)
.UseContactPoint(ClusterAddress)
.UseLoggerFactory(LoggerFactory) // optional
.Build();
}
private static void AwaitExitUserCmd()
{
var command = "";
do
{
Console.Write("Type 'exit' to stop: ");
command = Console.ReadLine();
} while (!command.ToLower().Equals("exit"));
}
}
}