SEBWIN-219: Enhanced implementation and logging of session operation.

This commit is contained in:
dbuechel 2018-02-22 10:00:18 +01:00
parent 2ab48cda09
commit 4e7b8fc88e
4 changed files with 86 additions and 41 deletions

View file

@ -74,6 +74,8 @@ namespace SafeExamBrowser.Client.Behaviour
public bool TryStart() public bool TryStart()
{ {
logger.Info("--- Initiating startup procedure ---");
splashScreen = uiFactory.CreateSplashScreen(); splashScreen = uiFactory.CreateSplashScreen();
operations.ProgressIndicator = splashScreen; operations.ProgressIndicator = splashScreen;
@ -86,6 +88,14 @@ namespace SafeExamBrowser.Client.Behaviour
RegisterEvents(); RegisterEvents();
runtime.InformClientReady(); runtime.InformClientReady();
splashScreen.Hide(); splashScreen.Hide();
logger.Info("--- Application successfully initialized ---");
logger.Log(string.Empty);
}
else
{
logger.Info("--- Application startup aborted! ---");
logger.Log(string.Empty);
} }
return success; return success;
@ -93,13 +103,27 @@ namespace SafeExamBrowser.Client.Behaviour
public void Terminate() public void Terminate()
{ {
logger.Log(string.Empty);
logger.Info("--- Initiating shutdown procedure ---");
splashScreen.Show(); splashScreen.Show();
splashScreen.BringToForeground(); splashScreen.BringToForeground();
// TODO // TODO
DeregisterEvents(); DeregisterEvents();
operations.TryRevert(); var success = operations.TryRevert();
if (success)
{
logger.Info("--- Application successfully finalized ---");
logger.Log(string.Empty);
}
else
{
logger.Info("--- Shutdown procedure failed! ---");
logger.Log(string.Empty);
}
splashScreen?.Close(); splashScreen?.Close();
} }

View file

@ -105,7 +105,6 @@ namespace SafeExamBrowser.Client
internal void LogShutdownInformation() internal void LogShutdownInformation()
{ {
logger?.Log(string.Empty);
logger?.Log($"# Client instance terminated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}"); logger?.Log($"# Client instance terminated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
} }

View file

@ -62,11 +62,12 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
session = configuration.InitializeSession(); session = configuration.InitializeSession();
runtimeHost.StartupToken = session.StartupToken; runtimeHost.StartupToken = session.StartupToken;
logger.Info("Initializing service session...");
service.StartSession(session.Id, configuration.CurrentSettings); service.StartSession(session.Id, configuration.CurrentSettings);
try try
{ {
StartClient(); sessionRunning = TryStartClient();
} }
catch (Exception e) catch (Exception e)
{ {
@ -80,7 +81,7 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
else else
{ {
Abort = true; Abort = true;
logger.Info($"Failed to start new session! Aborting..."); logger.Info($"Failed to start new session! Reverting service session and aborting procedure...");
service.StopSession(session.Id); service.StopSession(session.Id);
} }
} }
@ -92,6 +93,7 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
logger.Info($"Stopping session with identifier '{session.Id}'..."); logger.Info($"Stopping session with identifier '{session.Id}'...");
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_StopSession, true); ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_StopSession, true);
logger.Info("Stopping service session...");
service.StopSession(session.Id); service.StopSession(session.Id);
try try
@ -108,7 +110,7 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
} }
} }
private void StartClient() private bool TryStartClient()
{ {
var clientReady = false; var clientReady = false;
var clientReadyEvent = new AutoResetEvent(false); var clientReadyEvent = new AutoResetEvent(false);
@ -118,39 +120,44 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
var hostUri = configuration.RuntimeInfo.RuntimeAddress; var hostUri = configuration.RuntimeInfo.RuntimeAddress;
var token = session.StartupToken.ToString("D"); var token = session.StartupToken.ToString("D");
logger.Info("Starting new client process.");
runtimeHost.ClientReady += clientReadyEventHandler; runtimeHost.ClientReady += clientReadyEventHandler;
session.ClientProcess = processFactory.StartNew(clientExecutable, clientLogFile, hostUri, token); session.ClientProcess = processFactory.StartNew(clientExecutable, clientLogFile, hostUri, token);
clientReady = clientReadyEvent.WaitOne(TEN_SECONDS);
logger.Info("Waiting for client to complete initialization...");
clientReady = clientReadyEvent.WaitOne(TEN_SECONDS);
runtimeHost.ClientReady -= clientReadyEventHandler; runtimeHost.ClientReady -= clientReadyEventHandler;
// TODO: Check if client process alive! if (!clientReady)
if (clientReady)
{
if (client.Connect(session.StartupToken))
{
var response = client.RequestAuthentication();
// TODO: Further integrity checks necessary?
if (session.ClientProcess.Id == response?.ProcessId)
{
sessionRunning = true;
}
else
{
logger.Error("Failed to verify client integrity!");
}
}
else
{
logger.Error("Failed to connect to client!");
}
}
else
{ {
logger.Error($"Failed to start client within {TEN_SECONDS / 1000} seconds!"); logger.Error($"Failed to start client within {TEN_SECONDS / 1000} seconds!");
return false;
} }
logger.Info("Client has been successfully started and initialized.");
if (!client.Connect(session.StartupToken))
{
logger.Error("Failed to connect to client!");
return false;
}
logger.Info("Connection with client has been established.");
var response = client.RequestAuthentication();
if (session.ClientProcess.Id != response?.ProcessId)
{
logger.Error("Failed to verify client integrity!");
return false;
}
logger.Info("Authentication of client has been successful.");
return true;
} }
private void StopClient() private void StopClient()
@ -166,12 +173,28 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
runtimeHost.ClientDisconnected += disconnectedEventHandler; runtimeHost.ClientDisconnected += disconnectedEventHandler;
session.ClientProcess.Terminated += terminatedEventHandler; session.ClientProcess.Terminated += terminatedEventHandler;
logger.Info("Instructing client to initiate shutdown procedure.");
client.InitiateShutdown(); client.InitiateShutdown();
logger.Info("Disconnecting from client communication host.");
client.Disconnect(); client.Disconnect();
logger.Info("Waiting for client to disconnect from runtime communication host...");
disconnected = disconnectedEvent.WaitOne(TEN_SECONDS); disconnected = disconnectedEvent.WaitOne(TEN_SECONDS);
if (!disconnected)
{
logger.Error($"Client failed to disconnect within {TEN_SECONDS / 1000} seconds!");
}
logger.Info("Waiting for client process to terminate...");
terminated = terminatedEvent.WaitOne(TEN_SECONDS); terminated = terminatedEvent.WaitOne(TEN_SECONDS);
if (!terminated)
{
logger.Error($"Client failed to terminate within {TEN_SECONDS / 1000} seconds!");
}
runtimeHost.ClientDisconnected -= disconnectedEventHandler; runtimeHost.ClientDisconnected -= disconnectedEventHandler;
session.ClientProcess.Terminated -= terminatedEventHandler; session.ClientProcess.Terminated -= terminatedEventHandler;
@ -181,15 +204,7 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
} }
else else
{ {
if (!disconnected) logger.Warn("Attempting to kill client process since graceful shutdown failed!");
{
logger.Error($"Client failed to disconnect within {TEN_SECONDS / 1000} seconds!");
}
else if (!terminated)
{
logger.Error($"Client failed to terminate within {TEN_SECONDS / 1000} seconds!");
}
KillClient(); KillClient();
} }
} }

