SEBSP-23: Removed service health simulation and changed health value to normally be retrieved from transmission response.

This commit is contained in:
Damian Büchel 2024-02-21 19:17:08 +01:00
parent a021bebde6
commit 4aacf85e9a
3 changed files with 47 additions and 78 deletions

View file

@ -21,7 +21,7 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
{ {
} }
internal bool TryExecute(MetaData metaData, ScreenShot screenShot, string sessionId, out string message) internal bool TryExecute(MetaData metaData, ScreenShot screenShot, string sessionId, out int health, out string message)
{ {
var imageFormat = (Header.IMAGE_FORMAT, ToString(screenShot.Format)); var imageFormat = (Header.IMAGE_FORMAT, ToString(screenShot.Format));
var metdataJson = (Header.METADATA, metaData.ToJson()); var metdataJson = (Header.METADATA, metaData.ToJson());
@ -29,8 +29,14 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests
var url = api.ScreenShotEndpoint.Replace(Api.SESSION_ID, sessionId); var url = api.ScreenShotEndpoint.Replace(Api.SESSION_ID, sessionId);
var success = TryExecute(HttpMethod.Post, url, out var response, screenShot.Data, ContentType.OCTET_STREAM, Authorization, imageFormat, metdataJson, timestamp); var success = TryExecute(HttpMethod.Post, url, out var response, screenShot.Data, ContentType.OCTET_STREAM, Authorization, imageFormat, metdataJson, timestamp);
health = default;
message = response.ToLogString(); message = response.ToLogString();
if (success)
{
parser.TryParseHealth(response, out health);
}
return success; return success;
} }

View file

@ -87,10 +87,10 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service
return new ServiceResponse<int>(success, health, message); return new ServiceResponse<int>(success, health, message);
} }
internal ServiceResponse Send(MetaData metaData, ScreenShot screenShot) internal ServiceResponse<int> Send(MetaData metaData, ScreenShot screenShot)
{ {
var request = new ScreenShotRequest(api, httpClient, logger, parser); var request = new ScreenShotRequest(api, httpClient, logger, parser);
var success = request.TryExecute(metaData, screenShot, SessionId, out var message); var success = request.TryExecute(metaData, screenShot, SessionId, out var health, out var message);
if (success) if (success)
{ {
@ -101,7 +101,7 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service
logger.Error("Failed to send screen shot!"); logger.Error("Failed to send screen shot!");
} }
return new ServiceResponse(success, message); return new ServiceResponse<int>(success, health, message);
} }
internal ServiceResponse TerminateSession() internal ServiceResponse TerminateSession()

View file

