feat: Add daemon single-instance lock and graceful shutdown to systemd service.
This commit is contained in:
@@ -25,8 +25,22 @@ public static class DaemonService
|
|||||||
return Path.Combine(runtimeDir, "toak.sock");
|
return Path.Combine(runtimeDir, "toak.sock");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static FileStream? _lockFile;
|
||||||
|
|
||||||
public static async Task StartAsync(bool verbose)
|
public static async Task StartAsync(bool verbose)
|
||||||
{
|
{
|
||||||
|
var lockPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "toak", "daemon.lock");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(lockPath)!);
|
||||||
|
_lockFile = new FileStream(lockPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
||||||
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Toak daemon is already running.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Logger.Verbose = verbose;
|
Logger.Verbose = verbose;
|
||||||
var socketPath = GetSocketPath();
|
var socketPath = GetSocketPath();
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ After=network.target
|
|||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
ExecStart=/usr/bin/toak daemon
|
ExecStart=/usr/bin/toak daemon
|
||||||
|
ExecStop=/bin/kill -INT $MAINPID
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=3
|
RestartSec=3
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user