forked from masastack/MASA.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add MASA.Utils.Development.Dapr * chore: dapr init * chore: Stop dapr process after project is stopped * chore: Increase get pid based on port * chore: add dapr daemon, restart dapr after dapr is shut down abnormally, Added OSX system port handling * chore: dapr supports the default AppPort obtained from ApplicationUrl by default, automatically assigns the grpc and http ports of dapr and automatically supplements environment variables Supports automatic restart after the dapr process is closed, and the configuration after restart is consistent with the last successful startup configuration Supports automatic restart of the dapr process after configuration update. Currently, there are no restrictions on HttpPort and GrpcPort. However, if the configuration update changes HttpPort and GrpcPort, the port obtained by DaprClient will be inconsistent with the actual running one. It needs to be adjusted later here. * chore: remove redundant code * chore: remove bad code * fix: Fix AddDapr method enable dapr slidecar error * feature: add dapr background options * feature: add dapr background options * feature: add dapr background options * chore: Modify log Co-authored-by: zhenlei520 <[email protected]>
- Loading branch information
1 parent
bb2e838
commit 1ee722e
Showing
32 changed files
with
1,491 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) MASA Stack | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
38 changes: 38 additions & 0 deletions
38
src/Development/MASA.Utils.Development.Dapr.AspNetCore/DaprBackgroundOptions.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,38 @@ | ||
namespace MASA.Utils.Development.Dapr.AspNetCore; | ||
|
||
public class DaprBackgroundOptions | ||
{ | ||
private int _maxRetryTimes = 5; | ||
|
||
/// <summary> | ||
/// default: 5 | ||
/// </summary> | ||
public int MaxRetryTimes | ||
{ | ||
get => _maxRetryTimes; | ||
set | ||
{ | ||
if (value <= 0) | ||
throw new ArgumentException($"{nameof(MaxRetryTimes)} Must be greater than 0"); | ||
|
||
_maxRetryTimes = value; | ||
} | ||
} | ||
|
||
private int _defaultTime = 3000; | ||
|
||
/// <summary> | ||
/// default: 3000ms | ||
/// </summary> | ||
public int Time | ||
{ | ||
get => _defaultTime; | ||
set | ||
{ | ||
if (value <= 0) | ||
throw new ArgumentException($"{nameof(MaxRetryTimes)} Must be greater than 0"); | ||
|
||
_defaultTime = value; | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Development/MASA.Utils.Development.Dapr.AspNetCore/DaprBackgroundService.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,76 @@ | ||
namespace MASA.Utils.Development.Dapr.AspNetCore; | ||
|
||
public class DaprBackgroundService : IHostedService | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly IDaprProcess _daprProcess; | ||
private readonly DaprOptions _options; | ||
private readonly DaprBackgroundOptions _daprBackgroundOptions; | ||
private readonly ILogger<DaprBackgroundService>? _logger; | ||
|
||
public DaprBackgroundService( | ||
IServiceProvider serviceProvider, | ||
IDaprProcess daprProcess, | ||
IOptionsMonitor<DaprOptions> options, | ||
IOptions<DaprBackgroundOptions> daprBackgroundOptions, | ||
ILogger<DaprBackgroundService>? logger) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_daprProcess = daprProcess; | ||
_options = options.CurrentValue; | ||
options.OnChange(daprOptions => | ||
{ | ||
daprOptions.AppPort ??= GetAppPort(daprOptions); | ||
_daprProcess.Refresh(daprOptions); | ||
}); | ||
_daprBackgroundOptions = daprBackgroundOptions.Value; | ||
_logger = logger; | ||
} | ||
|
||
private ushort GetAppPort(DaprOptions options) | ||
{ | ||
int retry = 0; | ||
again: | ||
var server = _serviceProvider.GetRequiredService<IServer>(); | ||
var addresses = server.Features.Get<IServerAddressesFeature>()?.Addresses; | ||
if (addresses is { IsReadOnly: false, Count: 0 }) | ||
{ | ||
if (retry < _daprBackgroundOptions.MaxRetryTimes) | ||
{ | ||
_logger?.LogWarning("dapr: failed to get app running port, retrying, please wait..."); | ||
retry++; | ||
goto again; | ||
} | ||
throw new Exception("Failed to get the startup port, please specify the port manually"); | ||
} | ||
|
||
return addresses! | ||
.Select(address => new Uri(address)) | ||
.Where(address | ||
=> (options.EnableSsl is true && address.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase)) | ||
|| address.Scheme.Equals(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase)) | ||
.Select(address => (ushort)address.Port).FirstOrDefault(); | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger?.LogInformation("{Name} is Starting ...", nameof(DaprBackgroundService)); | ||
System.Timers.Timer timer = new System.Timers.Timer(_daprBackgroundOptions.Time); | ||
timer.Elapsed += delegate | ||
{ | ||
_options.AppPort ??= GetAppPort(_options); | ||
_daprProcess.Start(_options, cancellationToken); | ||
}; | ||
timer.AutoReset = false; | ||
timer.Enabled = true; | ||
timer.Start(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger?.LogInformation("{Name} is Stopping...", nameof(DaprBackgroundService)); | ||
_daprProcess.Stop(cancellationToken); | ||
return Task.CompletedTask; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ment/MASA.Utils.Development.Dapr.AspNetCore/MASA.Utils.Development.Dapr.AspNetCore.csproj
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\MASA.Utils.Development.Dapr\MASA.Utils.Development.Dapr.csproj" /> | ||
</ItemGroup> | ||
</Project> |
49 changes: 49 additions & 0 deletions
49
src/Development/MASA.Utils.Development.Dapr.AspNetCore/ServiceCollectionExtensions.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,49 @@ | ||
namespace MASA.Utils.Development.Dapr.AspNetCore; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDapr( | ||
this IServiceCollection services, | ||
Action<DaprOptions>? action = null, | ||
Action<DaprBackgroundOptions>? daprBackgroundOptionsAction = null) | ||
{ | ||
DaprOptions daprOptions = new(); | ||
action?.Invoke(daprOptions); | ||
return services.AddDapr(() => daprOptions, daprBackgroundOptionsAction); | ||
} | ||
|
||
public static IServiceCollection AddDapr(this IServiceCollection services, | ||
Func<DaprOptions> func, | ||
Action<DaprBackgroundOptions>? action = null) | ||
{ | ||
if (services.Any(service => service.ImplementationType == typeof(DaprService))) | ||
{ | ||
return services; | ||
} | ||
services.AddSingleton<DaprService>(); | ||
|
||
if (action != null) | ||
services.Configure(action); | ||
|
||
services.AddHostedService<DaprBackgroundService>(); | ||
services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(15)); | ||
return services.AddDaprCore(func); | ||
} | ||
|
||
public static IServiceCollection AddDapr(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
if (services.Any(service => service.ImplementationType == typeof(DaprService))) | ||
{ | ||
return services; | ||
} | ||
services.AddSingleton<DaprService>(); | ||
services.AddHostedService<DaprBackgroundService>(); | ||
services.Configure<DaprBackgroundOptions>(configuration); | ||
return services.AddDaprCore(configuration); | ||
} | ||
|
||
private class DaprService | ||
{ | ||
|
||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Development/MASA.Utils.Development.Dapr.AspNetCore/_Imports.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,7 @@ | ||
global using Microsoft.AspNetCore.Hosting.Server; | ||
global using Microsoft.AspNetCore.Hosting.Server.Features; | ||
global using Microsoft.Extensions.Configuration; | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.Extensions.Hosting; | ||
global using Microsoft.Extensions.Logging; | ||
global using Microsoft.Extensions.Options; |
26 changes: 26 additions & 0 deletions
26
src/Development/MASA.Utils.Development.Dapr/CommandLineBuilder.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,26 @@ | ||
namespace MASA.Utils.Development.Dapr; | ||
|
||
public class CommandLineBuilder | ||
{ | ||
public string Prefix { get; } | ||
|
||
public List<string> Arguments { get; set; } | ||
|
||
public CommandLineBuilder(string prefix) | ||
{ | ||
Prefix = prefix; | ||
Arguments = new(); | ||
} | ||
|
||
public CommandLineBuilder Add(string name, string value, bool isSkip = false) | ||
{ | ||
if (!isSkip) | ||
{ | ||
Arguments.Add($"{Prefix}{name} {value}"); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public override string ToString() => string.Join(' ', Arguments); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Development/MASA.Utils.Development.Dapr/Configurations/DaprRuntimeOptions.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,25 @@ | ||
namespace MASA.Utils.Development.Dapr.Configurations; | ||
|
||
public class DaprRuntimeOptions | ||
{ | ||
[JsonPropertyName("appId")] | ||
public string AppId { get; set; } = default!; | ||
|
||
[JsonPropertyName("httpPort")] | ||
public ushort HttpPort { get; set; } = default!; | ||
|
||
[JsonPropertyName("grpcPort")] | ||
public ushort GrpcPort { get; set; } = default!; | ||
|
||
[JsonPropertyName("appPort")] | ||
public ushort AppPort { get; set; } = default!; | ||
|
||
[JsonPropertyName("metricsEnabled")] | ||
public bool MetricsEnabled { get; set; } = default!; | ||
|
||
[JsonPropertyName("command")] | ||
public string Command { get; set; } = default!; | ||
|
||
[JsonPropertyName("pid")] | ||
public int PId { get; set; } = default!; | ||
} |
Oops, something went wrong.