Skip to content

Commit

Permalink
Store archived UnverifyLogs to the azure
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha12 committed Oct 25, 2023
1 parent ebb7a2e commit f4b6c99
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
19 changes: 18 additions & 1 deletion src/GrillBot.App/Jobs/Abstractions/ArchivationJobBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using System.Xml.Linq;
using System.IO.Compression;
using System.Xml.Linq;
using GrillBot.App.Helpers;
using GrillBot.App.Infrastructure.Jobs;
using Microsoft.Extensions.DependencyInjection;

namespace GrillBot.App.Jobs.Abstractions;

public abstract class ArchivationJobBase : Job
{
protected BlobManagerFactoryHelper BlobManagerFactoryHelper { get; }
protected GrillBotDatabaseBuilder DatabaseBuilder { get; }

protected ArchivationJobBase(IServiceProvider serviceProvider) : base(serviceProvider)
{
BlobManagerFactoryHelper = serviceProvider.GetRequiredService<BlobManagerFactoryHelper>();
DatabaseBuilder = serviceProvider.GetRequiredService<GrillBotDatabaseBuilder>();
}

protected static IEnumerable<XAttribute> CreateMetadata(int count)
Expand Down Expand Up @@ -56,4 +64,13 @@ private static XElement TransformUser(Database.Entity.User user)

return element;
}

protected static async Task AddXmlToZipAsync(ZipArchive archive, XElement xml, string xmlName)
{
var entry = archive.CreateEntry(xmlName);
entry.LastWriteTime = DateTimeOffset.Now;

await using var entryStream = entry.Open();
await xml.SaveAsync(entryStream, SaveOptions.OmitDuplicateNamespaces | SaveOptions.DisableFormatting, CancellationToken.None);
}
}
18 changes: 2 additions & 16 deletions src/GrillBot.App/Jobs/AuditLogClearingJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ namespace GrillBot.App.Jobs;
[DisallowConcurrentExecution]
public class AuditLogClearingJob : ArchivationJobBase
{
private GrillBotDatabaseBuilder DbFactory { get; }
private IAuditLogServiceClient AuditLogServiceClient { get; }
private BlobManagerFactoryHelper BlobManagerFactoryHelper { get; }

public AuditLogClearingJob(GrillBotDatabaseBuilder dbFactory, IServiceProvider serviceProvider, IAuditLogServiceClient auditLogServiceClient, BlobManagerFactoryHelper blobManagerFactoryHelper)
: base(serviceProvider)
public AuditLogClearingJob(IServiceProvider serviceProvider, IAuditLogServiceClient auditLogServiceClient) : base(serviceProvider)
{
DbFactory = dbFactory;
AuditLogServiceClient = auditLogServiceClient;
BlobManagerFactoryHelper = blobManagerFactoryHelper;
}

protected override async Task RunAsync(IJobExecutionContext context)
Expand All @@ -34,7 +29,7 @@ protected override async Task RunAsync(IJobExecutionContext context)

var xmlData = XElement.Parse(archivationResult.Xml);

await using var repository = DbFactory.CreateRepository();
await using var repository = DatabaseBuilder.CreateRepository();

await ProcessGuildsAsync(repository, archivationResult.GuildIds, xmlData);
await ProcessChannelsAsync(repository, archivationResult.ChannelIds, xmlData);
Expand Down Expand Up @@ -106,15 +101,6 @@ private async Task<long> StoreDataAsync(XElement xml, IEnumerable<string> files)
return archiveSize;
}

private static async Task AddXmlToZipAsync(ZipArchive archive, XElement xml, string xmlName)
{
var entry = archive.CreateEntry(xmlName);
entry.LastWriteTime = DateTimeOffset.Now;

await using var entryStream = entry.Open();
await xml.SaveAsync(entryStream, SaveOptions.OmitDuplicateNamespaces | SaveOptions.DisableFormatting, CancellationToken.None);
}

