Browse Source

Execute the OpenIddict Quartz job 2 minutes after the application starts

pull/1107/head
Kévin Chalet 6 years ago
parent
commit
00e2bf8e1b
  1. 2
      samples/Mvc.Server/Worker.cs
  2. 2
      src/OpenIddict.MongoDb/OpenIddictMongoDbHelpers.cs
  3. 2
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs
  4. 4
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs
  5. 2
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs
  6. 4
      src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs
  7. 8
      src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs

2
samples/Mvc.Server/Worker.cs

@ -21,7 +21,7 @@ namespace Mvc.Server
using var scope = _serviceProvider.CreateScope(); using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await context.Database.EnsureCreatedAsync(); await context.Database.EnsureCreatedAsync(cancellationToken);
await RegisterApplicationsAsync(scope.ServiceProvider); await RegisterApplicationsAsync(scope.ServiceProvider);
await RegisterScopesAsync(scope.ServiceProvider); await RegisterScopesAsync(scope.ServiceProvider);

2
src/OpenIddict.MongoDb/OpenIddictMongoDbHelpers.cs

@ -35,7 +35,7 @@ namespace MongoDB.Driver
static async IAsyncEnumerable<T> ExecuteAsync(IAsyncCursorSource<T> source, [EnumeratorCancellation] CancellationToken cancellationToken) static async IAsyncEnumerable<T> ExecuteAsync(IAsyncCursorSource<T> source, [EnumeratorCancellation] CancellationToken cancellationToken)
{ {
using var cursor = await source.ToCursorAsync(); using var cursor = await source.ToCursorAsync(cancellationToken);
while (await cursor.MoveNextAsync(cancellationToken)) while (await cursor.MoveNextAsync(cancellationToken))
{ {

2
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs

@ -102,7 +102,7 @@ namespace OpenIddict.MongoDb
if ((await collection.DeleteOneAsync(entity => if ((await collection.DeleteOneAsync(entity =>
entity.Id == application.Id && entity.Id == application.Id &&
entity.ConcurrencyToken == application.ConcurrencyToken)).DeletedCount == 0) entity.ConcurrencyToken == application.ConcurrencyToken, cancellationToken)).DeletedCount == 0)
{ {
throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0239)); throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0239));
} }

4
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs

@ -102,7 +102,7 @@ namespace OpenIddict.MongoDb
if ((await collection.DeleteOneAsync(entity => if ((await collection.DeleteOneAsync(entity =>
entity.Id == authorization.Id && entity.Id == authorization.Id &&
entity.ConcurrencyToken == authorization.ConcurrencyToken)).DeletedCount == 0) entity.ConcurrencyToken == authorization.ConcurrencyToken, cancellationToken)).DeletedCount == 0)
{ {
throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0241)); throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0241));
} }
@ -546,7 +546,7 @@ namespace OpenIddict.MongoDb
// maximum number of elements that can be removed by a single call to PruneAsync() is limited to 50000. // maximum number of elements that can be removed by a single call to PruneAsync() is limited to 50000.
foreach (var buffer in Buffer(identifiers.Take(50_000), 1_000)) foreach (var buffer in Buffer(identifiers.Take(50_000), 1_000))
{ {
await collection.DeleteManyAsync(authorization => buffer.Contains(authorization.Id)); await collection.DeleteManyAsync(authorization => buffer.Contains(authorization.Id), cancellationToken);
} }
static IEnumerable<List<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count) static IEnumerable<List<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count)

2
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs

@ -102,7 +102,7 @@ namespace OpenIddict.MongoDb
if ((await collection.DeleteOneAsync(entity => if ((await collection.DeleteOneAsync(entity =>
entity.Id == scope.Id && entity.Id == scope.Id &&
entity.ConcurrencyToken == scope.ConcurrencyToken)).DeletedCount == 0) entity.ConcurrencyToken == scope.ConcurrencyToken, cancellationToken)).DeletedCount == 0)
{ {
throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0245)); throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0245));
} }

4
src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs

@ -102,7 +102,7 @@ namespace OpenIddict.MongoDb
if ((await collection.DeleteOneAsync(entity => if ((await collection.DeleteOneAsync(entity =>
entity.Id == token.Id && entity.Id == token.Id &&
entity.ConcurrencyToken == token.ConcurrencyToken)).DeletedCount == 0) entity.ConcurrencyToken == token.ConcurrencyToken, cancellationToken)).DeletedCount == 0)
{ {
throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0247)); throw new OpenIddictExceptions.ConcurrencyException(SR.GetResourceString(SR.ID0247));
} }
@ -562,7 +562,7 @@ namespace OpenIddict.MongoDb
// maximum number of elements that can be removed by a single call to PruneAsync() is limited to 50000. // maximum number of elements that can be removed by a single call to PruneAsync() is limited to 50000.
foreach (var buffer in Buffer(identifiers.Take(50_000), 1_000)) foreach (var buffer in Buffer(identifiers.Take(50_000), 1_000))
{ {
await collection.DeleteManyAsync(token => buffer.Contains(token.Id)); await collection.DeleteManyAsync(token => buffer.Contains(token.Id), cancellationToken);
} }
static IEnumerable<List<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count) static IEnumerable<List<TSource>> Buffer<TSource>(IEnumerable<TSource> source, int count)

8
src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs

@ -58,15 +58,15 @@ namespace Microsoft.Extensions.DependencyInjection
descriptor.ImplementationInstance is ITrigger trigger && descriptor.ImplementationInstance is ITrigger trigger &&
trigger.JobKey.Equals(OpenIddictQuartzJob.Identity))) trigger.JobKey.Equals(OpenIddictQuartzJob.Identity)))
{ {
// Note: this trigger uses a quite long interval (1 hour), which means it may be // Note: this trigger uses a quite long interval (1 hour), which means it may be potentially
// potentially never reached if the application is shut down or recycled. As such, // never reached if the application is shut down or recycled. As such, this trigger is set up
// this trigger is set up to fire immediately to ensure it's executed at least once. // to fire 2 minutes after the application starts to ensure it's executed at least once.
builder.Services.AddSingleton( builder.Services.AddSingleton(
TriggerBuilder.Create() TriggerBuilder.Create()
.ForJob(OpenIddictQuartzJob.Identity) .ForJob(OpenIddictQuartzJob.Identity)
.WithSimpleSchedule(options => options.WithIntervalInHours(1).RepeatForever()) .WithSimpleSchedule(options => options.WithIntervalInHours(1).RepeatForever())
.WithDescription(SR.GetResourceString(SR.ID8001)) .WithDescription(SR.GetResourceString(SR.ID8001))
.StartNow() .StartAt(DateBuilder.FutureDate(2, IntervalUnit.Minute))
.Build()); .Build());
} }

Loading…
Cancel
Save