-
Notifications
You must be signed in to change notification settings - Fork 0
/
PulsJob.cs
53 lines (46 loc) · 1.49 KB
/
PulsJob.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
namespace SimulationModule.Jobs;
/// <summary>
/// PulsJob - Handles job execution.
/// </summary>
[DisallowConcurrentExecution]
public sealed partial class PulsJob : IJob
{
private readonly NumberGenerator numberGenerator;
public PulsJob(
NumberGenerator numberGenerator,
ILogger<PulsJob> logger)
{
this.logger = logger;
this.numberGenerator = numberGenerator;
}
public async Task Execute(
IJobExecutionContext context)
{
LogJobStarted(nameof(PulsJob));
try
{
var moduleClient = (IModuleClientWrapper?)context.JobDetail.JobDataMap[SimulationModuleConstants.ModuleClient];
if (moduleClient is null)
{
LogFailedToAcquireModuleClient(nameof(PulsJob));
return;
}
var temperature = numberGenerator.GetNextValue();
var json = $$"""{"temperature":{{temperature}}}""";
using var message = new Message(Encoding.UTF8.GetBytes(json));
message.ContentEncoding = Encoding.UTF8.WebName;
message.ContentType = MediaTypeNames.Application.Json;
await moduleClient.SendEventAsync("temperature", message, context.CancellationToken);
}
catch (Exception ex)
{
LogUnhandledExceptionInJob(
nameof(PulsJob),
ex.GetLastInnerMessage());
}
finally
{
LogJobEnded(nameof(PulsJob));
}
}
}