|
| 1 | +namespace ServiceControl.Persistence.Sql.Core.Implementation; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using DbContexts; |
| 8 | +using Entities; |
| 9 | +using Microsoft.EntityFrameworkCore; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using ServiceControl.Infrastructure.DomainEvents; |
| 13 | +using ServiceControl.Persistence.Recoverability; |
| 14 | +using ServiceControl.Recoverability; |
| 15 | + |
| 16 | +public class ArchiveMessages : DataStoreBase, IArchiveMessages |
| 17 | +{ |
| 18 | + readonly IDomainEvents domainEvents; |
| 19 | + readonly ILogger<ArchiveMessages> logger; |
| 20 | + |
| 21 | + public ArchiveMessages( |
| 22 | + IServiceProvider serviceProvider, |
| 23 | + IDomainEvents domainEvents, |
| 24 | + ILogger<ArchiveMessages> logger) : base(serviceProvider) |
| 25 | + { |
| 26 | + this.domainEvents = domainEvents; |
| 27 | + this.logger = logger; |
| 28 | + } |
| 29 | + |
| 30 | + public async Task ArchiveAllInGroup(string groupId) |
| 31 | + { |
| 32 | + // This would update all failed messages in the group to archived status |
| 33 | + // For now, this is a placeholder that would need the failed message infrastructure |
| 34 | + logger.LogInformation("Archiving all messages in group {GroupId}", groupId); |
| 35 | + await Task.CompletedTask; |
| 36 | + } |
| 37 | + |
| 38 | + public async Task UnarchiveAllInGroup(string groupId) |
| 39 | + { |
| 40 | + logger.LogInformation("Unarchiving all messages in group {GroupId}", groupId); |
| 41 | + await Task.CompletedTask; |
| 42 | + } |
| 43 | + |
| 44 | + public bool IsOperationInProgressFor(string groupId, ArchiveType archiveType) |
| 45 | + { |
| 46 | + return ExecuteWithDbContext(dbContext => |
| 47 | + { |
| 48 | + var operationId = MakeOperationId(groupId, archiveType); |
| 49 | + var operation = dbContext.ArchiveOperations |
| 50 | + .AsNoTracking() |
| 51 | + .FirstOrDefault(a => a.Id == Guid.Parse(operationId)); |
| 52 | + |
| 53 | + if (operation == null) |
| 54 | + { |
| 55 | + return Task.FromResult(false); |
| 56 | + } |
| 57 | + |
| 58 | + return Task.FromResult(operation.ArchiveState != (int)ArchiveState.ArchiveCompleted); |
| 59 | + }).Result; |
| 60 | + } |
| 61 | + |
| 62 | + public bool IsArchiveInProgressFor(string groupId) |
| 63 | + { |
| 64 | + return IsOperationInProgressFor(groupId, ArchiveType.FailureGroup) || |
| 65 | + IsOperationInProgressFor(groupId, ArchiveType.All); |
| 66 | + } |
| 67 | + |
| 68 | + public void DismissArchiveOperation(string groupId, ArchiveType archiveType) |
| 69 | + { |
| 70 | + ExecuteWithDbContext(dbContext => |
| 71 | + { |
| 72 | + var operationId = Guid.Parse(MakeOperationId(groupId, archiveType)); |
| 73 | + |
| 74 | + dbContext.ArchiveOperations.Where(a => a.Id == operationId).ExecuteDelete(); |
| 75 | + return Task.CompletedTask; |
| 76 | + }).Wait(); |
| 77 | + } |
| 78 | + |
| 79 | + public Task StartArchiving(string groupId, ArchiveType archiveType) |
| 80 | + { |
| 81 | + return ExecuteWithDbContext(async dbContext => |
| 82 | + { |
| 83 | + var operation = new ArchiveOperationEntity |
| 84 | + { |
| 85 | + Id = Guid.Parse(MakeOperationId(groupId, archiveType)), |
| 86 | + RequestId = groupId, |
| 87 | + GroupName = groupId, |
| 88 | + ArchiveType = (int)archiveType, |
| 89 | + ArchiveState = (int)ArchiveState.ArchiveStarted, |
| 90 | + TotalNumberOfMessages = 0, |
| 91 | + NumberOfMessagesArchived = 0, |
| 92 | + NumberOfBatches = 0, |
| 93 | + CurrentBatch = 0, |
| 94 | + Started = DateTime.UtcNow |
| 95 | + }; |
| 96 | + |
| 97 | + await dbContext.ArchiveOperations.AddAsync(operation); |
| 98 | + await dbContext.SaveChangesAsync(); |
| 99 | + |
| 100 | + logger.LogInformation("Started archiving for group {GroupId}", groupId); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + public Task StartUnarchiving(string groupId, ArchiveType archiveType) |
| 105 | + { |
| 106 | + return ExecuteWithDbContext(async dbContext => |
| 107 | + { |
| 108 | + var operation = new ArchiveOperationEntity |
| 109 | + { |
| 110 | + Id = Guid.Parse(MakeOperationId(groupId, archiveType)), |
| 111 | + RequestId = groupId, |
| 112 | + GroupName = groupId, |
| 113 | + ArchiveType = (int)archiveType, |
| 114 | + ArchiveState = (int)ArchiveState.ArchiveStarted, |
| 115 | + TotalNumberOfMessages = 0, |
| 116 | + NumberOfMessagesArchived = 0, |
| 117 | + NumberOfBatches = 0, |
| 118 | + CurrentBatch = 0, |
| 119 | + Started = DateTime.UtcNow |
| 120 | + }; |
| 121 | + |
| 122 | + await dbContext.ArchiveOperations.AddAsync(operation); |
| 123 | + await dbContext.SaveChangesAsync(); |
| 124 | + |
| 125 | + logger.LogInformation("Started unarchiving for group {GroupId}", groupId); |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + public IEnumerable<InMemoryArchive> GetArchivalOperations() |
| 130 | + { |
| 131 | + // Note: IEnumerable methods need direct scope management as they yield results |
| 132 | + using var scope = serviceProvider.CreateScope(); |
| 133 | + var dbContext = scope.ServiceProvider.GetRequiredService<ServiceControlDbContextBase>(); |
| 134 | + |
| 135 | + var operations = dbContext.ArchiveOperations |
| 136 | + .AsNoTracking() |
| 137 | + .AsEnumerable(); |
| 138 | + |
| 139 | + foreach (var op in operations) |
| 140 | + { |
| 141 | + yield return new InMemoryArchive(op.RequestId, (ArchiveType)op.ArchiveType, domainEvents) |
| 142 | + { |
| 143 | + GroupName = op.GroupName, |
| 144 | + ArchiveState = (ArchiveState)op.ArchiveState, |
| 145 | + TotalNumberOfMessages = op.TotalNumberOfMessages, |
| 146 | + NumberOfMessagesArchived = op.NumberOfMessagesArchived, |
| 147 | + NumberOfBatches = op.NumberOfBatches, |
| 148 | + CurrentBatch = op.CurrentBatch, |
| 149 | + Started = op.Started, |
| 150 | + Last = op.Last, |
| 151 | + CompletionTime = op.CompletionTime |
| 152 | + }; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + static string MakeOperationId(string groupId, ArchiveType archiveType) |
| 157 | + { |
| 158 | + return $"{archiveType}/{groupId}"; |
| 159 | + } |
| 160 | +} |
0 commit comments