//-----------------------------------------------------------------------
//
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.WorkerStatus;
using System.Collections.Concurrent;
///
/// Global service status class for monitoring and control.
///
public class GlobalServiceStatus
{
private readonly ConcurrentDictionary _workerStatuses = new();
///
/// Initializes a new instance of the class.
///
/// Name of the service that we are keeping track of.
public GlobalServiceStatus(string serviceName)
{
ServiceName = serviceName;
}
///
/// Gets or sets the status of the service.
///
public string Status { get; set; } = "Stopped";
///
/// Gets or sets the current status of the service.
///
public string CurrentStatus { get; set; } = "Stopped";
///
/// Gets or sets the ServiceName in order to identify the service and its workers in the database.
///
public string ServiceName { get; set; }
///
/// Register a worker with the service.
///
/// Name of the worker.
public void RegisterWorker(string workerName)
{
_workerStatuses[workerName] = false;
}
///
/// Set the status of a worker.
///
/// Name of the worker.
/// Boolean which indicates if worker is currently running.
public void SetWorkerStatus(string workerName, bool isRunning)
{
if (_workerStatuses.ContainsKey(workerName))
{
_workerStatuses[workerName] = isRunning;
}
}
///
/// Returns boolean indicating if all workers are running.
///
/// Boolean which indicates if all workers are started.
public bool AreAllWorkersRunning() => _workerStatuses.All(w => w.Value);
///
/// Returns boolean indicating if all workers are stopped.
///
/// Boolean which indicates if all workers are stopped.
public bool AreAllWorkersStopped() => _workerStatuses.All(w => !w.Value);
}