Skip to content

refactor(api): build the Quartz scheduler with Spring Boot auto-configuration - #4054

Open
Macbet wants to merge 3 commits into
mainfrom
refactor/quartz-boot-autoconfig
Open

Macbet wants to merge 3 commits into
mainfrom
refactor/quartz-boot-autoconfig

Conversation

@Macbet

@Macbet Macbet commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

What changed and why?

Part 1 of #4052.

The API declared its own SchedulerFactoryBean, so Spring Boot's Quartz auto-configuration backed off and the spring.quartz.* properties in application.properties were ignored. Boot now builds the scheduler. A SchedulerFactoryBeanCustomizer keeps the scheduler name (SCHED_NAME), thread count, JDBC job store, clustering and driver delegate exactly as before, so existing QRTZ_* rows stay valid.

Nine fixed-schedule registration classes become JobDetail / Trigger beans with the same keys. InactiveJobsService and JobReconciliationSweepService stay programmatic because they also schedule a one-shot run on every boot.

Behaviour changes:

  • spring.quartz.* now applies, including clusterCheckinInterval=20000 (it was the Quartz default, 7500). A dead node's jobs are picked up after about 27s instead of about 15s. Old and new pods can share the cluster during a rolling upgrade.
  • Job and trigger registration runs in one transaction on the JPA transaction manager, and the scheduler starts at the end of context startup instead of during bean creation.
  • A registration failure stops startup instead of being logged: an invalid io.terrakube.policy.drift.cron, io.terrakube.policy.evaluation.retentionCron or io.terrakube.logs.stream-cleanup.cron, or any SchedulerException.
  • A pending policy-drift follow-up trigger now survives a restart.

Testing

Ran: mvn -pl api test (Java 25, Docker running) -> 3560 tests, 0 failures, including the PostgreSQL and MSSQL Testcontainers tests.
     mvn -pl api test -Dtest=QuartzThreadCountTest after the last edits -> 9 tests, 0 failures.
Checked: job and trigger keys, descriptions, job data, crons and misfire policy captured before and after the change on H2 are identical; scheduler name follows io.terrakube.api.plugin.scheduler.instanceName when overridden.

@Macbet
Macbet added this pull request to stack #4056 November 29, 2026 00:10
@github-actions github-actions Bot added documentation Improvements or additions to documentation terrakube-api area/scheduler Touches job scheduling, queueing or locking author/contributor Authored by a returning contributor size/XL 1000 or more lines changed, consider splitting type/refactor Refactor, performance or style change labels Nov 29, 2026
@Macbet
Macbet force-pushed the refactor/quartz-boot-autoconfig branch 2 times, most recently from 5a5c698 to 8fa0929 Compare November 29, 2026 12:55
…guration

The custom SchedulerFactoryBean made Boot's Quartz auto-configuration back
off, so the spring.quartz.* properties were ignored. Boot now builds the
scheduler; a SchedulerFactoryBeanCustomizer keeps the scheduler name
(SCHED_NAME), thread count, JDBC store, clustering and driver delegate as
before.

Nine fixed-schedule registration classes become JobDetail/Trigger beans
with the same keys. InactiveJobsService and JobReconciliationSweepService
stay programmatic because they also schedule a one-shot run per boot.

Behaviour changes:
- spring.quartz.* now applies, including clusterCheckinInterval=20000
  (was the Quartz default 7500).
- Job and trigger registration runs in one transaction on the JPA
  transaction manager, and the scheduler starts at the end of context
  startup instead of during bean creation.
- A registration failure stops startup instead of being logged: an invalid
  io.terrakube.policy.drift.cron, io.terrakube.policy.evaluation.retentionCron
  or io.terrakube.logs.stream-cleanup.cron, or any SchedulerException.
- A pending policy-drift follow-up trigger survives a restart.

Refs #4052
@Macbet
Macbet force-pushed the refactor/quartz-boot-autoconfig branch from 8fa0929 to 64032ad Compare November 29, 2026 12:58
@alfespa17 alfespa17 self-assigned this Nov 29, 2026
@alfespa17

Copy link
Copy Markdown
Member

@Macbet this is a clean, solid refactor. Moving Quartz setup to Spring Boot auto-configuration fixes the silently ignored spring.quartz.* properties, removes ~400 lines of repetitive boilerplate, and improves startup/restart behavior without breaking compatibility with existing QRTZ_* rows.

