-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.cs
57 lines (37 loc) · 1.85 KB
/
Worker.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
using MediatR;
using Veronica.Sandbox.WS.Interfaces;
namespace Veronica.Sandbox.WS {
public class Worker : BackgroundService, INotificationHandler<SimpleMsg> {
private readonly ILogger<Worker> _logger;
private readonly ISingletonClass singClass;
private Guid id;
public Worker(ISingletonClass singletonClass, ILogger<Worker> logger) {
singClass = singletonClass;
id = Guid.NewGuid();
_logger = logger;
_logger.LogDebug($"Worker Init at: {DateTimeOffset.Now}");
_logger.LogInformation($"Worker loaded at: {DateTimeOffset.Now}");
_logger.LogInformation($"Worker Guid: --------------- {id}");
}
public override Task StartAsync(CancellationToken cancellationToken) {
_logger.LogInformation($"Worker Started at: {DateTimeOffset.Now}");
return base.StartAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
_logger.LogInformation($"Worker Start Execute at: {DateTimeOffset.Now}");
await Task.Delay(1000, stoppingToken);
singClass.InitClass("Worker");
singClass.PrintGuid();
await Task.Delay(5000, stoppingToken);
_logger.LogInformation("Worker Execution Ended...");
}
public override Task StopAsync(CancellationToken cancellationToken) {
_logger.LogInformation($"Worker stopped at: {DateTimeOffset.Now}");
return base.StopAsync(cancellationToken);
}
public Task Handle(SimpleMsg notification, CancellationToken cancellationToken) {
_logger.LogInformation($"Worker received notification {notification.msg} at: {DateTimeOffset.Now} ************************* ");
return Task.CompletedTask;
}
}
}