Skip to content
Snippets Groups Projects
Commit 0149e094 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Add SetSuspended to simplify handling of status changes

parent 5ed8b177
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,7 @@ namespace ATxCommon.Serializables
private DateTime _lastAdminNotification;
private DateTime _lastGraceNotification;
private string _limitReason;
private string _statusDescription;
private string _currentTransferSrc;
private string _currentTargetTmp;
......@@ -151,13 +151,13 @@ namespace ATxCommon.Serializables
/// <summary>
/// String indicating why the service is currently suspended (empty if not suspended).
/// </summary>
public string LimitReason {
get => _limitReason;
public string StatusDescription {
get => _statusDescription;
set {
if (_limitReason == value) return;
if (_statusDescription == value) return;
_limitReason = value;
Log.Trace("LimitReason was updated ({0}).", value);
_statusDescription = value;
Log.Trace("StatusDescription was updated ({0}).", value);
Serialize();
}
}
......@@ -286,6 +286,27 @@ namespace ATxCommon.Serializables
Environment.MachineName,
_currentTargetTmp);
}
/// <summary>
/// Helper to set the service state, logging a message if the state has changed.
/// </summary>
/// <param name="suspended">The target state for <see cref="ServiceSuspended"/>.</param>
/// <param name="description">The description to use for the log message as well as for
/// setting <see cref="StatusDescription"/>.</param>
public void SetSuspended(bool suspended, string description) {
if (description == _statusDescription && suspended == _serviceSuspended)
return;
_serviceSuspended = suspended;
_statusDescription = description;
Serialize();
if (suspended) {
Log.Info("Service suspended. Reason(s): [{0}]", description);
} else {
Log.Info("Service resuming operation ({0}).", description);
}
}
#region validate and report
......
......@@ -574,32 +574,18 @@ namespace ATxService
// all parameters within valid ranges, so set the state to "Running":
if (suspendReasons.Count == 0) {
if (_status.ServiceSuspended) {
_status.ServiceSuspended = false;
_status.LimitReason = ""; // reset to force a message on next service suspend
Log.Info("Service resuming operation (all parameters in valid ranges).");
}
_status.SetSuspended(false, "all parameters in valid ranges");
return;
}
// set state to "Running" if no-one is logged on:
if (SystemChecks.NoUserIsLoggedOn()) {
if (_status.ServiceSuspended) {
_status.ServiceSuspended = false;
_status.LimitReason = ""; // reset to force a message on next service suspend
Log.Info("Service resuming operation (no user logged on).");
}
_status.SetSuspended(false, "no user is currently logged on");
return;
}
// by reaching this point we know the service should be suspended:
_status.ServiceSuspended = true;
var limitReason = string.Join(", ", suspendReasons);
if (limitReason == _status.LimitReason)
return;
Log.Info("Service suspended. Reason(s): [{0}]", limitReason);
_status.LimitReason = limitReason;
_status.SetSuspended(true, string.Join(", ", suspendReasons));
}
/// <summary>
......
......@@ -459,18 +459,18 @@ namespace ATxTray
private void UpdateServiceSuspendedState() {
// first update the suspend reason as this can possibly change even if the service
// never leaves the suspended state and we should still display the correct reason:
if (_serviceSuspendReason == _status.LimitReason &&
if (_serviceSuspendReason == _status.StatusDescription &&
_serviceSuspended == _status.ServiceSuspended)
return;
_serviceSuspended = _status.ServiceSuspended;
_serviceSuspendReason = _status.LimitReason;
_serviceSuspendReason = _status.StatusDescription;
if (_serviceSuspended) {
_miSvcSuspended.Text = @"Service suspended, reason: " + _serviceSuspendReason;
_miSvcSuspended.BackColor = Color.LightSalmon;
/*
_notifyIcon.ShowBalloonTip(500, "AutoTx Monitor",
"Service suspended: " + _status.LimitReason, ToolTipIcon.Warning);
"Service suspended: " + _status.StatusDescription, ToolTipIcon.Warning);
*/
} else {
_miSvcSuspended.Text = @"No limits apply, service active.";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment