mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
Move jobs to api hosted service w/ quartz
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" />
|
||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.6.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.1.2" />
|
||||
<PackageReference Include="Quartz" Version="3.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -4,7 +4,6 @@ LABEL com.bitwarden.product="bitwarden"
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
cron \
|
||||
gosu \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
@ -12,11 +11,7 @@ ENV ASPNETCORE_URLS http://+:5000
|
||||
WORKDIR /app
|
||||
EXPOSE 5000
|
||||
COPY obj/Docker/publish/Api .
|
||||
COPY obj/Docker/publish/Jobs /jobs
|
||||
COPY entrypoint.sh /
|
||||
RUN chmod +x /entrypoint.sh
|
||||
|
||||
RUN mv /jobs/crontab /etc/cron.d/bitwarden-cron \
|
||||
&& chmod 0644 /etc/cron.d/bitwarden-cron
|
||||
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
|
23
src/Api/Jobs/AliveJob.cs
Normal file
23
src/Api/Jobs/AliveJob.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class AliveJob : IJob
|
||||
{
|
||||
private readonly ILogger<AliveJob> _logger;
|
||||
|
||||
public AliveJob(
|
||||
ILogger<AliveJob> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
_logger.LogInformation("It's alive!");
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
27
src/Api/Jobs/JobFactory.cs
Normal file
27
src/Api/Jobs/JobFactory.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Quartz;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class JobFactory : IJobFactory
|
||||
{
|
||||
private readonly IServiceProvider _container;
|
||||
|
||||
public JobFactory(IServiceProvider container)
|
||||
{
|
||||
_container = container;
|
||||
}
|
||||
|
||||
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
|
||||
{
|
||||
return _container.GetService(bundle.JobDetail.JobType) as IJob;
|
||||
}
|
||||
|
||||
public void ReturnJob(IJob job)
|
||||
{
|
||||
var disposable = job as IDisposable;
|
||||
disposable?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
40
src/Api/Jobs/JobListener.cs
Normal file
40
src/Api/Jobs/JobListener.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class JobListener : IJobListener
|
||||
{
|
||||
private readonly ILogger<JobListener> _logger;
|
||||
|
||||
public JobListener(ILogger<JobListener> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string Name => "JobListener";
|
||||
|
||||
public Task JobExecutionVetoed(IJobExecutionContext context,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task JobToBeExecuted(IJobExecutionContext context,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
_logger.LogInformation("Starting job {0} at {1}.", context.JobDetail.JobType.Name, DateTime.UtcNow);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
_logger.LogInformation("Finished job {0} at {1}.", context.JobDetail.JobType.Name, DateTime.UtcNow);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
80
src/Api/Jobs/JobsHostedService.cs
Normal file
80
src/Api/Jobs/JobsHostedService.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Quartz.Impl.Matchers;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class JobsHostedService : IHostedService, IDisposable
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger<JobListener> _listenerLogger;
|
||||
private IScheduler _scheduler;
|
||||
|
||||
public JobsHostedService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<JobsHostedService> logger,
|
||||
ILogger<JobListener> listenerLogger)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
_listenerLogger = listenerLogger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var factory = new StdSchedulerFactory(new NameValueCollection
|
||||
{
|
||||
{ "quartz.serializer.type", "binary" }
|
||||
});
|
||||
_scheduler = await factory.GetScheduler(cancellationToken);
|
||||
_scheduler.JobFactory = new JobFactory(_serviceProvider);
|
||||
_scheduler.ListenerManager.AddJobListener(new JobListener(_listenerLogger),
|
||||
GroupMatcher<JobKey>.AnyGroup());
|
||||
await _scheduler.Start(cancellationToken);
|
||||
|
||||
var aliveJob = JobBuilder.Create<AliveJob>().Build();
|
||||
var validateUsersJob = JobBuilder.Create<ValidateUsersJob>().Build();
|
||||
var validateOrganizationsJob = JobBuilder.Create<ValidateOrganizationsJob>().Build();
|
||||
|
||||
var everyTopOfTheHourTrigger = TriggerBuilder.Create()
|
||||
.StartNow()
|
||||
.WithCronSchedule("0 0 * * * ?")
|
||||
.Build();
|
||||
var everyTopOfTheSixthHourTrigger = TriggerBuilder.Create()
|
||||
.StartNow()
|
||||
.WithCronSchedule("0 0 */6 * * ?")
|
||||
.Build();
|
||||
var everyTwelfthHourAndThirtyMinutesTrigger = TriggerBuilder.Create()
|
||||
.StartNow()
|
||||
.WithCronSchedule("0 30 */12 * * ?")
|
||||
.Build();
|
||||
|
||||
await _scheduler.ScheduleJob(aliveJob, everyTopOfTheHourTrigger);
|
||||
await _scheduler.ScheduleJob(validateUsersJob, everyTopOfTheSixthHourTrigger);
|
||||
await _scheduler.ScheduleJob(validateOrganizationsJob, everyTwelfthHourAndThirtyMinutesTrigger);
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _scheduler?.Shutdown(cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{ }
|
||||
|
||||
public static void AddJobsServices(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<AliveJob>();
|
||||
services.AddTransient<ValidateUsersJob>();
|
||||
services.AddTransient<ValidateOrganizationsJob>();
|
||||
}
|
||||
}
|
||||
}
|
34
src/Api/Jobs/ValidateOrganizationsJob.cs
Normal file
34
src/Api/Jobs/ValidateOrganizationsJob.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class ValidateOrganizationsJob : IJob
|
||||
{
|
||||
private readonly ILicensingService _licensingService;
|
||||
private readonly ILogger<ValidateOrganizationsJob> _logger;
|
||||
|
||||
public ValidateOrganizationsJob(
|
||||
ILicensingService licensingService,
|
||||
ILogger<ValidateOrganizationsJob> logger)
|
||||
{
|
||||
_licensingService = licensingService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _licensingService.ValidateOrganizationsAsync();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(2, e, "Error performing {0}.", nameof(ValidateOrganizationsJob));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
src/Api/Jobs/ValidateUsersJob.cs
Normal file
34
src/Api/Jobs/ValidateUsersJob.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class ValidateUsersJob : IJob
|
||||
{
|
||||
private readonly ILicensingService _licensingService;
|
||||
private readonly ILogger<ValidateUsersJob> _logger;
|
||||
|
||||
public ValidateUsersJob(
|
||||
ILicensingService licensingService,
|
||||
ILogger<ValidateUsersJob> logger)
|
||||
{
|
||||
_licensingService = licensingService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _licensingService.ValidateUsersAsync();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(2, e, "Error performing {0}.", nameof(ValidateUsersJob));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -126,9 +126,15 @@ namespace Bit.Api
|
||||
config.Filters.Add(new ModelStateValidationFilterAttribute());
|
||||
}).AddJsonOptions(o => o.SerializerSettings.ContractResolver = new DefaultContractResolver());
|
||||
|
||||
// PDF generation
|
||||
if(!globalSettings.SelfHosted)
|
||||
if(globalSettings.SelfHosted)
|
||||
{
|
||||
// Jobs service
|
||||
Jobs.JobsHostedService.AddJobsServices(services);
|
||||
services.AddHostedService<Jobs.JobsHostedService>();
|
||||
}
|
||||
else
|
||||
{
|
||||
// PDF generation
|
||||
services.AddJsReport(new jsreport.Local.LocalReporting()
|
||||
.UseBinary(jsreport.Binary.JsReportBinary.GetBinary())
|
||||
.AsUtility()
|
||||
|
@ -9,13 +9,10 @@ echo -e "\nBuilding app"
|
||||
echo ".NET Core version $(dotnet --version)"
|
||||
echo "Restore"
|
||||
dotnet restore $DIR/Api.csproj
|
||||
dotnet restore $DIR/../Jobs/Jobs.csproj
|
||||
echo "Clean"
|
||||
dotnet clean $DIR/Api.csproj -c "Release" -o $DIR/obj/Docker/publish/Api
|
||||
dotnet clean $DIR/../Jobs/Jobs.csproj -c "Release" -o $DIR/obj/Docker/publish/Jobs
|
||||
echo "Publish"
|
||||
dotnet publish $DIR/Api.csproj -c "Release" -o $DIR/obj/Docker/publish/Api
|
||||
dotnet publish $DIR/../Jobs/Jobs.csproj -c "Release" -o $DIR/obj/Docker/publish/Jobs
|
||||
|
||||
echo -e "\nBuilding docker image"
|
||||
docker --version
|
||||
|
@ -29,19 +29,12 @@ mkhomedir_helper $USERNAME
|
||||
|
||||
# The rest...
|
||||
|
||||
touch /var/log/cron.log
|
||||
chown $USERNAME:$GROUPNAME /var/log/cron.log
|
||||
chown -R $USERNAME:$GROUPNAME /app
|
||||
chown -R $USERNAME:$GROUPNAME /jobs
|
||||
mkdir -p /etc/bitwarden/core
|
||||
mkdir -p /etc/bitwarden/logs
|
||||
mkdir -p /etc/bitwarden/ca-certificates
|
||||
chown -R $USERNAME:$GROUPNAME /etc/bitwarden
|
||||
|
||||
# Sounds like gosu keeps env when switching, but of course cron does not
|
||||
env > /etc/environment
|
||||
cron
|
||||
|
||||
cp /etc/bitwarden/ca-certificates/*.crt /usr/local/share/ca-certificates/ >/dev/null 2>&1 \
|
||||
&& update-ca-certificates
|
||||
|
||||
|
Reference in New Issue
Block a user