What looks great

  • Backward compatibility preserved: QuartzSchedulerConfiguration keeps SCHED_NAME (io.terrakube.api.plugin.scheduler.instanceName), thread count (io.terrakube.scheduler.thread-count), LocalDataSourceJobStore, clustering (isClustered=true), and the DataSourceType driver delegate mapping (MSSQLDelegate / PostgreSQLDelegate / StdJDBCDelegate) intact, with good unit/integration test coverage in QuartzThreadCountTest.
  • Safer startup lifecycle: Registration now runs transactionally and the scheduler starts at the end of context startup (SmartLifecycle) instead of during @Bean creation.
  • Fail-fast misconfiguration: Invalid cron overrides or startup SchedulerExceptions now fail startup instead of being logged and leaving jobs silently unscheduled.
  • Restart durability for follow-up triggers: Switching from deleteJob + scheduleJob to spring.quartz.overwrite-existing-jobs=true ensures pending extra triggers (such as TerrakubeV2_PolicyDriftEvaluation_FollowUp) are no longer wiped out on API restarts.

Minor suggestions

  1. Link triggers directly to JobDetail beans in FixedScheduleJobsConfiguration:
    Instead of .forJob(key) with string constants, passing the JobDetail bean into each trigger @Bean method (.forJob(jobDetail) and .withIdentity(jobDetail.getKey().getName())) gives compile-time wiring and avoids repeating key strings.
  2. Log a warning when overriding conflicting spring.quartz.* values:
    QuartzSchedulerConfiguration intentionally overrides schedulerName, threadCount, driverDelegateClass, and isClustered. Logging a WARN when QuartzProperties contains a conflicting value for any of those keys would save operators from debugging silently ignored spring.quartz.* overrides.
  3. Standalone config / release notes:
    • spring.quartz.overwrite-existing-jobs=true is only in application.properties (inherited by profiles normally, though worth keeping in mind for setups using custom spring.config.location).
    • Worth noting in the release notes that clusterCheckinInterval=20000 now takes effect (failover ~27s vs ~15s) and invalid cron overrides will now fail startup.
  4. Can you add spring.quartz.overwrite-existing-jobs=true in application-demo.properties file too? most of the time we use that profile for testing.

…errides

Addresses review feedback on #4054:
- build each fixed-schedule trigger from its JobDetail bean, so the job
  key and description come from one place instead of repeated strings
- log a WARN when QuartzSchedulerConfiguration overrides a conflicting
  spring.quartz.* value (scheduler name, thread count, clustering,
  driver delegate, job store class), so the override is not silent
- set spring.quartz.overwrite-existing-jobs=true in the demo profile too
@Macbet

Macbet commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

@alfespa17 thanks — addressed in ae9a10a:

  1. Triggers wired to JobDetail beans — each trigger is now built from its job bean (trigger(notificationOutboxPollerJobDetail(), ...) → .forJob(jobDetail), identity and description taken from the job), so the key strings live in one place. I call the @Bean method rather than injecting a parameter: the configuration class is proxied, so it returns the registered singleton, and it stays unambiguous with nine JobDetail beans. Keys are unchanged (covered by QuartzThreadCountTest).
  2. WARN on overridden spring.quartz.* — QuartzSchedulerConfiguration now logs a warning when spring.quartz.scheduler-name, threadPool.threadCount, jobStore.isClustered, jobStore.driverDelegateClass or jobStore.class carry a conflicting value, naming the value it uses and where it comes from (e.g. io.terrakube.scheduler.thread-count). Matching values (like the shipped isClustered=true) stay quiet. Covered by QuartzSchedulerConfigurationTest.
  3. Release notes — will note that clusterCheckinInterval=20000 now takes effect (failover ~27s instead of ~15s) and that invalid cron overrides now fail startup.
  4. Demo profile — added spring.quartz.overwrite-existing-jobs=true to application-demo.properties.

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/scheduler Touches job scheduling, queueing or locking author/contributor Authored by a returning contributor documentation Improvements or additions to documentation size/XL 1000 or more lines changed, consider splitting terrakube-api type/refactor Refactor, performance or style change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants