-
Notifications
You must be signed in to change notification settings - Fork 17
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
Lobs communication spike #947
Draft
tleed5
wants to merge
1
commit into
main
Choose a base branch
from
tl/Lobs-communication-spike
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,20 @@ | |
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Security.Principal; | ||
using Autofac; | ||
using Autofac.Extensions.DependencyInjection; | ||
#if !NETFRAMEWORK | ||
using Microsoft.AspNetCore.Server.Kestrel.Https; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Server.Kestrel.Core; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Octopus.Tentacle.Communications.gRPC; | ||
#endif | ||
using Octopus.Diagnostics; | ||
using Octopus.Tentacle.Background; | ||
using Octopus.Tentacle.Communications; | ||
|
@@ -41,6 +54,11 @@ public class RunAgentCommand : AbstractStandardCommand | |
|
||
public override bool CanRunAsService => true; | ||
|
||
readonly ILifetimeScope container; | ||
|
||
#if !NETFRAMEWORK | ||
private IHost host; | ||
#endif | ||
public RunAgentCommand( | ||
Lazy<IHalibutInitializer> halibut, | ||
Lazy<IWritableTentacleConfiguration> configuration, | ||
|
@@ -53,7 +71,7 @@ public RunAgentCommand( | |
IWindowsLocalAdminRightsChecker windowsLocalAdminRightsChecker, | ||
AppVersion appVersion, | ||
ILogFileOnlyLogger logFileOnlyLogger, | ||
IEnumerable<Lazy<IBackgroundTask>> backgroundTasks) : base(selector, log, logFileOnlyLogger) | ||
IEnumerable<Lazy<IBackgroundTask>> backgroundTasks, ILifetimeScope container) : base(selector, log, logFileOnlyLogger) | ||
{ | ||
this.halibut = halibut; | ||
this.configuration = configuration; | ||
|
@@ -66,6 +84,11 @@ public RunAgentCommand( | |
this.windowsLocalAdminRightsChecker = windowsLocalAdminRightsChecker; | ||
this.appVersion = appVersion; | ||
this.backgroundTasks = backgroundTasks; | ||
this.container = container; | ||
|
||
#if !NETFRAMEWORK | ||
host = CreateHostBuilder().Build(); | ||
#endif | ||
|
||
Options.Add("wait=", "Delay (ms) before starting", arg => wait = int.Parse(arg)); | ||
Options.Add("console", "Don't attempt to run as a service, even if the user is non-interactive", v => | ||
|
@@ -74,6 +97,74 @@ public RunAgentCommand( | |
// This option is added to show help | ||
}); | ||
} | ||
#if !NETFRAMEWORK | ||
private IHostBuilder CreateHostBuilder() => | ||
Host.CreateDefaultBuilder() | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseKestrel(options => | ||
{ | ||
// options.ConfigureHttpsDefaults(httpsOptions => | ||
// { | ||
// httpsOptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate; | ||
// httpsOptions.ClientCertificateValidation = (certificate, _, __) => | ||
// { | ||
// using var cert = new X509Certificate2(certificate.Export(X509ContentType.Cert)); | ||
// return configuration.Value.TrustedOctopusServers.Any(serverConfiguration => serverConfiguration.Thumbprint == cert.Thumbprint); | ||
// }; | ||
// }); | ||
options.Limits.MaxRequestBodySize = null; | ||
|
||
options.ListenAnyIP(5001, listenOptions => | ||
{ | ||
listenOptions.Protocols = HttpProtocols.Http1; | ||
// listenOptions.UseHttps(configuration.Value.TentacleCertificate!); | ||
}); | ||
options.ListenAnyIP(5002, listenOptions => | ||
{ | ||
listenOptions.Protocols = HttpProtocols.Http2; | ||
// listenOptions.UseHttps(configuration.Value.TentacleCertificate!); | ||
}); | ||
}); | ||
webBuilder.UseStartup<Startup>(); | ||
}).UseServiceProviderFactory(new AutofacChildLifetimeScopeServiceProviderFactory(container)); | ||
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. Had to update Autofac so I could easily get the existing container services into the Kestrel's container to allow for DI in the gRPC service |
||
|
||
class Startup | ||
{ | ||
// This method gets called by the runtime. Use this method to add services to the container. | ||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddGrpcReflection(); | ||
services.AddGrpc(options => | ||
{ | ||
options.MaxReceiveMessageSize = null; | ||
options.MaxSendMessageSize = null; | ||
}); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
// if (env.IsDevelopment()) | ||
// { | ||
app.UseDeveloperExceptionPage(); | ||
// } | ||
|
||
app.UseRouting(); | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapGrpcReflectionService(); | ||
endpoints.MapGrpcService<GreeterService>(); | ||
endpoints.MapGet("/", async context => | ||
{ | ||
await context.Response.WriteAsync( | ||
"Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); | ||
}); | ||
}); | ||
} | ||
} | ||
#endif | ||
|
||
protected override void Start() | ||
{ | ||
|
@@ -128,6 +219,9 @@ protected override void Start() | |
|
||
halibut.Value.Start(); | ||
halibutHasStarted = true; | ||
#if !NETFRAMEWORK | ||
host.Start(); | ||
#endif | ||
|
||
foreach (var backgroundTaskLazy in backgroundTasks) | ||
{ | ||
|
@@ -156,10 +250,16 @@ protected override void Stop() | |
halibut.Value.Stop(); | ||
} | ||
|
||
#if !NETFRAMEWORK | ||
Console.WriteLine("Stopping host"); | ||
host.StopAsync().GetAwaiter().GetResult(); | ||
host.Dispose(); | ||
#endif | ||
|
||
foreach (var backgroundTaskLazy in backgroundTasks.Where(bt => bt.IsValueCreated)) | ||
{ | ||
backgroundTaskLazy.Value.Stop(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Octopus.Tentacle.Communications | ||
{ | ||
public interface IMyEchoService | ||
{ | ||
string SayHello(string name); | ||
} | ||
|
||
public interface IAsyncClientMyEchoService | ||
{ | ||
Task<string> SayHelloAsync(string name); | ||
} | ||
|
||
public interface IAsyncMyEchoService | ||
{ | ||
Task<string> SayHelloAsync(string name, CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
source/Octopus.Tentacle/Communications/gRPC/GreeterService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#if !NETFRAMEWORK | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Grpc.Core; | ||
using Halibut; | ||
|
||
namespace Octopus.Tentacle.Communications.gRPC | ||
{ | ||
public class GreeterService: Greeter.GreeterBase | ||
{ | ||
readonly HalibutRuntime halibut; | ||
|
||
public GreeterService(HalibutRuntime halibut) | ||
{ | ||
this.halibut = halibut; | ||
} | ||
|
||
|
||
public override Task<HelloReply> SayHello( | ||
HelloRequest request, ServerCallContext context) | ||
{ | ||
var client = halibut.CreateAsyncClient<IMyEchoService, IAsyncClientMyEchoService>(HalibutInitializer.ServiceEndPoints.First()); | ||
|
||
Task.Run(async () => | ||
{ | ||
await client.SayHelloAsync("Hello from: " + request.Name); | ||
}); | ||
|
||
return Task.FromResult(new HelloReply | ||
{ | ||
Message = "Hello " + request.Name | ||
}); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is just so I could get it running locally on my mac https://learn.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-8.0#unable-to-start-aspnet-core-grpc-app-on-macos