mirror of
https://github.com/bitwarden/server.git
synced 2025-02-03 23:51:21 +01:00
move premium renewal job to hosted job service
This commit is contained in:
parent
aa647c37d3
commit
5f79af2e18
@ -159,7 +159,8 @@ namespace Bit.Api
|
||||
}
|
||||
|
||||
if(e.Level == LogEventLevel.Information &&
|
||||
(context.Contains(typeof(IpRateLimitMiddleware).FullName) || context.StartsWith("\"Bit.Api.Jobs")))
|
||||
(context.Contains(typeof(IpRateLimitMiddleware).FullName) ||
|
||||
context.StartsWith("\"Bit.Api.Jobs") || context.StartsWith("\"Bit.Core.Jobs")))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
40
src/Billing/Jobs/JobsHostedService.cs
Normal file
40
src/Billing/Jobs/JobsHostedService.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Jobs;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Billing.Jobs
|
||||
{
|
||||
public class JobsHostedService : BaseJobsHostedService
|
||||
{
|
||||
public JobsHostedService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<JobsHostedService> logger,
|
||||
ILogger<JobListener> listenerLogger)
|
||||
: base(serviceProvider, logger, listenerLogger) { }
|
||||
|
||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var everyDayAtNinePmTrigger = TriggerBuilder.Create()
|
||||
.StartNow()
|
||||
.WithCronSchedule("0 0 21 * * ?")
|
||||
.Build();
|
||||
|
||||
Jobs = new List<Tuple<Type, ITrigger>>
|
||||
{
|
||||
new Tuple<Type, ITrigger>(typeof(PremiumRenewalRemindersJob), everyDayAtNinePmTrigger)
|
||||
};
|
||||
|
||||
await base.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public static void AddJobsServices(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<PremiumRenewalRemindersJob>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +1,30 @@
|
||||
using Bit.Core;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Jobs;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Billing.Controllers
|
||||
namespace Bit.Billing.Jobs
|
||||
{
|
||||
[Route("jobs")]
|
||||
public class JobsController : Controller
|
||||
public class PremiumRenewalRemindersJob : BaseJob
|
||||
{
|
||||
private readonly BillingSettings _billingSettings;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IMailService _mailService;
|
||||
|
||||
public JobsController(
|
||||
public PremiumRenewalRemindersJob(
|
||||
IOptions<BillingSettings> billingSettings,
|
||||
GlobalSettings globalSettings,
|
||||
IUserRepository userRepository,
|
||||
IMailService mailService)
|
||||
IMailService mailService,
|
||||
ILogger<PremiumRenewalRemindersJob> logger)
|
||||
: base(logger)
|
||||
{
|
||||
_billingSettings = billingSettings?.Value;
|
||||
_globalSettings = globalSettings;
|
||||
@ -29,14 +32,8 @@ namespace Bit.Billing.Controllers
|
||||
_mailService = mailService;
|
||||
}
|
||||
|
||||
[HttpPost("premium-renewal-reminders")]
|
||||
public async Task<IActionResult> PostPremiumRenewalReminders([FromQuery] string key)
|
||||
protected async override Task ExecuteJobAsync(IJobExecutionContext context)
|
||||
{
|
||||
if(key != _billingSettings.JobsKey)
|
||||
{
|
||||
return new BadRequestResult();
|
||||
}
|
||||
|
||||
var users = await _userRepository.GetManyByPremiumRenewalAsync();
|
||||
foreach(var user in users)
|
||||
{
|
||||
@ -50,8 +47,6 @@ namespace Bit.Billing.Controllers
|
||||
}
|
||||
await _userRepository.UpdateRenewalReminderDateAsync(user.Id, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
return new OkResult();
|
||||
}
|
||||
}
|
||||
}
|
@ -58,6 +58,10 @@ namespace Bit.Billing
|
||||
config.Filters.Add(new LoggingExceptionHandlerFilterAttribute());
|
||||
});
|
||||
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
|
||||
|
||||
// Jobs service
|
||||
Jobs.JobsHostedService.AddJobsServices(services);
|
||||
services.AddHostedService<Jobs.JobsHostedService>();
|
||||
}
|
||||
|
||||
public void Configure(
|
||||
@ -67,7 +71,17 @@ namespace Bit.Billing
|
||||
GlobalSettings globalSettings,
|
||||
ILoggerFactory loggerFactory)
|
||||
{
|
||||
loggerFactory.AddSerilog(app, env, appLifetime, globalSettings, (e) => e.Level >= LogEventLevel.Error);
|
||||
loggerFactory.AddSerilog(app, env, appLifetime, globalSettings, (e) =>
|
||||
{
|
||||
var context = e.Properties["SourceContext"].ToString();
|
||||
if(e.Level == LogEventLevel.Information &&
|
||||
(context.StartsWith("\"Bit.Billing.Jobs") || context.StartsWith("\"Bit.Core.Jobs")))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return e.Level >= LogEventLevel.Error;
|
||||
});
|
||||
|
||||
if(env.IsDevelopment())
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user