diff --git a/src/AdminConsole/Billing/BackgroundServices/UserCountUpdaterBackgroundService.cs b/src/AdminConsole/Billing/BackgroundServices/UserCountUpdaterBackgroundService.cs new file mode 100644 index 000000000..0b26d5a83 --- /dev/null +++ b/src/AdminConsole/Billing/BackgroundServices/UserCountUpdaterBackgroundService.cs @@ -0,0 +1,33 @@ +using Passwordless.AdminConsole.Services; +using Passwordless.Common.Background; + +namespace Passwordless.AdminConsole.Billing.BackgroundServices; + +/// +/// Responsible for retrieving the current user count from the Passwordless API and storing it in the Admin Console. +/// It is only required for Bitwarden's internal reporting. +/// +public sealed class UserCountUpdaterBackgroundService( + IServiceProvider serviceProvider, + TimeProvider timeProvider, + ILogger logger) + : BaseDelayedPeriodicBackgroundService( + new TimeOnly(0, 0), + TimeSpan.FromDays(1), + timeProvider, + logger) +{ + protected override async Task DoWorkAsync(CancellationToken cancellationToken) + { + try + { + using IServiceScope scope = serviceProvider.CreateScope(); + var usageService = scope.ServiceProvider.GetRequiredService(); + await usageService.UpdateUsersCountAsync(); + } + catch (Exception e) + { + logger.LogCritical(e, $"{nameof(UserCountUpdaterBackgroundService)} failed."); + } + } +} \ No newline at end of file diff --git a/src/AdminConsole/Billing/BillingBootstrap.cs b/src/AdminConsole/Billing/BillingBootstrap.cs index 0c8ad04c6..f3489901a 100644 --- a/src/AdminConsole/Billing/BillingBootstrap.cs +++ b/src/AdminConsole/Billing/BillingBootstrap.cs @@ -25,6 +25,7 @@ public static void AddBilling(this WebApplicationBuilder builder) builder.Services.AddHostedService(); builder.Services.AddScoped(); builder.Services.AddHostedService(); + builder.Services.AddHostedService(); } } } \ No newline at end of file