View file

@ -75,7 +75,7 @@ namespace SafeExamBrowser.Runtime.Behaviour
if (initialized) if (initialized)
{ {
logger.Info("--- Application successfully initialized! ---"); logger.Info("--- Application successfully initialized ---");
logger.Log(string.Empty); logger.Log(string.Empty);
logger.Subscribe(runtimeWindow); logger.Subscribe(runtimeWindow);
@ -112,7 +112,7 @@ namespace SafeExamBrowser.Runtime.Behaviour
if (success) if (success)
{ {
logger.Info("--- Application successfully finalized! ---"); logger.Info("--- Application successfully finalized ---");
logger.Log(string.Empty); logger.Log(string.Empty);
} }
else else
@ -127,11 +127,13 @@ namespace SafeExamBrowser.Runtime.Behaviour
private void StartSession(bool initial = false) private void StartSession(bool initial = false)
{ {
runtimeWindow.Show(); runtimeWindow.Show();
logger.Info(">------ Initiating session procedure ------<");
sessionRunning = initial ? sessionSequence.TryPerform() : sessionSequence.TryRepeat(); sessionRunning = initial ? sessionSequence.TryPerform() : sessionSequence.TryRepeat();
if (sessionRunning) if (sessionRunning)
{ {
logger.Info(">------ Session is running ------<");
runtimeWindow.HideProgressBar(); runtimeWindow.HideProgressBar();
runtimeWindow.UpdateText(TextKey.RuntimeWindow_ApplicationRunning); runtimeWindow.UpdateText(TextKey.RuntimeWindow_ApplicationRunning);
@ -142,6 +144,7 @@ namespace SafeExamBrowser.Runtime.Behaviour
} }
else else
{ {
logger.Info(">------ Session procedure was aborted! ------<");
// TODO: Not when user chose to terminate after reconfiguration! Probably needs IOperationSequenceResult or alike... // TODO: Not when user chose to terminate after reconfiguration! Probably needs IOperationSequenceResult or alike...
uiFactory.Show(TextKey.MessageBox_SessionStartError, TextKey.MessageBox_SessionStartErrorTitle, icon: MessageBoxIcon.Error); uiFactory.Show(TextKey.MessageBox_SessionStartError, TextKey.MessageBox_SessionStartErrorTitle, icon: MessageBoxIcon.Error);
@ -157,17 +160,21 @@ namespace SafeExamBrowser.Runtime.Behaviour
runtimeWindow.Show(); runtimeWindow.Show();
runtimeWindow.BringToForeground(); runtimeWindow.BringToForeground();
runtimeWindow.ShowProgressBar(); runtimeWindow.ShowProgressBar();
logger.Info(">------ Reverting session operations ------<");
var success = sessionSequence.TryRevert(); var success = sessionSequence.TryRevert();
if (success) if (success)
{ {
logger.Info(">------ Session is terminated ------<");
sessionRunning = false; sessionRunning = false;
// TODO // TODO
} }
else else
{ {
logger.Info(">------ Session reversion was erroneous! ------<");
// TODO // TODO
} }
} }