@ -71,13 +71,8 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
thread.IsBackground = true; thread.IsBackground = true;
thread.Start(); thread.Start();
// TODO: Only use timer when BAD, until then read health from transmission response!
timer.AutoReset = false; timer.AutoReset = false;
timer.Elapsed += Timer_Elapsed; timer.Interval = FIFTEEN_SECONDS;
timer.Interval = 10000;
// TODO: Revert!
// timer.Interval = FIFTEEN_SECONDS;
timer.Start();
} }
internal void Stop() internal void Stop()
@ -88,8 +83,8 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
{ {
logger.Debug("Stopping..."); logger.Debug("Stopping...");
timer.Elapsed -= Timer_Elapsed;
timer.Stop(); timer.Stop();
timer.Elapsed -= Timer_Elapsed;
try try
{ {
@ -113,6 +108,7 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
logger.Error("Failed to stop!", e); logger.Error("Failed to stop!", e);
} }
recovering = false;
resume = default; resume = default;
thread = default; thread = default;
token = default; token = default;
@ -157,6 +153,9 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
recovering = true; recovering = true;
resume = DateTime.Now.AddSeconds(random.Next(0, THREE_MINUTES)); resume = DateTime.Now.AddSeconds(random.Next(0, THREE_MINUTES));
timer.Elapsed += Timer_Elapsed;
timer.Start();
logger.Warn($"Activating local caching and suspending transmission due to bad service health (value: {health}, resume: {resume:HH:mm:ss})."); logger.Warn($"Activating local caching and suspending transmission due to bad service health (value: {health}, resume: {resume:HH:mm:ss}).");
} }
@ -188,6 +187,9 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
} }
else else
{ {
timer.Stop();
timer.Elapsed -= Timer_Elapsed;
logger.Info($"Deactivating local caching and resuming transmission due to improved service health (value: {health})."); logger.Info($"Deactivating local caching and resuming transmission due to improved service health (value: {health}).");
} }
} }
@ -196,23 +198,6 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
{ {
buffer.Enqueue((metaData, schedule, screenShot)); buffer.Enqueue((metaData, schedule, screenShot));
buffer = new Queue<(MetaData, DateTime, ScreenShot)>(buffer.OrderBy((b) => b.schedule)); buffer = new Queue<(MetaData, DateTime, ScreenShot)>(buffer.OrderBy((b) => b.schedule));
// TODO: Remove!
PrintBuffer();
}
private void PrintBuffer()
{
logger.Log("-------------------------------------------------------------------------------------------------------");
logger.Info("");
logger.Log($"\t\t\t\tBuffer: {buffer.Count} items");
foreach (var (m, t, s) in buffer)
{
logger.Log($"\t\t\t\t{s.CaptureTime:HH:mm:ss} -> {t:HH:mm:ss} ({m.Elapsed} {s.Data.Length / 1000:N0}kB)");
}
logger.Log("-------------------------------------------------------------------------------------------------------");
} }
private void BufferFromCache() private void BufferFromCache()
@ -241,9 +226,6 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
{ {
buffer.Dequeue(); buffer.Dequeue();
screenShot.Dispose(); screenShot.Dispose();
// TODO: Remove!
PrintBuffer();
} }
} }
} }
@ -313,9 +295,6 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
{ {
buffer.Dequeue(); buffer.Dequeue();
screenShot.Dispose(); screenShot.Dispose();
// TODO: Remove!
PrintBuffer();
} }
} }
@ -370,7 +349,13 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
if (service.IsConnected) if (service.IsConnected)
{ {
success = service.Send(metaData, screenShot).Success; var response = service.Send(metaData, screenShot);
if (response.Success)
{
health = UpdateHealth(response.Value);
success = true;
}
} }
else else
{ {
@ -380,58 +365,36 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring
return success; return success;
} }
private int factor = 2;
private int bads = 0;
private void Timer_Elapsed(object sender, ElapsedEventArgs e) private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{ {
// TODO: Revert! if (service.IsConnected)
//if (service.IsConnected)
//{
// var response = service.GetHealth();
// if (response.Success)
// {
// var previous = health;
// health = response.Value > BAD ? BAD : (response.Value < GOOD ? GOOD : response.Value);
// if (previous != health)
// {
// logger.Info($"Service health {(previous < health ? "deteriorated" : "improved")} from {previous} to {health}.");
// }
// }
//}
//else
//{
// logger.Warn("Cannot query health as service is disconnected!");
//}
var previous = health;
if (bads < 2)
{ {
bads += health == BAD ? 1 : 0; var response = service.GetHealth();
factor = health == BAD ? -2 : (health == GOOD ? 2 : factor);
health += factor; if (response.Success)
health = health < GOOD ? GOOD : (health > BAD ? BAD : health); {
health = UpdateHealth(response.Value);
}
} }
else else
{ {
health = 0; logger.Warn("Cannot query health as service is disconnected!");
}
if (previous != health)
{
logger.Warn($"Service health {(previous < health ? "deteriorated" : "improved")} from {previous} to {health}.");
if (bads >= 2 && health == 0)
{
logger.Warn("Stopped health simulation.");
}
} }
timer.Start(); timer.Start();
} }
private int UpdateHealth(int value)
{
var previous = health;
var current = value > BAD ? BAD : (value < GOOD ? GOOD : value);
if (previous != current)
{
logger.Info($"Service health {(previous < current ? "deteriorated" : "improved")} from {previous} to {current}.");
}
return current;
}
} }
} }