-
Notifications
You must be signed in to change notification settings - Fork 53
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| 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; | ||
| private Long borrowingId; | ||
| private LocalDate reminderDate; | ||
| private boolean sent; | ||
| @Enumerated(EnumType.STRING) | ||
| private ReminderType type; | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
| 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 | ||
| } |
| 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.libraryman_api.scheduler; | ||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import com.libraryman_api.entity.Reminder; | ||
| import com.libraryman_api.repository.ReminderRepository; | ||
| import com.libraryman_api.service.NotificationService; | ||
|
|
||
| /** | ||
| * Scheduler responsible for sending due-date and overdue book reminders. | ||
| * | ||
| * <p>This scheduler runs daily and processes only reminders that are due | ||
| * on the current date, avoiding unnecessary full-table scans.</p> | ||
| */ | ||
| @Component | ||
| public class ReminderScheduler { | ||
|
|
||
| private static final Logger log = | ||
| LoggerFactory.getLogger(ReminderScheduler.class); | ||
|
|
||
| private final ReminderRepository reminderRepository; | ||
| private final NotificationService notificationService; | ||
|
|
||
| public ReminderScheduler(ReminderRepository reminderRepository, | ||
| NotificationService notificationService) { | ||
| this.reminderRepository = reminderRepository; | ||
| this.notificationService = notificationService; | ||
| } | ||
|
|
||
| /** | ||
| * Sends reminders scheduled for the current date. | ||
| * | ||
| * <p>Runs daily at 9 AM. A reminder is marked as sent only after | ||
| * successful notification delivery.</p> | ||
| */ | ||
| @Scheduled(cron = "0 0 9 * * ?") | ||
| @Transactional | ||
| public void sendDueReminders() { | ||
|
|
||
| LocalDate today = LocalDate.now(); | ||
| List<Reminder> reminders = | ||
| reminderRepository.findByReminderDateAndSentFalse(today); | ||
|
|
||
| for (Reminder reminder : reminders) { | ||
| try { | ||
| notificationService.sendReminder(reminder); // actual notification | ||
| reminder.setSent(true); | ||
| } catch (Exception e) { | ||
| log.error( | ||
| "Failed to send reminder for borrowingId: {}", | ||
| reminder.getBorrowingId(), | ||
| e | ||
| ); | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| reminderRepository.saveAll(reminders); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.libraryman_api.service; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import com.libraryman_api.entity.Reminder; | ||
|
|
||
| @Service | ||
| public class NotificationService { | ||
|
|
||
| private static final Logger log = | ||
| LoggerFactory.getLogger(NotificationService.class); | ||
|
|
||
| public void sendReminder(Reminder reminder) { | ||
| log.info( | ||
| "Sending reminder for borrowingId: {}, Type: {}", | ||
| reminder.getBorrowingId(), | ||
| reminder.getType() | ||
| ); | ||
| // TODO: Integrate with EmailService to send actual email notifications | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implement it also |
||
| } | ||
|
Comment on lines
+15
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace This method currently only prints to stdout, which is not suitable for production. Use SLF4J logger for consistency with the rest of the codebase (e.g., 🔎 Proposed logging fix and integration approach package com.libraryman_api.service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.libraryman_api.entity.Reminder;
@Service
public class NotificationService {
+ private static final Logger log = LoggerFactory.getLogger(NotificationService.class);
+
public void sendReminder(Reminder reminder) {
-
- System.out.println(
- "Reminder sent for borrowingId: " + reminder.getBorrowingId() +
- " Type: " + reminder.getType()
+ log.info(
+ "Sending reminder for borrowingId: {}, Type: {}",
+ reminder.getBorrowingId(),
+ reminder.getType()
);
+ // TODO: Integrate with EmailService to send actual email notifications
}
}🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.