-
Notifications
You must be signed in to change notification settings - Fork 520
Add start stop button on ServiceDetails page #948
base: main
Are you sure you want to change the base?
Changes from all commits
4e1981b
e473f59
5ff8ca7
91ee730
972e571
2b13b59
8cbfb05
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 | ||||
---|---|---|---|---|---|---|
|
@@ -178,7 +178,7 @@ service.Description.RunInfo is ProjectRunInfo project2 && | |||||
} | ||||||
} | ||||||
|
||||||
private void LaunchService(Application application, Service service) | ||||||
public void LaunchService(Application application, Service service) | ||||||
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.
Suggested change
|
||||||
{ | ||||||
var serviceDescription = service.Description; | ||||||
var processInfo = new ProcessInfo(new Task[service.Description.Replicas]); | ||||||
|
@@ -460,7 +460,7 @@ async Task RunApplicationAsync(IEnumerable<(int ExternalPort, int Port, string? | |||||
service.Items[typeof(ProcessInfo)] = processInfo; | ||||||
} | ||||||
|
||||||
private Task KillRunningProcesses(IDictionary<string, Service> services) | ||||||
public Task KillRunningProcesses(IDictionary<string, Service> services) | ||||||
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.
Suggested change
|
||||||
{ | ||||||
static Task KillProcessAsync(Service service) | ||||||
{ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,9 @@ namespace Microsoft.Tye.Hosting | |
public class TyeDashboardApi | ||
{ | ||
private readonly JsonSerializerOptions _options; | ||
private readonly ProcessRunner _processRunner; | ||
|
||
public TyeDashboardApi() | ||
public TyeDashboardApi(ProcessRunner processRunner) | ||
{ | ||
_options = new JsonSerializerOptions() | ||
{ | ||
|
@@ -34,6 +35,7 @@ public TyeDashboardApi() | |
}; | ||
|
||
_options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); | ||
_processRunner = processRunner; | ||
} | ||
|
||
public void MapRoutes(IEndpointRouteBuilder endpoints) | ||
|
@@ -42,6 +44,8 @@ public void MapRoutes(IEndpointRouteBuilder endpoints) | |
endpoints.MapGet("/api/v1/application", ApplicationIndex); | ||
endpoints.MapDelete("/api/v1/control", ControlPlaneShutdown); | ||
endpoints.MapGet("/api/v1/services", Services); | ||
endpoints.MapPost("/api/v1/services/{name}/stop", ServiceStop); | ||
endpoints.MapPost("/api/v1/services/{name}/start", ServiceStart); | ||
endpoints.MapGet("/api/v1/services/{name}", Service); | ||
endpoints.MapGet("/api/v1/logs/{name}", Logs); | ||
endpoints.MapGet("/api/v1/metrics", AllMetrics); | ||
|
@@ -128,6 +132,38 @@ private Task Service(HttpContext context) | |
return JsonSerializer.SerializeAsync(context.Response.Body, serviceJson, _options); | ||
} | ||
|
||
private Task ServiceStart(HttpContext context) | ||
{ | ||
var app = context.RequestServices.GetRequiredService<Application>(); | ||
|
||
var name = (string?)context.Request.RouteValues["name"]; | ||
if (!string.IsNullOrEmpty(name) && app.Services.TryGetValue(name, out var service)) | ||
{ | ||
_processRunner.LaunchService(app, service); | ||
} | ||
|
||
context.Response.Redirect($"/services/{name}"); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private async Task ServiceStop(HttpContext context) | ||
{ | ||
var app = context.RequestServices.GetRequiredService<Application>(); | ||
|
||
var name = (string?)context.Request.RouteValues["name"]; | ||
if (!string.IsNullOrEmpty(name) && app.Services.TryGetValue(name, out var service)) | ||
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. Again, if no service with given name is found, 404 and no redirect, please. |
||
{ | ||
var services = new Dictionary<string, Service>(); | ||
services.Add(name, service); | ||
await _processRunner.KillRunningProcesses(services); | ||
} | ||
|
||
context.Response.Redirect($"/services/{name}"); | ||
|
||
return; | ||
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. Redundant |
||
} | ||
|
||
private static V1Service CreateServiceJson(Service service) | ||
{ | ||
var description = service.Description; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,6 +204,13 @@ private IHost BuildWebApplication(Application application, HostOptions options, | |
}); | ||
}); | ||
services.AddSingleton(application); | ||
services.AddScoped(sp => | ||
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. Not sure if this is necessary. You should be able to access |
||
{ | ||
var server = sp.GetRequiredService<AspNetCore.Hosting.Server.IServer>(); | ||
var addressFeature = server.Features.Get<AspNetCore.Hosting.Server.Features.IServerAddressesFeature>(); | ||
var baseAddress = addressFeature?.Addresses.FirstOrDefault() ?? string.Empty; | ||
return new System.Net.Http.HttpClient { BaseAddress = new Uri(baseAddress) }; | ||
}); | ||
}) | ||
.Build(); | ||
} | ||
|
@@ -218,14 +225,17 @@ private void ConfigureApplication(IApplicationBuilder app) | |
|
||
app.UseRouting(); | ||
|
||
var api = new TyeDashboardApi(); | ||
|
||
app.UseEndpoints(endpoints => | ||
if (_logger != null && _replicaRegistry != null) | ||
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. How would these be null? 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. Compiler shows null warnings 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. I am not sure if it makes a lot of difference to delay creation of |
||
{ | ||
api.MapRoutes(endpoints); | ||
endpoints.MapBlazorHub(); | ||
endpoints.MapFallbackToPage("/_Host"); | ||
}); | ||
var api = new TyeDashboardApi(new ProcessRunner(_logger, _replicaRegistry, ProcessRunnerOptions.FromHostOptions(_options))); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
api.MapRoutes(endpoints); | ||
endpoints.MapBlazorHub(); | ||
endpoints.MapFallbackToPage("/_Host"); | ||
}); | ||
} | ||
} | ||
|
||
private int ComputePort(int? port) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my other comment regarding
HttpClient
injection. Can you try to see if you can get away with creating aHttpClient
instance directly?