Fix schedule

This commit is contained in:
2025-02-17 18:06:07 +03:00
parent 1dc1172202
commit 6cd79f689c
4 changed files with 31 additions and 15 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ dependencies {
// https://mvnrepository.com/artifact/io.hypersistence/hypersistence-utils-hibernate-63 // https://mvnrepository.com/artifact/io.hypersistence/hypersistence-utils-hibernate-63
implementation group: 'io.hypersistence', name: 'hypersistence-utils-hibernate-63', version: '3.9.1' implementation group: 'io.hypersistence', name: 'hypersistence-utils-hibernate-63', version: '3.9.1'
implementation 'org.jobrunr:jobrunr-spring-boot-starter:5.3.3' implementation group: 'org.jobrunr', name: 'jobrunr-spring-boot-3-starter', version: '7.4.0'
// https://mvnrepository.com/artifact/org.flywaydb/flyway-database-postgresql // https://mvnrepository.com/artifact/org.flywaydb/flyway-database-postgresql
runtimeOnly group: 'org.flywaydb', name: 'flyway-database-postgresql', version: '11.3.1' runtimeOnly group: 'org.flywaydb', name: 'flyway-database-postgresql', version: '11.3.1'
// https://mvnrepository.com/artifact/org.postgresql/postgresql // https://mvnrepository.com/artifact/org.postgresql/postgresql
@@ -1,10 +1,14 @@
package ru.cathub.telegabot.service.impl; package ru.cathub.telegabot.service.impl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jobrunr.jobs.annotations.Recurring;
import org.jobrunr.scheduling.cron.Cron; import org.jobrunr.scheduling.cron.Cron;
import org.jobrunr.spring.annotations.Recurring;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.cathub.telegabot.model.BotUser; import ru.cathub.telegabot.model.BotUser;
import ru.cathub.telegabot.model.TextRole;
import ru.cathub.telegabot.repository.UserRepository; import ru.cathub.telegabot.repository.UserRepository;
import ru.cathub.telegabot.service.OpenRouterService;
import ru.cathub.telegabot.service.TelegramClientService; import ru.cathub.telegabot.service.TelegramClientService;
import ru.cathub.telegabot.utils.BookFormatUtils; import ru.cathub.telegabot.utils.BookFormatUtils;
@@ -13,24 +17,31 @@ import java.time.Instant;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.List; import java.util.List;
import static ru.cathub.telegabot.utils.BookFormatUtils.escapeNonMarkdown;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j
public class BookReminderScheduler { public class BookReminderScheduler {
private final UserRepository userRepository; private final UserRepository userRepository;
private final TelegramClientService telegramClientService; private final TelegramClientService telegramClientService;
private final OpenRouterService openRouterService;
@Recurring(cron = "0 0 18 * * *", id = "book-reminder") @Recurring(cron = "0 0 18 * * *", id = "book-reminder")
public void checkReadingActivity() { public void checkReadingActivity() {
Date weekAgo = Date.from(Instant.now().minus(7, ChronoUnit.DAYS)); Date weekAgo = new Date(
Instant.now().minus(7, ChronoUnit.DAYS)
.toEpochMilli()
);
userRepository.findUsersNeedingReminder(weekAgo) for (var user : userRepository.findUsersNeedingReminder(weekAgo)) {
.forEach(user -> { try {
String message = "📚 Вы давно не отмечали прочтение книги! Последняя запись: " String prompt = "Придумай креативное и мотивирующее напоминание о том, что пользователь давно не читал книг";
+ BookFormatUtils.formatDate(user.getLastReadDate()); String congrat = openRouterService.getChatResponse(prompt, TextRole.DEFAULT);
telegramClientService.sendMessage(user, message); telegramClientService.sendMessageWithMarkdown(user, escapeNonMarkdown(congrat));
} catch (Exception e) {
user.setLastNotificationDate(new Date(System.currentTimeMillis())); log.error("Error sending reminder to user {}", user.getId(), e);
userRepository.save(user); }
}); };
} }
} }
+6 -3
View File
@@ -52,6 +52,9 @@ spring.datasource.password=${DB_PASSWORD}
openrouter.api.key=${OPENROUTER_API_KEY} openrouter.api.key=${OPENROUTER_API_KEY}
# JobRunr Configuration # JobRunr Configuration
jobrunr.background-job-server.enabled=true
jobrunr.dashboard.enabled=true org.jobrunr.job-scheduler.enabled=true
jobrunr.database.type=sql org.jobrunr.background-job-server.enabled=true
org.jobrunr.database.type=sql
org.jobrunr.dashboard.enabled=true
org.jobrunr.database.tablePrefix=telegabot.
@@ -0,0 +1,2 @@
ALTER TABLE bot_user
ADD last_notification_date date;