Add scheduler lock support for schedule tasks#50
Conversation
Test Results53 tests 53 ✅ 0s ⏱️ Results for commit 9bf4697. ♻️ This comment has been updated with latest results. |
There was a problem hiding this comment.
Pull request overview
Introduces distributed locking for scheduled notification jobs using ShedLock to prevent concurrent execution across clustered deployments, along with production shutdown/health endpoint configuration updates.
Changes:
- Add ShedLock dependencies and configure a JDBC-based
LockProviderplus@EnableSchedulerLock. - Apply
@SchedulerLockto notification-related scheduled jobs to enforce single-run semantics. - Update production config for graceful shutdown and expose the Actuator health endpoint.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| build.gradle.kts | Adds ShedLock Spring + JDBC template provider dependencies. |
| src/main/kotlin/com/moa/common/config/SchedulingConfig.kt | Enables scheduler locking; configures LockProvider and a pooled TaskScheduler. |
| src/main/kotlin/com/moa/service/notification/NotificationBatchScheduler.kt | Adds lock annotation; adjusts cron schedule. |
| src/main/kotlin/com/moa/service/notification/NotificationDispatchScheduler.kt | Adds lock annotation to per-minute dispatch job. |
| src/main/kotlin/com/moa/service/notification/PaydayNotificationBatchScheduler.kt | Adds lock annotation to daily payday batch job. |
| src/main/resources/application-prod.yml | Enables graceful shutdown; exposes health endpoint with no details. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @Bean | ||
| fun lockProvider(dataSource: DataSource): LockProvider = | ||
| JdbcTemplateLockProvider( | ||
| JdbcTemplateLockProvider.Configuration.builder() | ||
| .withJdbcTemplate(JdbcTemplate(dataSource)) | ||
| .usingDbTime() | ||
| .build() | ||
| ) |
There was a problem hiding this comment.
JdbcTemplateLockProvider requires the ShedLock table to exist in the target database (and H2 for local runs). This PR adds the provider but doesn’t add any schema/migration/init script to create the lock table, which will cause runtime failures when a scheduled method tries to acquire a lock. Please add a DB migration or SQL init for the ShedLock table (and ensure it’s applied in prod/local) before enabling the provider.
| @Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul") | ||
| @SchedulerLock( |
There was a problem hiding this comment.
The cron expression was changed from 0 20 0 * * * to 0 0 0 * * *, which moves the job time from 00:20 to 00:00. Since the PR description is focused on adding distributed locks (not changing schedules), please confirm this timing change is intended; otherwise revert to the previous schedule.
This pull request introduces distributed locking for scheduled tasks using ShedLock, updates scheduler configurations, and enhances application shutdown and monitoring settings. The main goal is to ensure that scheduled jobs are not executed concurrently in clustered environments, improving reliability and preventing duplicate task execution.
Distributed locking for scheduled jobs:
SchedulingConfigto provide aLockProviderbean and enabling@EnableSchedulerLock, ensuring only one instance of each scheduled task runs at a time across multiple nodes. (src/main/kotlin/com/moa/common/config/SchedulingConfig.kt, src/main/kotlin/com/moa/common/config/SchedulingConfig.ktR3-R36)NotificationBatchScheduler,NotificationDispatchScheduler, andPaydayNotificationBatchSchedulerwith@SchedulerLock, specifying unique lock names and lock durations to prevent concurrent execution. (src/main/kotlin/com/moa/service/notification/NotificationBatchScheduler.kt, [1] [2];src/main/kotlin/com/moa/service/notification/NotificationDispatchScheduler.kt, [3] [4];src/main/kotlin/com/moa/service/notification/PaydayNotificationBatchScheduler.kt, [5] [6]Scheduler and shutdown configuration:
TaskSchedulerbean with a thread pool and graceful shutdown settings to improve scheduling reliability and ensure tasks complete on shutdown. (src/main/kotlin/com/moa/common/config/SchedulingConfig.kt, src/main/kotlin/com/moa/common/config/SchedulingConfig.ktR3-R36)application-prod.ymlto enable graceful server shutdown and set a shutdown timeout, ensuring all scheduled tasks have time to finish during application termination. (src/main/resources/application-prod.yml, src/main/resources/application-prod.ymlR11-R24)Monitoring improvements:
src/main/resources/application-prod.yml, src/main/resources/application-prod.ymlR11-R24)