forked from redis-windows/redis-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
83 lines (57 loc) · 2.1 KB
/
Program.cs
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
using System.Diagnostics;
namespace RedisService
{
class Program
{
static void Main(string[] args)
{
string configFilePath = "redis.conf";
if (args.Length > 1 && args[0] == "-c")
{
configFilePath = args[1];
}
IHost host = Host.CreateDefaultBuilder().UseWindowsService().ConfigureServices((hostContext, services) =>
{
services.AddHostedService(serviceProvider =>
new RedisService(configFilePath));
}).Build();
host.Run();
}
}
public class RedisService(string configFilePath) : BackgroundService
{
private Process? redisProcess = new();
public override Task StartAsync(CancellationToken stoppingToken)
{
var basePath = Path.Combine(AppContext.BaseDirectory);
if (!Path.IsPathRooted(configFilePath))
{
configFilePath = Path.Combine(basePath, configFilePath);
}
configFilePath = Path.GetFullPath(configFilePath);
var diskSymbol = configFilePath[..configFilePath.IndexOf(":")];
var fileConf = configFilePath.Replace(diskSymbol + ":", "/cygdrive/" + diskSymbol).Replace("\\", "/");
string fileName = Path.Combine(basePath, "redis-server.exe").Replace("\\", "/");
string arguments = $"\"{fileConf}\"";
ProcessStartInfo processStartInfo = new(fileName, arguments)
{
WorkingDirectory = basePath
};
redisProcess = Process.Start(processStartInfo);
return Task.CompletedTask;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Delay(-1, stoppingToken);
}
public override Task StopAsync(CancellationToken stoppingToken)
{
if (redisProcess != null)
{
redisProcess.Kill();
redisProcess.Dispose();
}
return Task.CompletedTask;
}
}
}