Improved reliability of the shutdown process.
This commit is contained in:
parent
3f6007922c
commit
d94b1cb813
3 changed files with 47 additions and 63 deletions
SharpChat
|
@ -2,7 +2,6 @@ using SharpChat;
|
|||
using SharpChat.Configuration;
|
||||
using SharpChat.Flashii;
|
||||
using SharpChat.MariaDB;
|
||||
using SharpChat.Messages;
|
||||
using SharpChat.SQLite;
|
||||
using System.Text;
|
||||
|
||||
|
@ -22,24 +21,27 @@ if(SharpInfo.IsDebugBuild) {
|
|||
} else
|
||||
Console.WriteLine(SharpInfo.VersionStringShort.PadLeft(28, ' '));
|
||||
|
||||
using ManualResetEvent mre = new(false);
|
||||
bool hasCancelled = false;
|
||||
using CancellationTokenSource cts = new();
|
||||
|
||||
void cancelKeyPressHandler(object? sender, ConsoleCancelEventArgs ev) {
|
||||
Console.CancelKeyPress -= cancelKeyPressHandler;
|
||||
hasCancelled = true;
|
||||
ev.Cancel = true;
|
||||
mre.Set();
|
||||
void exitHandler() {
|
||||
if(cts.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
cts.Cancel();
|
||||
Logger.Write("Shutdown requested through console...");
|
||||
}
|
||||
;
|
||||
Console.CancelKeyPress += cancelKeyPressHandler;
|
||||
|
||||
if(hasCancelled) return;
|
||||
AppDomain.CurrentDomain.ProcessExit += (sender, ev) => { exitHandler(); };
|
||||
Console.CancelKeyPress += (sender, ev) => { ev.Cancel = true; exitHandler(); };
|
||||
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
string configFile = CONFIG;
|
||||
|
||||
// If the config file doesn't exist and we're using the default path, run the converter
|
||||
if(!File.Exists(configFile) && configFile == CONFIG) {
|
||||
Logger.Write("Creating example configuration file...");
|
||||
|
||||
using Stream s = new FileStream(CONFIG, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
||||
s.SetLength(0);
|
||||
s.Flush();
|
||||
|
@ -110,38 +112,44 @@ if(!File.Exists(configFile) && configFile == CONFIG) {
|
|||
sw.Flush();
|
||||
}
|
||||
|
||||
Logger.Write("Initialising configuration...");
|
||||
using StreamConfig config = StreamConfig.FromPath(configFile);
|
||||
|
||||
if(hasCancelled) return;
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Initialising HTTP client...");
|
||||
using HttpClient httpClient = new(new HttpClientHandler() {
|
||||
UseProxy = false,
|
||||
});
|
||||
httpClient.DefaultRequestHeaders.Add("User-Agent", SharpInfo.ProgramName);
|
||||
|
||||
if(hasCancelled) return;
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Initialising Flashii client...");
|
||||
FlashiiClient flashii = new(httpClient, config.ScopeTo("msz"));
|
||||
|
||||
if(hasCancelled) return;
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Initialising storage...");
|
||||
Storage storage = string.IsNullOrWhiteSpace(config.SafeReadValue("mariadb:host", string.Empty))
|
||||
? new SQLiteStorage(SQLiteStorage.BuildConnectionString(config.ScopeTo("sqlite")))
|
||||
: new MariaDBStorage(MariaDBStorage.BuildConnectionString(config.ScopeTo("mariadb")));
|
||||
|
||||
try {
|
||||
if(hasCancelled) return;
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Upgrading storage...");
|
||||
await storage.UpgradeStorage();
|
||||
|
||||
if(hasCancelled) return;
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
using SockChatServer scs = new(flashii, flashii, storage.CreateMessageStorage(), config.ScopeTo("chat"));
|
||||
scs.Listen(mre);
|
||||
|
||||
mre.WaitOne();
|
||||
Logger.Write("Preparing server...");
|
||||
await new SockChatServer(cts, flashii, flashii, storage.CreateMessageStorage(), config.ScopeTo("chat")).Listen(cts.Token);
|
||||
} finally {
|
||||
if(storage is IDisposable disp)
|
||||
if(storage is IDisposable disp) {
|
||||
Logger.Write("Cleaning storage...");
|
||||
disp.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Write("Exiting...");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue