feat: add daily book reading reminder scheduler with JobRunr
This commit is contained in:
@@ -50,6 +50,7 @@ dependencies {
|
||||
|
||||
// https://mvnrepository.com/artifact/io.hypersistence/hypersistence-utils-hibernate-63
|
||||
implementation group: 'io.hypersistence', name: 'hypersistence-utils-hibernate-63', version: '3.9.1'
|
||||
implementation 'org.jobrunr:jobrunr-spring-boot-starter:5.3.3'
|
||||
// https://mvnrepository.com/artifact/org.flywaydb/flyway-database-postgresql
|
||||
runtimeOnly group: 'org.flywaydb', name: 'flyway-database-postgresql', version: '11.3.1'
|
||||
// https://mvnrepository.com/artifact/org.postgresql/postgresql
|
||||
|
||||
@@ -50,5 +50,8 @@ public class BotUser {
|
||||
private List<Book> books;
|
||||
|
||||
private Date lastReadDate;
|
||||
|
||||
@Column(name = "last_notification_date")
|
||||
private Date lastNotificationDate;
|
||||
|
||||
}
|
||||
|
||||
@@ -3,5 +3,14 @@ package ru.cathub.telegabot.repository;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.cathub.telegabot.model.BotUser;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import ru.cathub.telegabot.model.BotUser;
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository extends JpaRepository<BotUser, Long> {
|
||||
@Query("SELECT u FROM BotUser u WHERE u.lastReadDate < :weekAgo AND " +
|
||||
"(u.lastNotificationDate IS NULL OR u.lastNotificationDate < :weekAgo)")
|
||||
List<BotUser> findUsersNeedingReminder(@Param("weekAgo") Date weekAgo);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package ru.cathub.telegabot.service.impl;
|
||||
|
||||
import org.jobrunr.scheduling.cron.Cron;
|
||||
import org.jobrunr.spring.annotations.Recurring;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.cathub.telegabot.model.BotUser;
|
||||
import ru.cathub.telegabot.repository.UserRepository;
|
||||
import ru.cathub.telegabot.service.TelegramClientService;
|
||||
import ru.cathub.telegabot.utils.BookFormatUtils;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BookReminderScheduler {
|
||||
private final UserRepository userRepository;
|
||||
private final TelegramClientService telegramClientService;
|
||||
|
||||
@Recurring(cron = "0 0 18 * * *", id = "book-reminder")
|
||||
public void checkReadingActivity() {
|
||||
Date weekAgo = Date.from(Instant.now().minus(7, ChronoUnit.DAYS));
|
||||
|
||||
userRepository.findUsersNeedingReminder(weekAgo)
|
||||
.forEach(user -> {
|
||||
String message = "📚 Вы давно не отмечали прочтение книги! Последняя запись: "
|
||||
+ BookFormatUtils.formatDate(user.getLastReadDate());
|
||||
telegramClientService.sendMessage(user, message);
|
||||
|
||||
user.setLastNotificationDate(new Date(System.currentTimeMillis()));
|
||||
userRepository.save(user);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -49,4 +49,9 @@ spring.datasource.username=${DB_USER}
|
||||
spring.datasource.password=${DB_PASSWORD}
|
||||
|
||||
# OpenRouter AI
|
||||
openrouter.api.key=${OPENROUTER_API_KEY}
|
||||
openrouter.api.key=${OPENROUTER_API_KEY}
|
||||
|
||||
# JobRunr Configuration
|
||||
jobrunr.background-job-server.enabled=true
|
||||
jobrunr.dashboard.enabled=true
|
||||
jobrunr.database.type=sql
|
||||
|
||||
Reference in New Issue
Block a user