forked from camunda-community-hub/zeebe-client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerBase.cs
51 lines (37 loc) · 1.54 KB
/
WorkerBase.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
using Zeebe.Client;
using Zeebe.Client.Api.Commands;
using Zeebe.Client.Api.Responses;
using Zeebe.Client.Api.Worker;
namespace Client.GracefulStopping.Example.Workers;
public abstract class WorkerBase : IHostedService
{
private WorkerManager _workerManager;
private ILogger<WorkerBase> _logger;
private WorkerInstance workerInstance;
private string _jobType;
private CancellationTokenSource _workerCancellationTokenSource;
private WorkerHandlerCounter _workerHandlerCounter;
protected WorkerBase(WorkerManager workerManager, string jobType,
ILogger<WorkerBase> logger, WorkerHandlerCounter workerHandlerCounter)
{
_workerManager = workerManager;
_jobType = jobType;
_logger = logger;
_workerHandlerCounter = workerHandlerCounter;
}
protected abstract Task WorkTaskAsync(IJob job, ICompleteJobCommandStep1 cmd, CancellationToken cancellationToken);
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Start worker");
workerInstance = _workerManager.StartWorker(_jobType, WorkTaskAsync, cancellationToken);
return Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("StopAsync. Start. Dispose worker");
workerInstance.JobWorker.Dispose();
_logger.LogInformation("StopAsync. Delay before close");
await _workerHandlerCounter.WaitForActiveHandlersAsync();
_logger.LogInformation("StopAsync. Finish");
}
}