mirror of
https://github.com/bitwarden/server.git
synced 2025-05-29 15:24:51 -05: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 &&
|
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;
|
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 System;
|
||||||
using Bit.Core.Repositories;
|
|
||||||
using Bit.Core.Services;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
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 PremiumRenewalRemindersJob : BaseJob
|
||||||
public class JobsController : Controller
|
|
||||||
{
|
{
|
||||||
private readonly BillingSettings _billingSettings;
|
private readonly BillingSettings _billingSettings;
|
||||||
private readonly GlobalSettings _globalSettings;
|
private readonly GlobalSettings _globalSettings;
|
||||||
private readonly IUserRepository _userRepository;
|
private readonly IUserRepository _userRepository;
|
||||||
private readonly IMailService _mailService;
|
private readonly IMailService _mailService;
|
||||||
|
|
||||||
public JobsController(
|
public PremiumRenewalRemindersJob(
|
||||||
IOptions<BillingSettings> billingSettings,
|
IOptions<BillingSettings> billingSettings,
|
||||||
GlobalSettings globalSettings,
|
GlobalSettings globalSettings,
|
||||||
IUserRepository userRepository,
|
IUserRepository userRepository,
|
||||||
IMailService mailService)
|
IMailService mailService,
|
||||||
|
ILogger<PremiumRenewalRemindersJob> logger)
|
||||||
|
: base(logger)
|
||||||
{
|
{
|
||||||
_billingSettings = billingSettings?.Value;
|
_billingSettings = billingSettings?.Value;
|
||||||
_globalSettings = globalSettings;
|
_globalSettings = globalSettings;
|
||||||
@ -29,14 +32,8 @@ namespace Bit.Billing.Controllers
|
|||||||
_mailService = mailService;
|
_mailService = mailService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("premium-renewal-reminders")]
|
protected async override Task ExecuteJobAsync(IJobExecutionContext context)
|
||||||
public async Task<IActionResult> PostPremiumRenewalReminders([FromQuery] string key)
|
|
||||||
{
|
{
|
||||||
if(key != _billingSettings.JobsKey)
|
|
||||||
{
|
|
||||||
return new BadRequestResult();
|
|
||||||
}
|
|
||||||
|
|
||||||
var users = await _userRepository.GetManyByPremiumRenewalAsync();
|
var users = await _userRepository.GetManyByPremiumRenewalAsync();
|
||||||
foreach(var user in users)
|
foreach(var user in users)
|
||||||
{
|
{
|
||||||
@ -50,8 +47,6 @@ namespace Bit.Billing.Controllers
|
|||||||
}
|
}
|
||||||
await _userRepository.UpdateRenewalReminderDateAsync(user.Id, DateTime.UtcNow);
|
await _userRepository.UpdateRenewalReminderDateAsync(user.Id, DateTime.UtcNow);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OkResult();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -58,6 +58,10 @@ namespace Bit.Billing
|
|||||||
config.Filters.Add(new LoggingExceptionHandlerFilterAttribute());
|
config.Filters.Add(new LoggingExceptionHandlerFilterAttribute());
|
||||||
});
|
});
|
||||||
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
|
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
|
||||||
|
|
||||||
|
// Jobs service
|
||||||
|
Jobs.JobsHostedService.AddJobsServices(services);
|
||||||
|
services.AddHostedService<Jobs.JobsHostedService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(
|
public void Configure(
|
||||||
@ -67,7 +71,17 @@ namespace Bit.Billing
|
|||||||
GlobalSettings globalSettings,
|
GlobalSettings globalSettings,
|
||||||
ILoggerFactory loggerFactory)
|
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())
|
if(env.IsDevelopment())
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user