question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quartz Never Runs and Performs Cleanup of Tokens and Authorizations

See original GitHub issue

Confirm you’ve already contributed to this project or that you sponsor it

  • I confirm I’m a sponsor or a contributor

Version

3.x

Question

I am slightly confused here. I have followed the examples and looked at the configuration class in the source code. I am adding the Quartz support

public static IServiceCollection AddQuartzServices(this IServiceCollection services)
{
	// Quartz here is used to automatically prune/remove authorization and token entries that
	// are no longer valid from the database.
	return services
		.AddQuartz(options =>
		{
			options.UseMicrosoftDependencyInjectionJobFactory();
			options.UseSimpleTypeLoader();
			options.UseInMemoryStore();
		})
		// Register the Quartz.NET service and configure it to block shutdown until jobs are complete.
		.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);
}

I then add the Core service and initialize with UseQuartz

services
	.AddOpenIddict()
	.AddCore(options =>
	{
		options.UseMongoDb()
			   .UseDatabase(new MongoClient(_mongoDbSettings.ConnectionString).GetDatabase(_mongoDbSettings.DatabaseName))
			   .SetApplicationsCollectionName("applications")
			   .SetAuthorizationsCollectionName("authorizations")
			   .SetTokensCollectionName("tokens")
			   .SetScopesCollectionName("scopes");

		options.UseQuartz(builder => 
		{ 
			builder.Configure(o => { o.MaximumRefireCount = 3; }); 
		});

               // I have also tried
               // options.UseQuartz();
	})

This gives the following output,

[12:39:12.720 INF] [] [Quartz.Core.SchedulerSignalerImpl] Initialized Scheduler Signaller of type: Quartz.Core.SchedulerSignalerImpl [12:39:12.721 INF] [] [Quartz.Core.QuartzScheduler] Quartz Scheduler created [12:39:12.721 INF] [] [Quartz.Core.QuartzScheduler] JobFactory set to: Quartz.Simpl.MicrosoftDependencyInjectionJobFactory [12:39:12.721 INF] [] [Quartz.Simpl.RAMJobStore] RAMJobStore initialized. [12:39:12.723 INF] [] [Quartz.Core.QuartzScheduler] Scheduler meta-data: Quartz Scheduler (v3.4.0.0) ‘QuartzScheduler’ with instanceId ‘NON_CLUSTERED’ Scheduler class: ‘Quartz.Core.QuartzScheduler’ - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool ‘Quartz.Simpl.DefaultThreadPool’ - with 10 threads. Using job-store ‘Quartz.Simpl.RAMJobStore’ - which does not support persistence. and is not clustered.

[12:39:12.723 INF] [] [Quartz.Impl.StdSchedulerFactory] Quartz scheduler ‘QuartzScheduler’ initialized [12:39:12.723 INF] [] [Quartz.Impl.StdSchedulerFactory] Quartz scheduler version: 3.4.0.0 [12:39:12.738 INF] [] [Quartz.ContainerConfigurationProcessor] Adding 1 jobs, 1 triggers. [12:39:12.739 INF] [] [Quartz.ContainerConfigurationProcessor] Adding job: OpenIddict.Quartz.OpenIddictQuartzJob // … Other logging … [12:39:13.316 WRN] [] [Microsoft.AspNetCore.Server.Kestrel] Overriding address(es) ‘“https://localhost:6699”’. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead. [12:39:13.337 INF] [] [Microsoft.Hosting.Lifetime] Now listening on: “https://[::]:6699” [12:39:13.337 INF] [] [Microsoft.Hosting.Lifetime] Application started. Press Ctrl+C to shut down. [12:39:13.337 INF] [] [Microsoft.Hosting.Lifetime] Hosting environment: “Localhost” [12:39:13.338 INF] [] [Microsoft.Hosting.Lifetime] Content root path: “C:\VRPM\Repos\authentication-server\src\Services\Migr8.AuthenticationServer\bin\Debug\net6.0” [12:39:13.343 INF] [] [Quartz.Core.QuartzScheduler] Scheduler QuartzScheduler_$_NON_CLUSTERED started.

but the cleanup never fires and the code that is supposed to be triggered between 1 to 10 min after initialization in OpenIddictQuartzConfiguration

options.AddTrigger(builder =>
	{
		// Note: this trigger uses a quite long interval (1 hour), which means it may be potentially never
		// reached if the application is shut down or recycled. As such, this trigger is set up to fire
		// between 1 and 10 minutes after the application starts to ensure the job is executed at least once.
		builder.ForJob(OpenIddictQuartzJob.Identity)
			   .WithSimpleSchedule(options => options.WithIntervalInHours(1).RepeatForever())
			   .WithDescription(SR.GetResourceString(SR.ID8002))
			   .StartAt(DateBuilder.FutureDate(new Random().Next(1, 10), IntervalUnit.Minute));
	});

never runs.

What am I doing wrong here?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Camuvingiancommented, Jul 17, 2022

Makes perfect sense, thank you for your time and all the fantastic work with this project.

0reactions
kevinchaletcommented, Sep 6, 2022

Hi,

Does OpenIddict automatically set the token and authorization status’s based on the token lifetimes? These being configured with:

No, the pruning job doesn’t change the status and directly uses the expiration_date field to determine whether the expiration date has been reached or not.

Where can I set OpenIddictQuartzOptions.MinimumTokenLifespan and OpenIddictQuartzOptions.MinimumAuthorizationLifespan or are these set with the config I use above?

options.UseQuartz()
       .SetMinimumAuthorizationLifespan(TimeSpan.FromDays(1))
       .SetMinimumTokenLifespan(TimeSpan.FromDays(1));

Based on the thread above, I reduced the token lifespan to 7 days, but the tokens and authorizations are not being pruned.

Try using IOpenIddictTokenManager.PruneAsync() and IOpenIddictAuthorizationManager.PruneAsync() manually with the threshold of your choice to see if it correctly removes the expired tokens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CAS - Ticket Registry Cleaner
The ticket registry cleaner use case primarily addresses stale tickets that would otherwise never be requested and processed to go through the on-demand ......
Read more >
Operations Dashboard | Adobe Experience Manager
The Operations Dashboard in AEM 6 helps system operators to monitor AEM system health at a glance. It also provides auto-generated diagnosis ...
Read more >
Jobs API 2.0 | Databricks on AWS
Jobs enable you to run non-interactive code in a Databricks cluster. ... You should never hard code secrets or store them in plain...
Read more >
Common Application Properties
Name Description Default... spring.cassandra.config Location of the configuration file to use. spring.cassandra.controlconnection.timeout Timeout to use for control queries. 5s spring.cassandra.keyspace‑name Keyspace name to use.
Read more >
Database Cleanup
This command cleans up expired tokens, unused grants and expired. JTI . If the session cache persists data into the database, the expired...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found