diff --git a/ALttPRandomizer/Azure/AzureStorage.cs b/ALttPRandomizer/Azure/AzureStorage.cs index d6da412..7d94e60 100644 --- a/ALttPRandomizer/Azure/AzureStorage.cs +++ b/ALttPRandomizer/Azure/AzureStorage.cs @@ -15,6 +15,10 @@ private ILogger Logger { get; } private BlobContainerClient BlobClient { get; } + public async Task DeleteFile(string name) { + await BlobClient.DeleteBlobAsync(name); + } + public async Task UploadFile(string name, Stream data) { await BlobClient.UploadBlobAsync(name, data); } @@ -41,9 +45,6 @@ await foreach (var blob in blobs) { var result = await this.BlobClient.GetBlobClient(blob.Name).DownloadContentAsync(); - if (result.Value.Details.ContentLength == 0) { - continue; - } if (!blob.Name.StartsWith(prefix)) { this.Logger.LogWarning("Found prefix mismatch for seed id {seedId}, blob name {blobName}", seedId, blob.Name); diff --git a/ALttPRandomizer/Randomizer.cs b/ALttPRandomizer/Randomizer.cs index d3372e3..67bcf3e 100644 --- a/ALttPRandomizer/Randomizer.cs +++ b/ALttPRandomizer/Randomizer.cs @@ -30,7 +30,7 @@ private ILogger Logger { get; } private ServiceOptions Configuration => this.OptionsMonitor.CurrentValue; - public void Randomize(string id, SeedSettings settings) { + public async Task Randomize(string id, SeedSettings settings) { this.Logger.LogDebug("Recieved request for id {id} to randomize settings {@settings}", id, settings); var start = new ProcessStartInfo() { @@ -79,11 +79,20 @@ var exitcode = process.ExitCode; if (exitcode != 0) { - this.GenerationFailed(id, exitcode); + await this.GenerationFailed(id, exitcode); } else { await this.GenerationSucceeded(id, settings); } }; + + var settingsJson = JsonSerializer.SerializeToDocument(settings, JsonOptions.Default); + var settingsOut = string.Format("{0}/settings.json", id); + var uploadSettings = this.AzureStorage.UploadFile(settingsOut, new BinaryData(settingsJson)); + + var generating = string.Format("{0}/generating", id); + var uploadGenerating = this.AzureStorage.UploadFile(generating, BinaryData.Empty); + + await Task.WhenAll(uploadSettings, uploadGenerating); } private async Task GenerationSucceeded(string id, SeedSettings settings) { @@ -102,11 +111,10 @@ var meta = this.ProcessMetadata(metaIn); var uploadMeta = this.AzureStorage.UploadFile(metaOut, new BinaryData(meta)); - var settingsJson = JsonSerializer.SerializeToDocument(settings, JsonOptions.Default); - var settingsOut = string.Format("{0}/settings.json", id); - var uploadSettings = this.AzureStorage.UploadFile(settingsOut, new BinaryData(settingsJson)); + var generating = string.Format("{0}/generating", id); + var deleteGenerating = this.AzureStorage.DeleteFile(generating); - await Task.WhenAll(uploadPatch, uploadSpoiler, uploadMeta, uploadSettings); + await Task.WhenAll(uploadPatch, uploadSpoiler, uploadMeta, deleteGenerating); this.Logger.LogDebug("Deleting file {filepath}", metaIn); File.Delete(metaIn); @@ -123,7 +131,6 @@ orig = JsonDocument.Parse(file); } - var processed = new Dictionary(); foreach (var toplevel in orig.RootElement.EnumerateObject()) { var value = toplevel.Value; @@ -137,7 +144,11 @@ return JsonSerializer.SerializeToDocument(processed, JsonOptions.Default); } - private void GenerationFailed(string id, int exitcode) { + private async Task GenerationFailed(string id, int exitcode) { + var generating = string.Format("{0}/generating", id); + var deleteGenerating = this.AzureStorage.DeleteFile(generating); + + await Task.WhenAll(deleteGenerating); } } } diff --git a/ALttPRandomizer/SeedController.cs b/ALttPRandomizer/SeedController.cs index e771f0e..32de1f0 100644 --- a/ALttPRandomizer/SeedController.cs +++ b/ALttPRandomizer/SeedController.cs @@ -18,8 +18,8 @@ [Route("/generate")] [HttpPost] - public ActionResult Generate(SeedSettings settings) { - var id = this.RandomizeService.RandomizeSeed(settings); + public async Task Generate(SeedSettings settings) { + var id = await this.RandomizeService.RandomizeSeed(settings); var url = string.Format("/seed/{0}", id); return Accepted(url, id); } diff --git a/ALttPRandomizer/Service/RandomizeService.cs b/ALttPRandomizer/Service/RandomizeService.cs index e7316ae..7ea4fbe 100644 --- a/ALttPRandomizer/Service/RandomizeService.cs +++ b/ALttPRandomizer/Service/RandomizeService.cs @@ -1,5 +1,6 @@ using ALttPRandomizer.Model; using Microsoft.Extensions.Logging; +using System.Threading.Tasks; namespace ALttPRandomizer.Service { public class RandomizeService { @@ -14,10 +15,10 @@ namespace ALttPRandomizer.Service { private IdGenerator IdGenerator { get; } private Randomizer Randomizer { get; } - public string RandomizeSeed(SeedSettings settings) { + public async Task RandomizeSeed(SeedSettings settings) { var id = this.IdGenerator.GenerateId(); this.Logger.LogInformation("Generating seed {seedId} with settings {@settings}", id, settings); - this.Randomizer.Randomize(id, settings); + await this.Randomizer.Randomize(id, settings); return id; } } diff --git a/ALttPRandomizer/Service/SeedService.cs b/ALttPRandomizer/Service/SeedService.cs index 122392a..5d0dc7e 100644 --- a/ALttPRandomizer/Service/SeedService.cs +++ b/ALttPRandomizer/Service/SeedService.cs @@ -1,21 +1,26 @@ namespace ALttPRandomizer.Service { using ALttPRandomizer.Azure; using ALttPRandomizer.Model; + using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Text.Json; using System.Threading.Tasks; public class SeedService { - public SeedService(AzureStorage azureStorage) { + public SeedService(AzureStorage azureStorage, ILogger logger) { this.AzureStorage = azureStorage; + this.Logger = logger; } private AzureStorage AzureStorage { get; } + private ILogger Logger { get; } public async Task> GetSeed(string seedId) { var files = await this.AzureStorage.GetFiles(seedId); + this.Logger.LogDebug("Found files: {@files}", files.Keys); + var result = new Dictionary(); if (!files.TryGetValue("settings.json", out var settingsData)) {