Background Schedulers
Relevant source files
The following files were used as context for generating this wiki page:
Background Schedulers in the Dashboard Task Management (DTM) system are automated components responsible for maintaining data integrity and keeping users informed about time-sensitive events. These schedulers primarily focus on monitoring task deadlines, updating "overdue" statuses, and triggering notifications when tasks exceed their assigned due dates.
The system utilizes Spring's @Scheduled annotation to run these processes at specific intervals, ensuring that even without manual user intervention, the status of tasks remains accurate relative to the current date.
Overdue Task Management
The management of overdue tasks is handled by two specialized scheduler components that operate at different times of the day to ensure consistency. These components interact with the TaskRepository to identify tasks where the dueDate has passed and the status is not yet set to DONE.
Scheduler Logic and Lifecycle
The primary logic involves filtering tasks based on their due date and current status. If a task is found to be past its due date, the system updates the isOverdue flag on the Task entity and persists this change to the database.
The following diagram illustrates the lifecycle of an overdue task check:
This flow ensures that any task missing its deadline is correctly flagged in the UI, often represented by a red indicator or dot.
Sources: src/main/java/com/example/demo/scheduler/OverdueTaskScheduler.java:34-45, src/main/java/com/example/demo/entity/Task.java:82-88, README.md:126-132
Execution Frequency
The system defines specific cron expressions for these background jobs to minimize server load while ensuring timely updates.
| Scheduler Class | Execution Time | Purpose |
|---|---|---|
OverdueTaskScheduler | 09:00 AM Daily | Main daily check and detailed notification routine. |
OverdueNotificationScheduler | 00:05 AM Daily | Early morning synchronization of overdue statuses. |
Sources: src/main/java/com/example/demo/scheduler/OverdueTaskScheduler.java:31, src/main/java/com/example/demo/scheduler/OverdueNotificationScheduler.java:27
Notification Dispatching
A critical role of background schedulers is the automated dispatching of TASK_OVERDUE alerts. When the OverdueTaskScheduler identifies an overdue task, it initiates a notification sequence for relevant stakeholders.
Targeted Recipients
Notifications are not sent blindly; the logic identifies specific participants linked to the task:
- Creator: The user who originally created the task.
- Assignees: All users currently assigned to execute the task.
The scheduler includes logic to prevent duplicate notifications if the creator is also an assignee.
Sources: src/main/java/com/example/demo/scheduler/OverdueTaskScheduler.java:54-78
Notification Flow
The process integrates with the NotificationService to ensure messages are only sent once per day for a specific task to avoid spamming users.
Data Consistency and Persistence
While schedulers handle bulk updates, the Task entity itself contains defensive logic to manage the isOverdue flag during standard CRUD operations.
Entity-Level Overdue Checks
The Task entity uses JPA lifecycle hooks (@PreUpdate and @PrePersist) to verify the overdue status whenever a task is created or modified in the TaskService. This ensures that if a user sets a due date in the past via the API, the task is immediately flagged without waiting for the next scheduler run.
@PreUpdate
protected void onUpdate() {
updatedAt = LocalDateTime.now();
if (dueDate != null && dueDate.isBefore(LocalDate.now()) && status != TaskStatus.DONE) {
isOverdue = true;
}
}
Sources: src/main/java/com/example/demo/entity/Task.java:73-80, src/main/java/com/example/demo/services/TaskService.java:188-195
Implementation Components Summary
| Component | Responsibility | Relevant Files |
|---|---|---|
| Scheduled Jobs | Triggering logic based on cron expressions. | OverdueTaskScheduler.java, OverdueNotificationScheduler.java |
| Persistence | Filtering overdue tasks in the database. | TaskRepository (referenced in TaskService.java) |
| Business Logic | Calculating overdue state and managing history. | TaskService.java, Task.java |
| Notification | Sending alerts to creators and assignees. | NotificationService (referenced in schedulers) |
Sources: src/main/java/com/example/demo/scheduler/OverdueTaskScheduler.java:21-30, src/main/java/com/example/demo/services/TaskService.java:23-35
Conclusion
Background Schedulers are vital for the "Overdue" feature of the Dashboard Task Management system. By combining daily cron-based checks with entity lifecycle listeners, the system guarantees that task deadlines are strictly monitored and that delays are communicated effectively to the team. This dual approach ensures both real-time accuracy and reliable daily reporting.