Return proper responses for missing seed ids

This commit is contained in:
2025-03-03 07:50:06 -06:00
parent a2dc4372bb
commit 8aa2fae591
5 changed files with 34 additions and 16 deletions

View File

@@ -15,6 +15,10 @@
private ILogger<AzureStorage> 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);

View File

@@ -30,7 +30,7 @@
private ILogger<Randomizer> 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<string, JsonElement>();
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);
}
}
}

View File

@@ -18,8 +18,8 @@
[Route("/generate")]
[HttpPost]
public ActionResult Generate(SeedSettings settings) {
var id = this.RandomizeService.RandomizeSeed(settings);
public async Task<ActionResult> Generate(SeedSettings settings) {
var id = await this.RandomizeService.RandomizeSeed(settings);
var url = string.Format("/seed/{0}", id);
return Accepted(url, id);
}

View File

@@ -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<string> 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;
}
}

View File

@@ -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<SeedService> logger) {
this.AzureStorage = azureStorage;
this.Logger = logger;
}
private AzureStorage AzureStorage { get; }
private ILogger<SeedService> Logger { get; }
public async Task<IDictionary<string, object>> GetSeed(string seedId) {
var files = await this.AzureStorage.GetFiles(seedId);
this.Logger.LogDebug("Found files: {@files}", files.Keys);
var result = new Dictionary<string, object>();
if (!files.TryGetValue("settings.json", out var settingsData)) {