106 lines
3.1 KiB
C#
106 lines
3.1 KiB
C#
namespace ThreadPilot.Services.Core
|
|
{
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
public abstract class BaseSystemService : ISystemService, IDisposable
|
|
{
|
|
protected readonly ILogger Logger;
|
|
private bool isAvailable;
|
|
private bool disposed;
|
|
|
|
public bool IsAvailable
|
|
{
|
|
get => this.isAvailable;
|
|
protected set
|
|
{
|
|
if (this.isAvailable != value)
|
|
{
|
|
this.isAvailable = value;
|
|
this.OnAvailabilityChanged(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public event EventHandler<ServiceAvailabilityChangedEventArgs>? AvailabilityChanged;
|
|
|
|
protected BaseSystemService(ILogger logger)
|
|
{
|
|
this.Logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
public virtual async Task InitializeAsync()
|
|
{
|
|
try
|
|
{
|
|
this.Logger.LogInformation("Initializing {ServiceType}", this.GetType().Name);
|
|
await this.InitializeServiceAsync();
|
|
this.IsAvailable = true;
|
|
this.Logger.LogInformation("{ServiceType} initialized successfully", this.GetType().Name);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.Logger.LogError(ex, "Failed to initialize {ServiceType}", this.GetType().Name);
|
|
this.IsAvailable = false;
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public virtual async Task DisposeAsync()
|
|
{
|
|
if (this.disposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
this.Logger.LogInformation("Disposing {ServiceType}", this.GetType().Name);
|
|
await this.DisposeServiceAsync();
|
|
this.IsAvailable = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.Logger.LogError(ex, "Error disposing {ServiceType}", this.GetType().Name);
|
|
}
|
|
finally
|
|
{
|
|
this.disposed = true;
|
|
}
|
|
}
|
|
|
|
protected abstract Task InitializeServiceAsync();
|
|
|
|
protected abstract Task DisposeServiceAsync();
|
|
|
|
protected virtual void OnAvailabilityChanged(bool isAvailable, string? reason = null)
|
|
{
|
|
this.AvailabilityChanged?.Invoke(this, new ServiceAvailabilityChangedEventArgs(isAvailable, reason));
|
|
}
|
|
|
|
protected void ThrowIfDisposed()
|
|
{
|
|
if (this.disposed)
|
|
{
|
|
throw new ObjectDisposedException(this.GetType().Name);
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
this.Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (!this.disposed && disposing)
|
|
{
|
|
_ = Task.Run(async () => await this.DisposeAsync());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|