Skip to content

Commit ad0512f

Browse files
committed
Add graceful shutdown using IHostApplicationLifetime
Introduce a mechanism to gracefully stop the application by leveraging the IHostApplicationLifetime interface. - In BotApp.cs, call StopApplication() on IHostApplicationLifetime after logging that the bot has stopped. - In BotBuilder.cs, reorganize using directives and register IHostApplicationLifetime as a singleton service. - Add HostApplicationLifetime.cs to implement IHostApplicationLifetime, providing lifecycle event tokens and a method to stop the application.
1 parent 0d00fae commit ad0512f

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Sources/TelegramBot/BotApp.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public void Run(CancellationToken token = default)
9494
_logger.LogInformation("Bot stopped - no longer receiving updates.");
9595
}
9696
_logger.LogInformation("Stopping hosted services...");
97+
var hostApplicationLifetime = _serviceProvider.GetRequiredService<IHostApplicationLifetime>();
98+
hostApplicationLifetime.StopApplication();
9799
foreach (var hostedService in hostedServices)
98100
{
99101
hostedService.StopAsync(token).Wait(token);

Sources/TelegramBot/Builders/BotBuilder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
2+
using System.Linq;
23
using Telegram.Bot;
4+
using TelegramBot.Services;
5+
using TelegramBot.Providers;
6+
using TelegramBot.Abstractions;
37
using Microsoft.Extensions.Logging;
8+
using Microsoft.Extensions.Hosting;
49
using Microsoft.Extensions.Configuration;
510
using Microsoft.Extensions.DependencyInjection;
6-
using System.Linq;
7-
using TelegramBot.Abstractions;
8-
using TelegramBot.Providers;
911

1012
namespace TelegramBot.Builders
1113
{
@@ -134,6 +136,7 @@ public IBot Build()
134136
{
135137
Services.AddSingleton<IKeyValueProvider, InMemoryKeyValueProvider>();
136138
}
139+
Services.AddSingleton<IHostApplicationLifetime, HostApplicationLifetime>();
137140
return new BotApp(client, Services.BuildServiceProvider());
138141
}
139142
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Threading;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace TelegramBot.Services
6+
{
7+
internal class HostApplicationLifetime : IHostApplicationLifetime
8+
{
9+
public CancellationToken ApplicationStarted => _cancellationTokenSource.Token;
10+
11+
public CancellationToken ApplicationStopping => _cancellationTokenSource.Token;
12+
13+
public CancellationToken ApplicationStopped => _cancellationTokenSource.Token;
14+
15+
16+
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
17+
18+
public void StopApplication()
19+
{
20+
_cancellationTokenSource.Cancel();
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)