-
Notifications
You must be signed in to change notification settings - Fork 708
Make RedisInsight work with WithLifetime(...). #6425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Globalization; | ||
| using System.Net.Http.Json; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using Aspire.Hosting.Redis; | ||
| using Aspire.Hosting.Utils; | ||
|
|
@@ -192,20 +194,50 @@ public static IResourceBuilder<RedisResource> WithRedisInsight(this IResourceBui | |
| return builder; | ||
| } | ||
|
|
||
| static async Task ImportRedisDatabases(ILogger resourceLogger, IEnumerable<RedisResource> redisInstances, HttpClient client, CancellationToken ct) | ||
| static async Task ImportRedisDatabases(ILogger resourceLogger, IEnumerable<RedisResource> redisInstances, HttpClient client, CancellationToken cancellationToken) | ||
| { | ||
| var databasesPath = "/api/databases"; | ||
|
|
||
| var pipeline = new ResiliencePipelineBuilder().AddRetry(new Polly.Retry.RetryStrategyOptions | ||
| { | ||
| Delay = TimeSpan.FromSeconds(2), | ||
| MaxRetryAttempts = 5, | ||
| }).Build(); | ||
|
|
||
| using (var stream = new MemoryStream()) | ||
| { | ||
| // As part of configuring RedisInsight we need to factor in the possibility that the | ||
| // container resource is being run with persistence turned on. In this case we need | ||
| // to get the list of existing databases because we might need to delete some. | ||
| var lookup = await pipeline.ExecuteAsync(async (ctx) => | ||
| { | ||
| var getDatabasesResponse = await client.GetFromJsonAsync<RedisDatabaseDto[]>(databasesPath, cancellationToken).ConfigureAwait(false); | ||
| return getDatabasesResponse?.ToLookup( | ||
| i => i.Name ?? throw new InvalidDataException("Database name is missing."), | ||
| i => i.Id ?? throw new InvalidDataException("Database ID is missing.")); | ||
| }, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| var databasesToDelete = new List<Guid>(); | ||
|
|
||
| using var writer = new Utf8JsonWriter(stream); | ||
|
|
||
| writer.WriteStartArray(); | ||
|
|
||
| foreach (var redisResource in redisInstances) | ||
| { | ||
| if (lookup is { } && lookup.Contains(redisResource.Name)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we deleting the servers that are already there, only to add them again? Wouldn't the more appropriate approach be "if the server already exists, don't add it again" ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the credentials could have changed. Using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add this in a comment. I don't think this algorithm is intuitive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Did this ever happen? |
||
| { | ||
| // It is possible that there are multiple databases with | ||
| // a conflicting name so we delete them all. This just keeps | ||
| // track of the specific ID that we need to delete. | ||
| databasesToDelete.AddRange(lookup[redisResource.Name]); | ||
| } | ||
|
|
||
| if (redisResource.PrimaryEndpoint.IsAllocated) | ||
| { | ||
| var endpoint = redisResource.PrimaryEndpoint; | ||
| writer.WriteStartObject(); | ||
|
|
||
| writer.WriteString("host", redisResource.Name); | ||
| writer.WriteNumber("port", endpoint.TargetPort!.Value); | ||
| writer.WriteString("name", redisResource.Name); | ||
|
|
@@ -218,7 +250,7 @@ static async Task ImportRedisDatabases(ILogger resourceLogger, IEnumerable<Redis | |
| } | ||
| } | ||
| writer.WriteEndArray(); | ||
| await writer.FlushAsync(ct).ConfigureAwait(false); | ||
| await writer.FlushAsync(cancellationToken).ConfigureAwait(false); | ||
| stream.Seek(0, SeekOrigin.Begin); | ||
|
|
||
| var content = new MultipartFormDataContent(); | ||
|
|
@@ -227,23 +259,39 @@ static async Task ImportRedisDatabases(ILogger resourceLogger, IEnumerable<Redis | |
|
|
||
| content.Add(fileContent, "file", "RedisInsight_connections.json"); | ||
|
|
||
| var apiUrl = $"/api/databases/import"; | ||
|
|
||
| var pipeline = new ResiliencePipelineBuilder().AddRetry(new Polly.Retry.RetryStrategyOptions | ||
| { | ||
| Delay = TimeSpan.FromSeconds(2), | ||
| MaxRetryAttempts = 5, | ||
| }).Build(); | ||
| var apiUrl = $"{databasesPath}/import"; | ||
|
|
||
| try | ||
| { | ||
| if (databasesToDelete.Any()) | ||
| { | ||
| await pipeline.ExecuteAsync(async (ctx) => | ||
| { | ||
| // Create a DELETE request to send to the existing instance of | ||
| // RedisInsight with the IDs of the database to delete. | ||
| var deleteContent = JsonContent.Create(new | ||
| { | ||
| ids = databasesToDelete | ||
| }); | ||
|
|
||
| var deleteRequest = new HttpRequestMessage(HttpMethod.Delete, databasesPath) | ||
| { | ||
| Content = deleteContent | ||
| }; | ||
|
|
||
| var deleteResponse = await client.SendAsync(deleteRequest, cancellationToken).ConfigureAwait(false); | ||
| deleteResponse.EnsureSuccessStatusCode(); | ||
|
|
||
| }, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| await pipeline.ExecuteAsync(async (ctx) => | ||
| { | ||
| var response = await client.PostAsync(apiUrl, content, ctx) | ||
| .ConfigureAwait(false); | ||
|
|
||
| response.EnsureSuccessStatusCode(); | ||
| }, ct).ConfigureAwait(false); | ||
| }, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| } | ||
| catch (Exception ex) | ||
|
|
@@ -254,6 +302,15 @@ await pipeline.ExecuteAsync(async (ctx) => | |
| } | ||
| } | ||
|
|
||
| private class RedisDatabaseDto | ||
| { | ||
| [JsonPropertyName("id")] | ||
| public Guid? Id { get; set; } | ||
|
|
||
| [JsonPropertyName("name")] | ||
| public string? Name { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the host port that the Redis Commander resource is exposed on instead of using randomly assigned port. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Dcp; | ||
|
|
||
| namespace Aspire.Hosting.Tests.Dcp; | ||
|
|
||
| public class ApplicationExecutorProxy | ||
| { | ||
| internal ApplicationExecutorProxy(ApplicationExecutor executor) | ||
| { | ||
| _executor = executor; | ||
| } | ||
|
|
||
| private readonly ApplicationExecutor _executor; | ||
|
|
||
| public Task StartResourceAsync(string resourceName, CancellationToken cancellationToken) => _executor.StartResourceAsync(resourceName, cancellationToken); | ||
|
|
||
| public Task StopResourceAsync(string resourceName, CancellationToken cancellationToken) => _executor.StopResourceAsync(resourceName, cancellationToken); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.