forked from openiddict/openiddict-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenIddictQuartzJob.cs
More file actions
186 lines (161 loc) · 7.86 KB
/
OpenIddictQuartzJob.cs
File metadata and controls
186 lines (161 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenIddict.Extensions;
namespace OpenIddict.Quartz;
/// <summary>
/// Represents a Quartz.NET job performing scheduled tasks for OpenIddict.
/// </summary>
[DisallowConcurrentExecution, EditorBrowsable(EditorBrowsableState.Advanced)]
public sealed class OpenIddictQuartzJob : IJob
{
private readonly IOptionsMonitor<OpenIddictQuartzOptions> _options;
private readonly IServiceProvider _provider;
/// <summary>
/// Creates a new instance of the <see cref="OpenIddictQuartzJob"/> class.
/// </summary>
public OpenIddictQuartzJob() => throw new InvalidOperationException(SR.GetResourceString(SR.ID0082));
/// <summary>
/// Creates a new instance of the <see cref="OpenIddictQuartzJob"/> class.
/// </summary>
/// <param name="options">The OpenIddict Quartz.NET options.</param>
/// <param name="provider">The service provider.</param>
public OpenIddictQuartzJob(IOptionsMonitor<OpenIddictQuartzOptions> options, IServiceProvider provider)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
}
/// <summary>
/// Gets the default identity assigned to this job.
/// </summary>
public static JobKey Identity { get; } = new JobKey(
name: SR.GetResourceString(SR.ID8003),
group: SR.GetResourceString(SR.ID8005));
/// <inheritdoc/>
public async Task Execute(IJobExecutionContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
List<Exception>? exceptions = null;
// Note: this job is registered as a transient service. As such, it cannot directly depend on scoped services
// like the core managers. To work around this limitation, a scope is manually created for each invocation.
var scope = _provider.CreateScope();
try
{
// Important: since authorizations that still have tokens attached are never
// pruned, the tokens MUST be deleted before deleting the authorizations.
if (!_options.CurrentValue.DisableTokenPruning)
{
var manager = scope.ServiceProvider.GetService<IOpenIddictTokenManager>() ??
throw new JobExecutionException(new InvalidOperationException(SR.GetResourceString(SR.ID0278)))
{
RefireImmediately = false,
UnscheduleAllTriggers = true,
UnscheduleFiringTrigger = true
};
var threshold = (
#if SUPPORTS_TIME_PROVIDER
_options.CurrentValue.TimeProvider?.GetUtcNow() ??
#endif
DateTimeOffset.UtcNow) - _options.CurrentValue.MinimumTokenLifespan;
try
{
await manager.PruneAsync(threshold, context.CancellationToken);
}
// OperationCanceledExceptions are typically thrown when the host is about to shut down.
// To allow the host to shut down as fast as possible, this exception type is special-cased
// to prevent further processing in this job and inform Quartz.NET it shouldn't be refired.
catch (OperationCanceledException exception) when (context.CancellationToken.IsCancellationRequested)
{
throw new JobExecutionException(exception)
{
RefireImmediately = false
};
}
// AggregateExceptions are generally thrown by the manager itself when one or multiple exception(s)
// occurred while trying to prune the entities. In this case, add the inner exceptions to the collection.
catch (AggregateException exception) when (!OpenIddictHelpers.IsFatal(exception))
{
exceptions ??= [];
exceptions.AddRange(exception.InnerExceptions);
}
// Other non-fatal exceptions are assumed to be transient and are added to the exceptions collection
// to be re-thrown later (typically, at the very end of this job, as an AggregateException).
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
exceptions ??= [];
exceptions.Add(exception);
}
}
if (!_options.CurrentValue.DisableAuthorizationPruning)
{
var manager = scope.ServiceProvider.GetService<IOpenIddictAuthorizationManager>() ??
throw new JobExecutionException(new InvalidOperationException(SR.GetResourceString(SR.ID0278)))
{
RefireImmediately = false,
UnscheduleAllTriggers = true,
UnscheduleFiringTrigger = true
};
var threshold = (
#if SUPPORTS_TIME_PROVIDER
_options.CurrentValue.TimeProvider?.GetUtcNow() ??
#endif
DateTimeOffset.UtcNow) - _options.CurrentValue.MinimumAuthorizationLifespan;
try
{
await manager.PruneAsync(threshold, context.CancellationToken);
}
// OperationCanceledExceptions are typically thrown when the host is about to shut down.
// To allow the host to shut down as fast as possible, this exception type is special-cased
// to prevent further processing in this job and inform Quartz.NET it shouldn't be refired.
catch (OperationCanceledException exception) when (context.CancellationToken.IsCancellationRequested)
{
throw new JobExecutionException(exception)
{
RefireImmediately = false
};
}
// AggregateExceptions are generally thrown by the manager itself when one or multiple exception(s)
// occurred while trying to prune the entities. In this case, add the inner exceptions to the collection.
catch (AggregateException exception) when (!OpenIddictHelpers.IsFatal(exception))
{
exceptions ??= [];
exceptions.AddRange(exception.InnerExceptions);
}
// Other non-fatal exceptions are assumed to be transient and are added to the exceptions collection
// to be re-thrown later (typically, at the very end of this job, as an AggregateException).
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
exceptions ??= [];
exceptions.Add(exception);
}
}
if (exceptions is not null)
{
throw new JobExecutionException(new AggregateException(exceptions))
{
// Only refire the job if the maximum refire count set in the options wasn't reached.
RefireImmediately = context.RefireCount < _options.CurrentValue.MaximumRefireCount
};
}
}
finally
{
if (scope is IAsyncDisposable disposable)
{
await disposable.DisposeAsync();
}
else
{
scope.Dispose();
}
}
}
}