-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHangfireScheduleHelper.cs
More file actions
91 lines (81 loc) · 5.35 KB
/
Copy pathHangfireScheduleHelper.cs
File metadata and controls
91 lines (81 loc) · 5.35 KB
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
using Altinn.Correspondence.Application.PublishCorrespondence;
using Altinn.Correspondence.Common.Caching;
using Altinn.Correspondence.Core.Models.Entities;
using Altinn.Correspondence.Core.Models.Notifications;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;
using Altinn.Correspondence.Integrations.Hangfire;
using Hangfire;
using Microsoft.Extensions.Logging;
namespace Altinn.Correspondence.Application.Helpers
{
public class HangfireScheduleHelper(IBackgroundJobClient backgroundJobClient,
IHybridCacheWrapper hybridCacheWrapper,
ICorrespondenceRepository correspondenceRepository,
ILogger<HangfireScheduleHelper> logger)
{
public void SchedulePublishAfterDialogCreated(Guid correspondenceId, string dialogJobId, CancellationToken cancellationToken)
{
backgroundJobClient.ContinueJobWith<HangfireScheduleHelper>(dialogJobId, HangfireQueues.LiveMigration, (helper) => helper.SchedulePublishAtPublishTime(correspondenceId, CancellationToken.None));
}
public async Task SchedulePublishAfterDialogCreated(Guid correspondenceId, CancellationToken cancellationToken)
{
if (!await correspondenceRepository.AreAllAttachmentsPublished(correspondenceId, cancellationToken))
{
logger.LogInformation("Not all attachments published for correspondence {correspondenceId}, skipping publish scheduling", correspondenceId);
return;
}
var dialogJobId = await hybridCacheWrapper.GetAsync<string?>($"dialogJobId_{correspondenceId}", cancellationToken: cancellationToken);
if (dialogJobId is null)
{
logger.LogError("Could not find dialogJobId for correspondence {correspondenceId} in cache. More than 24 hours delayed?", correspondenceId);
await SchedulePublishAtPublishTime(correspondenceId, cancellationToken);
}
else
{
#pragma warning disable CS4014 // Hangfire handles Task-returning job expressions by awaiting them during job execution
backgroundJobClient.ContinueJobWith<HangfireScheduleHelper>(dialogJobId, (helper) => helper.SchedulePublishAtPublishTime(correspondenceId, CancellationToken.None), JobContinuationOptions.OnAnyFinishedState);
#pragma warning restore CS4014
}
}
public async Task SchedulePublishAfterTransmissionCreated(Guid correspondenceId, string transmissionJobId, CancellationToken cancellationToken)
{
if (transmissionJobId is null)
{
logger.LogError("Could not find transmissionJobId for correspondence {correspondenceId} in cache. More than 24 hours delayed?", correspondenceId);
await SchedulePublishAtPublishTime(correspondenceId, cancellationToken);
}
else
{
#pragma warning disable CS4014 // Hangfire handles Task-returning job expressions by awaiting them during job execution
backgroundJobClient.ContinueJobWith<HangfireScheduleHelper>(transmissionJobId, (helper) => helper.SchedulePublishAtPublishTime(correspondenceId, CancellationToken.None), JobContinuationOptions.OnAnyFinishedState);
#pragma warning restore CS4014
}
}
public async Task SchedulePublishAtPublishTime(Guid correspondenceId, CancellationToken cancellationToken)
{
var correspondence = await correspondenceRepository.GetCorrespondenceById(correspondenceId, true, false, false, cancellationToken);
if (correspondence is null)
{
throw new Exception($"Correspondence with id {correspondenceId} not found when scheduling publish");
}
SchedulePublishAtPublishTime(correspondence, cancellationToken);
}
public void SchedulePublishAtPublishTime(CorrespondenceEntity correspondence, CancellationToken cancellationToken)
{
backgroundJobClient.Schedule<PublishCorrespondenceHandler>(HangfireQueues.Default, (handler) => handler.Process(correspondence.Id, null, CancellationToken.None), GetActualPublishTime(correspondence.RequestedPublishTime));
}
private static DateTimeOffset GetActualPublishTime(DateTimeOffset publishTime) => publishTime < DateTimeOffset.UtcNow ? DateTimeOffset.UtcNow : publishTime; // If in past, do now
public async Task CreateActivityAfterDialogCreated(Guid correspondenceId, NotificationOrderRequestV2 notification, DateTimeOffset operationTimestamp)
{
var dialogJobId = await hybridCacheWrapper.GetAsync<string?>($"dialogJobId_{correspondenceId}");
if (dialogJobId is null)
{
logger.LogError("Could not find dialogJobId for correspondence {correspondenceId} in cache. More than 24 hours delayed?", correspondenceId);
return;
}
backgroundJobClient.ContinueJobWith<IDialogportenService>(dialogJobId, (dialogPortenService) => dialogPortenService.CreateInformationActivity(correspondenceId, DialogportenActorType.ServiceOwner, DialogportenTextType.NotificationOrderCreated, operationTimestamp, notification.RequestedSendTime.ToString("yyyy-MM-dd HH:mm")), JobContinuationOptions.OnlyOnSucceededState);
}
}
}