-
Notifications
You must be signed in to change notification settings - Fork 54
Optimize reminder scheduling using dedicated Reminder entity #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
divyanshkande
wants to merge
3
commits into
ajaynegi33:main
Choose a base branch
from
divyanshkande:optimize-reminder-scheduler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package com.libraryman_api.entity; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| /** | ||
| * Entity representing a scheduled reminder for borrowed books. | ||
| * | ||
| * <p>This entity is created at the time of borrowing a book and is used to | ||
| * efficiently schedule due-date and overdue notifications without scanning | ||
| * all borrow records daily.</p> | ||
| * | ||
| * <p>Each reminder is triggered on a specific date and marked as sent | ||
| * after notification delivery.</p> | ||
| */ | ||
|
|
||
| @Entity | ||
| public class Reminder { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public Long getBorrowingId() { | ||
| return borrowingId; | ||
| } | ||
|
|
||
| public void setBorrowingId(Long borrowingId) { | ||
| this.borrowingId = borrowingId; | ||
| } | ||
|
|
||
| public LocalDate getReminderDate() { | ||
| return reminderDate; | ||
| } | ||
|
|
||
| public void setReminderDate(LocalDate reminderDate) { | ||
| this.reminderDate = reminderDate; | ||
| } | ||
|
|
||
| public boolean isSent() { | ||
| return sent; | ||
| } | ||
|
|
||
| public void setSent(boolean sent) { | ||
| this.sent = sent; | ||
| } | ||
|
|
||
| public ReminderType getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public void setType(ReminderType type) { | ||
| this.type = type; | ||
| } | ||
|
|
||
| private Long borrowingId; | ||
| private LocalDate reminderDate; | ||
| private boolean sent; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| private ReminderType type; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.libraryman_api.entity; | ||
| /** | ||
| * Defines the type of reminder to be sent. | ||
| * | ||
| * DUE_SOON - Reminder sent before the book due date. | ||
| * OVERDUE - Daily reminder sent after the due date has passed. | ||
| */ | ||
| public enum ReminderType { | ||
| DUE_SOON, | ||
| OVERDUE | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/libraryman_api/repository/ReminderRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.libraryman_api.repository; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import com.libraryman_api.entity.Reminder; | ||
| /** | ||
| * Repository for accessing and managing Reminder entities. | ||
| * | ||
| * Provides optimized queries to fetch only pending reminders | ||
| * scheduled for a specific date. | ||
| */ | ||
|
|
||
| public interface ReminderRepository extends JpaRepository<Reminder, Long> { | ||
| List<Reminder> findByReminderDateAndSentFalse(LocalDate date); | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/libraryman_api/scheduler/ReminderScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.libraryman_api.scheduler; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import com.libraryman_api.entity.Reminder; | ||
| import com.libraryman_api.repository.ReminderRepository; | ||
|
|
||
| @Component | ||
| /** | ||
| * Scheduler responsible for sending due-date and overdue book reminders. | ||
| * | ||
| * <p>This scheduler queries only the reminders that are due on the current | ||
| * date, avoiding full table scans of borrowing records and significantly | ||
| * improving system performance.</p> | ||
| */ | ||
|
|
||
| public class ReminderScheduler { | ||
|
|
||
| private final ReminderRepository reminderRepository; | ||
| public ReminderScheduler(ReminderRepository reminderRepository) { | ||
| this.reminderRepository = reminderRepository; | ||
| } | ||
| /** | ||
| * Sends reminders scheduled for the current date. | ||
| * | ||
| * <p>Executed daily at 9 AM to process pending reminders and mark them | ||
| * as sent after successful notification delivery.</p> | ||
| */ | ||
|
|
||
| @Scheduled(cron = "0 0 9 * * ?") | ||
| public void sendDueReminders() { | ||
|
|
||
| LocalDate today = LocalDate.now(); | ||
| List<Reminder> reminders = | ||
| reminderRepository.findByReminderDateAndSentFalse(today); | ||
|
|
||
| for (Reminder reminder : reminders) { | ||
| // call notification logic | ||
| reminder.setSent(true); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| reminderRepository.saveAll(reminders); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.