private async Task AddFilesToArchiveAsync(IEnumerable<string> files, ZipArchive archive)
{
if (!files.Any())
Expand Down
47 changes: 26 additions & 21 deletions src/GrillBot.App/Jobs/UnverifyLogArchivationJob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO.Compression;
using System.Xml.Linq;
using GrillBot.App.Helpers;
using GrillBot.App.Jobs.Abstractions;
using GrillBot.Common.FileStorage;
using Quartz;
Expand All @@ -9,15 +10,10 @@ namespace GrillBot.App.Jobs;
[DisallowConcurrentExecution]
public class UnverifyLogArchivationJob : ArchivationJobBase
{
private GrillBotDatabaseBuilder DatabaseBuilder { get; }
private FileStorageFactory FileStorageFactory { get; }
private IConfiguration Configuration { get; }

public UnverifyLogArchivationJob(IServiceProvider serviceProvider, GrillBotDatabaseBuilder databaseBuilder, FileStorageFactory fileStorageFactory,
IConfiguration configuration) : base(serviceProvider)
public UnverifyLogArchivationJob(IServiceProvider serviceProvider, IConfiguration configuration) : base(serviceProvider)
{
DatabaseBuilder = databaseBuilder;
FileStorageFactory = fileStorageFactory;
Configuration = configuration;
}

Expand Down Expand Up @@ -63,34 +59,43 @@ protected override async Task RunAsync(IJobExecutionContext context)
repository.Remove(item);
}

var zipName = await SaveDataAsync(logRoot);
var archiveSize = await SaveDataAsync(logRoot);
await repository.CommitAsync();

var xmlSize = Encoding.UTF8.GetBytes(logRoot.ToString()).Length.Bytes().ToString();
var zipSize = new FileInfo(zipName).Length.Bytes().ToString();
var zipSize = archiveSize.Bytes().ToString();

context.Result = BuildReport(xmlSize, zipSize, data);
}

private async Task<string> SaveDataAsync(XElement xml)
private async Task<long> SaveDataAsync(XElement xml)
{
var storage = FileStorageFactory.Create("Unverify");
var backupFilename = $"UnverifyLog_{DateTime.Now:yyyyMMdd_HHmmss}.xml";
var fileinfo = await storage.GetFileInfoAsync(backupFilename);
var xmlBaseName = $"UnverifyLog_{DateTime.Now:yyyyMMdd_HHmmss}";
var temporaryPath = Path.GetTempPath();
var zipName = $"{xmlBaseName}.zip";
var zipPath = Path.Combine(temporaryPath, zipName);
long archiveSize;

await using (var stream = fileinfo.OpenWrite())
try
{
await xml.SaveAsync(stream, SaveOptions.OmitDuplicateNamespaces | SaveOptions.DisableFormatting, CancellationToken.None);
using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
await AddXmlToZipAsync(zipArchive, xml, $"{xmlBaseName}.xml");
}

using var reader = File.OpenRead(zipPath);
var archiveManager = await BlobManagerFactoryHelper.CreateAsync(BlobConstants.UnverifyLogArchives);
await archiveManager.UploadAsync(zipName, reader);
}
finally
{
archiveSize = new FileInfo(zipPath).Length;

var zipFilename = Path.ChangeExtension(fileinfo.FullName, ".zip");
if (File.Exists(zipFilename)) File.Delete(zipFilename);

using var archive = ZipFile.Open(zipFilename, ZipArchiveMode.Create);
archive.CreateEntryFromFile(fileinfo.FullName, backupFilename, CompressionLevel.Optimal);
if (File.Exists(zipPath))
File.Delete(zipPath);
}

File.Delete(fileinfo.FullName);
return zipFilename;
return archiveSize;
}

private static string BuildReport(string xmlSize, string zipSize, List<Database.Entity.UnverifyLog> data)
Expand Down
1 change: 1 addition & 0 deletions src/GrillBot.Common/FileStorage/BlobConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public static class BlobConstants
{
public const string AuditLogDeletedAttachments = "audit-log-attachments";
public const string AuditLogArchives = "audit-log-archives";
public const string UnverifyLogArchives = "unverify-log-archives";
}

0 comments on commit f4b6c99

Please sign in to comment.