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