* Implemented IDisposable interface for handlers and added virtual method if such needed

This commit is contained in:
2025-08-04 05:22:43 +04:00
parent a794b6ed54
commit e790f31495
2 changed files with 38 additions and 5 deletions
@@ -9,7 +9,7 @@ namespace Telegrator.Handlers.Components
/// <summary> /// <summary>
/// Base class for update handlers, providing execution and lifetime management for Telegram updates. /// Base class for update handlers, providing execution and lifetime management for Telegram updates.
/// </summary> /// </summary>
public abstract class UpdateHandlerBase(UpdateType handlingUpdateType) public abstract class UpdateHandlerBase(UpdateType handlingUpdateType) : IDisposable
{ {
/// <summary> /// <summary>
/// Gets the <see cref="UpdateType"/> that this handler processes. /// Gets the <see cref="UpdateType"/> that this handler processes.
@@ -101,6 +101,16 @@ namespace Telegrator.Handlers.Components
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
protected abstract Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken); protected abstract Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken);
/// <summary>
/// Dispose resources of this handler. Override if needed
/// </summary>
/// <param name="disposing"></param>
/// <returns>Return <see langword="true"/> if dispose was successfull and garbage collecting for this object can be supressed</returns>
protected virtual bool Dispose(bool disposing)
{
return false;
}
/// <summary> /// <summary>
/// Handles failed filters during handler describing. /// Handles failed filters during handler describing.
/// Use <see cref="Result"/> to control how router should treat this fail. /// Use <see cref="Result"/> to control how router should treat this fail.
@@ -115,5 +125,15 @@ namespace Telegrator.Handlers.Components
{ {
return Task.FromResult(Result.Ok()); return Task.FromResult(Result.Ok());
} }
/// <inheritdoc/>
public void Dispose()
{
if (LifetimeToken.IsEnded)
return;
if (Dispose(true))
GC.SuppressFinalize(this);
}
} }
} }
+16 -3
View File
@@ -1,4 +1,5 @@
using Telegrator.Logging; using Telegrator.Handlers.Components;
using Telegrator.Logging;
using Telegrator.MadiatorCore; using Telegrator.MadiatorCore;
using Telegrator.MadiatorCore.Descriptors; using Telegrator.MadiatorCore.Descriptors;
@@ -79,20 +80,32 @@ namespace Telegrator.Polling
Alligator.LogDebug("Described handler '{0}'", handlerInfo.DisplayString); Alligator.LogDebug("Described handler '{0}'", handlerInfo.DisplayString);
HandlerExecuting?.Invoke(handlerInfo); HandlerExecuting?.Invoke(handlerInfo);
lastResult = await handlerInfo.HandlerInstance.Execute(handlerInfo); using (UpdateHandlerBase instance = handlerInfo.HandlerInstance)
{
lastResult = await instance.Execute(handlerInfo);
ExecutingHandlersSemaphore?.Release(1); ExecutingHandlersSemaphore?.Release(1);
}
if (lastResult.RouteNext) if (lastResult.RouteNext)
{ {
Alligator.LogDebug("Handler requested route continuation"); Alligator.LogDebug("Handler requested route continuation");
} }
} }
catch (NotImplementedException)
{
_ = 0xBAD + 0xC0DE;
}
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
_ = 0xBAD + 0xC0DE;
break; break;
} }
catch (Exception ex)
{
Alligator.LogError("Failed to process handler!", ex);
}
if (!lastResult.RouteNext) if (lastResult != null && !lastResult.RouteNext)
break; break;
} }
} }