mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Use file-scoped namespaces
This commit is contained in:
@@ -5,70 +5,69 @@ using BotFramework.Abstractions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Service
|
||||
namespace Kruzya.TelegramBot.Core.Service;
|
||||
|
||||
public abstract class AbstractTimedHostedService : IHostedService, IDisposable
|
||||
{
|
||||
public abstract class AbstractTimedHostedService : IHostedService, IDisposable
|
||||
private Timer _timer;
|
||||
|
||||
protected readonly ILogger _logger;
|
||||
protected readonly IBotInstance _bot;
|
||||
|
||||
protected virtual TimeSpan TimerPeriod => TimeSpan.FromSeconds(45);
|
||||
|
||||
protected AbstractTimedHostedService(ILogger logger, IBotInstance bot)
|
||||
{
|
||||
private Timer _timer;
|
||||
_bot = bot;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
#region IHostedService
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Message sender service is starting.");
|
||||
_timer = new Timer(Run, null, TimeSpan.Zero, TimerPeriod);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Message sender service is stopping.");
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
protected readonly ILogger _logger;
|
||||
protected readonly IBotInstance _bot;
|
||||
|
||||
protected virtual TimeSpan TimerPeriod => TimeSpan.FromSeconds(45);
|
||||
#endregion
|
||||
#region IDisposable
|
||||
/// <summary>
|
||||
/// Disposes the timer.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected AbstractTimedHostedService(ILogger logger, IBotInstance bot)
|
||||
private async void Run(object state)
|
||||
{
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
try
|
||||
{
|
||||
_bot = bot;
|
||||
_logger = logger;
|
||||
await OnRun();
|
||||
}
|
||||
|
||||
#region IHostedService
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
finally
|
||||
{
|
||||
_logger.LogInformation("Message sender service is starting.");
|
||||
_timer = new Timer(Run, null, TimeSpan.Zero, TimerPeriod);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Message sender service is stopping.");
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region IDisposable
|
||||
/// <summary>
|
||||
/// Disposes the timer.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
#endregion
|
||||
|
||||
private async void Run(object state)
|
||||
{
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
|
||||
try
|
||||
{
|
||||
await OnRun();
|
||||
}
|
||||
finally
|
||||
{
|
||||
var timerPeriod = TimerPeriod;
|
||||
_timer?.Change(timerPeriod, timerPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual async Task OnRun()
|
||||
{
|
||||
await Task.Delay(500);
|
||||
var timerPeriod = TimerPeriod;
|
||||
_timer?.Change(timerPeriod, timerPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual async Task OnRun()
|
||||
{
|
||||
await Task.Delay(500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user