Skip to content

Commit 404a5ad

Browse files
committed
fix(DaprStarter): Fix Refresh method
1 parent 4230fd1 commit 404a5ad

File tree

4 files changed

+105
-74
lines changed

4 files changed

+105
-74
lines changed

src/Contrib/Development/Masa.Contrib.Development.DaprStarters.AspNetCore/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static IServiceCollection AddDaprStarter(
2222
Action<DaprOptions> daprOptionAction,
2323
bool isDelay = true)
2424
{
25-
ArgumentNullException.ThrowIfNull(daprOptionAction, nameof(daprOptionAction));
25+
ArgumentNullException.ThrowIfNull(daprOptionAction);
2626

2727
return services.AddDaprStarter(() =>
2828
{

src/Contrib/Development/Masa.Contrib.Development.DaprStarters/DaprProcess.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void CheckAndCompleteDaprEnvironment(string? data)
110110
/// </summary>
111111
private void CompleteDaprEnvironment(ushort httpPort, ushort grpcPort, Action action)
112112
{
113-
if (CompleteDaprHttpPortEnvironment(httpPort) & CompleteDaprGrpcPortEnvironment(grpcPort))
113+
if (CompleteDaprHttpPortEnvironment(httpPort) && CompleteDaprGrpcPortEnvironment(grpcPort))
114114
{
115115
action.Invoke();
116116
_logger?.LogInformation(
@@ -161,6 +161,11 @@ public void Refresh(DaprOptions options)
161161

162162
if (SuccessDaprOptions != null)
163163
{
164+
options.AppPort = SuccessDaprOptions.AppPort;
165+
options.EnableSsl = SuccessDaprOptions.EnableSsl;
166+
options.DaprHttpPort = SuccessDaprOptions.DaprHttpPort;
167+
options.DaprGrpcPort = SuccessDaprOptions.DaprGrpcPort;
168+
164169
UpdateStatus(DaprProcessStatus.Restarting);
165170
_logger?.LogDebug(
166171
"Dapr configuration refresh, Dapr AppId is {AppId}, closing dapr, please wait...",
@@ -249,5 +254,14 @@ private void UpdateStatus(DaprProcessStatus status)
249254
}
250255
}
251256

252-
public void Dispose() => Stop();
257+
public void Dispose()
258+
{
259+
Dispose(true);
260+
GC.SuppressFinalize(this);
261+
}
262+
263+
protected virtual void Dispose(bool disposing)
264+
{
265+
Stop();
266+
}
253267
}

src/Contrib/Development/Masa.Contrib.Development.DaprStarters/DaprProvider.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public List<DaprRuntimeOptions> GetDaprList(string appId)
4545
}
4646
catch (Exception exception)
4747
{
48-
_logger?.LogWarning(exception, "----- Error getting list of running dapr, response message is {response}", response);
48+
_logger?.LogWarning(exception, "----- Error getting list of running dapr, response message is {Response}", response);
4949
return new List<DaprRuntimeOptions>();
5050
}
5151
return daprList.Where(dapr => dapr.AppId == appId).ToList();
@@ -61,7 +61,7 @@ public Process DaprStart(string arguments,
6161
processUtils.OutputDataReceived += delegate(object? sender, DataReceivedEventArgs args)
6262
{
6363
outputDataReceivedAction.Invoke(sender, args);
64-
DaprProcess_OutputDataReceived(sender, args);
64+
DaprProcess_OutputDataReceived(args);
6565
};
6666
processUtils.ErrorDataReceived += DaprProcess_ErrorDataReceived;
6767
processUtils.Exit += delegate
@@ -72,7 +72,7 @@ public Process DaprStart(string arguments,
7272
return processUtils.Run(Constant.DEFAULT_FILE_NAME, $"run {arguments}", createNoWindow);
7373
}
7474

75-
private static void DaprProcess_OutputDataReceived(object? sender, DataReceivedEventArgs e)
75+
private static void DaprProcess_OutputDataReceived(DataReceivedEventArgs e)
7676
{
7777
if (e.Data == null) return;
7878

@@ -96,8 +96,6 @@ private static void DaprProcess_OutputDataReceived(object? sender, DataReceivedE
9696
case "fatal":
9797
Console.ForegroundColor = ConsoleColor.Red;
9898
break;
99-
default:
100-
break;
10199
}
102100

103101
Console.WriteLine(e.Data);
@@ -116,7 +114,11 @@ private static void DaprProcess_ErrorDataReceived(object? sender, DataReceivedEv
116114

117115
public void DaprStop(string appId)
118116
{
119-
var process = Process.Start(@$"{Constant.DEFAULT_FILE_NAME}", $"stop {appId}");
117+
var process = new ProcessUtils(_loggerFactory).Run(
118+
$"{Constant.DEFAULT_FILE_NAME}",
119+
$"stop {appId}",
120+
createNoWindow: false,
121+
isWait: false);
120122
process.WaitForExit();
121123
}
122124

src/Contrib/Development/Masa.Contrib.Development.DaprStarters/Process/ProcessProvider.cs

Lines changed: 80 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,88 +30,74 @@ public List<int> GetPidByPort(ushort port)
3030
{
3131
List<int> pIdList = new();
3232
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
33+
pIdList = GetPidByPortByWindows(port);
34+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
35+
pIdList = GetPidByPortByLinux(port);
36+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
37+
pIdList = GetPidByPortByOsx(port);
38+
else
3339
{
34-
List<string> output = GetResponse("netstat", $"-a -n -o", "\r\n");
35-
36-
foreach (var line in output)
37-
{
38-
if (line.Trim().StartsWith("Proto") || line.Trim().StartsWith("协议"))
39-
continue;
40+
_logger?.LogError("unsupported operating system");
41+
}
4042

41-
var parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
43+
return pIdList.Where(pid => pid > 0).ToList();
44+
}
4245

43-
var len = parts.Length;
44-
if (len > 2)
45-
{
46-
var pId = int.Parse(parts[len - 1].Split('/')[0]);
47-
if (int.Parse(parts[1].Split(':').Last()) == port && !pIdList.Contains(pId))
48-
{
49-
pIdList.Add(pId);
50-
}
51-
}
52-
}
53-
}
54-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
46+
private static List<int> GetPidByPortByWindows(ushort port)
47+
{
48+
List<int> pIdList = new();
49+
List<string> output = GetResponse("netstat", $"-a -n -o", "\r\n");
50+
foreach (var line in output)
5551
{
56-
List<string> output = GetResponse("netstat", $"-tunlp", "\n");
57-
58-
_logger?.LogDebug("{Result} by netstat on linux", System.Text.Json.JsonSerializer.Serialize(output));
59-
var index = 0;
60-
foreach (var line in output)
61-
{
62-
index++;
63-
_logger?.LogDebug("the {Index}nth record: {Result} by netstat on linux", index, line);
64-
if (!line.Trim().StartsWith("tcp", StringComparison.OrdinalIgnoreCase) &&
65-
!line.Trim().StartsWith("udp", StringComparison.OrdinalIgnoreCase))
66-
continue;
52+
if (line.Trim().StartsWith("Proto") || line.Trim().StartsWith("协议"))
53+
continue;
6754

68-
var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
55+
var parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
6956

70-
var len = parts.Length;
71-
if (len > 2)
57+
var len = parts.Length;
58+
if (len > 2)
59+
{
60+
var pId = int.Parse(parts[len - 1].Split('/')[0]);
61+
if (int.Parse(parts[1].Split(':').Last()) == port && !pIdList.Contains(pId))
7262
{
73-
var pId = int.Parse(parts[GetIndex(parts, "LISTEN") + 1].Split('/')[0]);
74-
if (int.Parse(parts[3].Split(':').Last()) == port && !pIdList.Contains(pId))
75-
{
76-
pIdList.Add(pId);
77-
}
63+
pIdList.Add(pId);
7864
}
7965
}
8066
}
81-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
82-
{
83-
List<string> output = GetResponse("lsof", $"-nP -iTCP -sTCP:LISTEN", "\n");
67+
return pIdList;
68+
}
8469

85-
_logger?.LogDebug("{Result} by netstat on OSX", System.Text.Json.JsonSerializer.Serialize(output));
86-
var index = 0;
87-
foreach (var line in output)
88-
{
89-
index++;
90-
_logger?.LogDebug("the {Index}nth record: {Result} by netstat on OSX", index, line);
91-
if (line.Trim().StartsWith("COMMAND", StringComparison.OrdinalIgnoreCase))
92-
continue;
70+
private List<int> GetPidByPortByLinux(ushort port)
71+
{
72+
List<int> pIdList = new();
73+
List<string> output = GetResponse("netstat", $"-tunlp", "\n");
74+
75+
_logger?.LogDebug("{Result} by netstat on linux", System.Text.Json.JsonSerializer.Serialize(output));
76+
var index = 0;
77+
foreach (var line in output)
78+
{
79+
index++;
80+
_logger?.LogDebug("the {Index}nth record: {Result} by netstat on linux", index, line);
81+
if (!line.Trim().StartsWith("tcp", StringComparison.OrdinalIgnoreCase) &&
82+
!line.Trim().StartsWith("udp", StringComparison.OrdinalIgnoreCase))
83+
continue;
9384

94-
var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
85+
var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
9586

96-
var len = parts.Length;
97-
if (len > 2)
87+
var len = parts.Length;
88+
if (len > 2)
89+
{
90+
var pId = int.Parse(parts[GetIndex(parts, "LISTEN") + 1].Split('/')[0]);
91+
if (int.Parse(parts[3].Split(':').Last()) == port && !pIdList.Contains(pId))
9892
{
99-
var pId = int.Parse(parts[1]);
100-
if (int.Parse(parts[parts.Length - 2].Split(':').Last()) == port && !pIdList.Contains(pId))
101-
{
102-
pIdList.Add(pId);
103-
}
93+
pIdList.Add(pId);
10494
}
10595
}
10696
}
107-
else
108-
{
109-
_logger?.LogError("unsupported operating system");
110-
}
111-
return pIdList.Where(pid => pid > 0).ToList();
97+
return pIdList;
11298
}
11399

114-
private int GetIndex(string[] array, string content)
100+
private static int GetIndex(string[] array, string content)
115101
{
116102
for (var index = 0; index < array.Length; index++)
117103
{
@@ -121,7 +107,36 @@ private int GetIndex(string[] array, string content)
121107
return 0;
122108
}
123109

124-
private List<string> GetResponse(string fileName, string arguments, string pattern)
110+
private List<int> GetPidByPortByOsx(ushort port)
111+
{
112+
List<int> pIdList = new();
113+
List<string> output = GetResponse("lsof", $"-nP -iTCP -sTCP:LISTEN", "\n");
114+
115+
_logger?.LogDebug("{Result} by netstat on OSX", System.Text.Json.JsonSerializer.Serialize(output));
116+
var index = 0;
117+
foreach (var line in output)
118+
{
119+
index++;
120+
_logger?.LogDebug("the {Index}nth record: {Result} by netstat on OSX", index, line);
121+
if (line.Trim().StartsWith("COMMAND", StringComparison.OrdinalIgnoreCase))
122+
continue;
123+
124+
var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
125+
126+
var len = parts.Length;
127+
if (len > 2)
128+
{
129+
var pId = int.Parse(parts[1]);
130+
if (int.Parse(parts[parts.Length - 2].Split(':').Last()) == port && !pIdList.Contains(pId))
131+
{
132+
pIdList.Add(pId);
133+
}
134+
}
135+
}
136+
return pIdList;
137+
}
138+
139+
private static List<string> GetResponse(string fileName, string arguments, string pattern)
125140
{
126141
var process = new Process()
127142
{
@@ -145,7 +160,7 @@ private List<string> GetResponse(string fileName, string arguments, string patte
145160
/// get the currently used port
146161
/// </summary>
147162
/// <returns>Port set that has been used</returns>
148-
private IEnumerable<int> GetPortsByUsed()
163+
private static IEnumerable<int> GetPortsByUsed()
149164
{
150165
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
151166
var connectionEndPoints = ipGlobalProperties.GetActiveTcpConnections().Select(information => information.LocalEndPoint);

0 commit comments

Comments
 (0)