forked from redis-windows/redis-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (103 loc) · 3.57 KB
/
Program.cs
File metadata and controls
137 lines (103 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Diagnostics;
namespace ValkeyService
{
class Program
{
static void Main(string[] args)
{
string configFilePath = "valkey.conf";
if (args.Length > 1 && args[0] == "-c")
{
configFilePath = args[1];
}
IHost host = Host.CreateDefaultBuilder()
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService(_ => new ValkeyService(configFilePath));
})
.Build();
host.Run();
}
}
public class ValkeyService : BackgroundService
{
private readonly string configFilePath;
private Process? valkeyProcess;
private string valkeyServerPath = string.Empty;
private string configPathForValkey = string.Empty;
public ValkeyService(string configFilePath)
{
this.configFilePath = configFilePath;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
var basePath = AppContext.BaseDirectory;
string conf = configFilePath;
if (!Path.IsPathRooted(conf))
conf = Path.Combine(basePath, conf);
conf = Path.GetFullPath(conf);
var diskSymbol = conf[..conf.IndexOf(":")];
configPathForValkey = conf
.Replace(diskSymbol + ":", "/cygdrive/" + diskSymbol)
.Replace("\\", "/");
valkeyServerPath = Path.Combine(basePath, "valkey-server.exe")
.Replace("\\", "/");
string arguments = $"\"{configPathForValkey}\"";
valkeyProcess = Process.Start(new ProcessStartInfo(valkeyServerPath, arguments)
{
WorkingDirectory = basePath,
UseShellExecute = false
});
return Task.CompletedTask;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Delay(Timeout.Infinite, stoppingToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
if (valkeyProcess == null || valkeyProcess.HasExited)
return;
try
{
await TryGracefulShutdownAsync();
bool exited = await WaitForExitAsync(valkeyProcess, 5000);
if (!exited)
{
valkeyProcess.Kill(true);
}
}
catch
{
if (!valkeyProcess.HasExited)
valkeyProcess.Kill(true);
}
valkeyProcess.Dispose();
}
private async Task TryGracefulShutdownAsync()
{
string valkeyCliPath = Path.Combine(AppContext.BaseDirectory, "valkey-cli.exe");
if (!File.Exists(valkeyCliPath))
return;
var psi = new ProcessStartInfo(valkeyCliPath, "SHUTDOWN")
{
RedirectStandardOutput = true,
RedirectStandardError = true
};
try
{
using var cli = Process.Start(psi);
if (cli != null)
await cli.WaitForExitAsync();
}
catch
{
}
}
private static Task<bool> WaitForExitAsync(Process process, int timeoutMs)
{
return Task.Run(() => process.WaitForExit(timeoutMs));
}
}
}