Skip to content

Commit d288f3c

Browse files
authored
feat(cron): improve error handling in scheduled tasks (#2330)
2 parents 58891c4 + 6b98884 commit d288f3c

File tree

6 files changed

+151
-110
lines changed

6 files changed

+151
-110
lines changed

src/main/java/ai/elimu/tasks/LetterSoundUsageCountScheduler.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import ai.elimu.dao.WordDao;
55
import ai.elimu.entity.content.LetterSound;
66
import ai.elimu.entity.content.Word;
7+
import ai.elimu.util.DiscordHelper;
8+
import ai.elimu.util.DiscordHelper.Channel;
79

810
import java.util.HashMap;
911
import java.util.Map;
@@ -28,20 +30,25 @@ public class LetterSoundUsageCountScheduler {
2830
public synchronized void execute() {
2931
log.info("execute");
3032

31-
// <ID, frequency>
32-
Map<Long, Integer> frequencyMap = new HashMap<>();
33+
try {
34+
// <ID, frequency>
35+
Map<Long, Integer> frequencyMap = new HashMap<>();
3336

34-
// Calculate the frequency of each letter-sound
35-
for (Word word : wordDao.readAll()) {
36-
for (LetterSound letterSound : word.getLetterSounds()) {
37-
frequencyMap.put(letterSound.getId(), frequencyMap.getOrDefault(letterSound.getId(), 0) + word.getUsageCount());
37+
// Calculate the frequency of each letter-sound
38+
for (Word word : wordDao.readAll()) {
39+
for (LetterSound letterSound : word.getLetterSounds()) {
40+
frequencyMap.put(letterSound.getId(), frequencyMap.getOrDefault(letterSound.getId(), 0) + word.getUsageCount());
41+
}
3842
}
39-
}
4043

41-
// Update the values previously stored in the database
42-
for (LetterSound letterSound : letterSoundDao.readAll()) {
43-
letterSound.setUsageCount(frequencyMap.getOrDefault(letterSound.getId(), 0));
44-
letterSoundDao.update(letterSound);
44+
// Update the values previously stored in the database
45+
for (LetterSound letterSound : letterSoundDao.readAll()) {
46+
letterSound.setUsageCount(frequencyMap.getOrDefault(letterSound.getId(), 0));
47+
letterSoundDao.update(letterSound);
48+
}
49+
} catch (Exception e) {
50+
log.error("Error in scheduled task:", e);
51+
DiscordHelper.postToChannel(Channel.CONTENT, "Error in `" + e.getClass() + ": " + e.getMessage() + "`");
4552
}
4653

4754
log.info("execute complete");

src/main/java/ai/elimu/tasks/LetterUsageCountScheduler.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import ai.elimu.dao.LetterSoundDao;
55
import ai.elimu.entity.content.Letter;
66
import ai.elimu.entity.content.LetterSound;
7+
import ai.elimu.util.DiscordHelper;
8+
import ai.elimu.util.DiscordHelper.Channel;
79

810
import java.util.HashMap;
911
import java.util.Map;
@@ -28,20 +30,25 @@ public class LetterUsageCountScheduler {
2830
public synchronized void execute() {
2931
log.info("execute");
3032

31-
// <ID, frequency>
32-
Map<Long, Integer> frequencyMap = new HashMap<>();
33-
34-
// Calculate the frequency of each letter
35-
for (LetterSound letterSound : letterSoundDao.readAll()) {
36-
for (Letter letter : letterSound.getLetters()) {
37-
frequencyMap.put(letter.getId(), frequencyMap.getOrDefault(letter.getId(), 0) + letterSound.getUsageCount());
33+
try {
34+
// <ID, frequency>
35+
Map<Long, Integer> frequencyMap = new HashMap<>();
36+
37+
// Calculate the frequency of each letter
38+
for (LetterSound letterSound : letterSoundDao.readAll()) {
39+
for (Letter letter : letterSound.getLetters()) {
40+
frequencyMap.put(letter.getId(), frequencyMap.getOrDefault(letter.getId(), 0) + letterSound.getUsageCount());
41+
}
3842
}
39-
}
4043

41-
// Update the values previously stored in the database
42-
for (Letter letter : letterDao.readAll()) {
43-
letter.setUsageCount(frequencyMap.getOrDefault(letter.getId(), 0));
44-
letterDao.update(letter);
44+
// Update the values previously stored in the database
45+
for (Letter letter : letterDao.readAll()) {
46+
letter.setUsageCount(frequencyMap.getOrDefault(letter.getId(), 0));
47+
letterDao.update(letter);
48+
}
49+
} catch (Exception e) {
50+
log.error("Error in scheduled task:", e);
51+
DiscordHelper.postToChannel(Channel.CONTENT, "Error in `" + e.getClass() + ": " + e.getMessage() + "`");
4552
}
4653

4754
log.info("execute complete");

src/main/java/ai/elimu/tasks/ParagraphWordScheduler.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import ai.elimu.model.v2.enums.Language;
88
import ai.elimu.rest.v2.service.StoryBooksJsonService;
99
import ai.elimu.util.ConfigHelper;
10+
import ai.elimu.util.DiscordHelper;
11+
import ai.elimu.util.DiscordHelper.Channel;
1012
import ai.elimu.util.WordExtractionHelper;
1113
import java.util.ArrayList;
1214
import java.util.List;
@@ -33,36 +35,41 @@ public class ParagraphWordScheduler {
3335
public synchronized void execute() {
3436
log.info("execute");
3537

36-
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
38+
try {
39+
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
3740

38-
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll();
39-
log.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
40-
for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
41-
log.info("storyBookParagraph.getId(): " + storyBookParagraph.getId());
41+
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll();
42+
log.info("storyBookParagraphs.size(): " + storyBookParagraphs.size());
43+
for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
44+
log.info("storyBookParagraph.getId(): " + storyBookParagraph.getId());
4245

43-
List<String> wordsInOriginalText = WordExtractionHelper.getWords(storyBookParagraph.getOriginalText(), language);
44-
log.info("wordsInOriginalText.size(): " + wordsInOriginalText.size());
46+
List<String> wordsInOriginalText = WordExtractionHelper.getWords(storyBookParagraph.getOriginalText(), language);
47+
log.info("wordsInOriginalText.size(): " + wordsInOriginalText.size());
4548

46-
// Look for matches of existing Words in the paragraph's original text
47-
List<Word> words = new ArrayList<>();
48-
for (String wordInOriginalText : wordsInOriginalText) {
49-
log.debug("wordInOriginalText: \"" + wordInOriginalText + "\"");
50-
wordInOriginalText = wordInOriginalText.toLowerCase();
51-
log.debug("wordInOriginalText (lower-case): \"" + wordInOriginalText + "\"");
52-
Word word = wordDao.readByText(wordInOriginalText);
53-
log.debug("word: " + word);
54-
words.add(word);
49+
// Look for matches of existing Words in the paragraph's original text
50+
List<Word> words = new ArrayList<>();
51+
for (String wordInOriginalText : wordsInOriginalText) {
52+
log.debug("wordInOriginalText: \"" + wordInOriginalText + "\"");
53+
wordInOriginalText = wordInOriginalText.toLowerCase();
54+
log.debug("wordInOriginalText (lower-case): \"" + wordInOriginalText + "\"");
55+
Word word = wordDao.readByText(wordInOriginalText);
56+
log.debug("word: " + word);
57+
words.add(word);
58+
}
59+
log.info("words.size(): " + words.size());
60+
storyBookParagraph.setWords(words);
61+
62+
// Update the paragraph's list of Words in the database
63+
storyBookParagraphDao.update(storyBookParagraph);
5564
}
56-
log.info("words.size(): " + words.size());
57-
storyBookParagraph.setWords(words);
5865

59-
// Update the paragraph's list of Words in the database
60-
storyBookParagraphDao.update(storyBookParagraph);
66+
// Refresh REST API cache
67+
storyBooksJsonService.refreshStoryBooksJSONArray();
68+
} catch (Exception e) {
69+
log.error("Error in scheduled task:", e);
70+
DiscordHelper.postToChannel(Channel.CONTENT, "Error in `" + e.getClass() + ": " + e.getMessage() + "`");
6171
}
6272

63-
// Refresh REST API cache
64-
storyBooksJsonService.refreshStoryBooksJSONArray();
65-
6673
log.info("execute complete");
6774
}
6875
}

src/main/java/ai/elimu/tasks/SoundUsageCountScheduler.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import ai.elimu.dao.SoundDao;
55
import ai.elimu.entity.content.LetterSound;
66
import ai.elimu.entity.content.Sound;
7+
import ai.elimu.util.DiscordHelper;
8+
import ai.elimu.util.DiscordHelper.Channel;
9+
710
import java.util.HashMap;
811
import java.util.Map;
912
import lombok.RequiredArgsConstructor;
@@ -27,20 +30,25 @@ public class SoundUsageCountScheduler {
2730
public synchronized void execute() {
2831
log.info("execute");
2932

30-
// <ID, frequency>
31-
Map<Long, Integer> frequencyMap = new HashMap<>();
33+
try {
34+
// <ID, frequency>
35+
Map<Long, Integer> frequencyMap = new HashMap<>();
3236

33-
// Calculate the frequency of each sound
34-
for (LetterSound letterSound : letterSoundDao.readAll()) {
35-
for (Sound sound : letterSound.getSounds()) {
36-
frequencyMap.put(sound.getId(), frequencyMap.getOrDefault(sound.getId(), 0) + letterSound.getUsageCount());
37+
// Calculate the frequency of each sound
38+
for (LetterSound letterSound : letterSoundDao.readAll()) {
39+
for (Sound sound : letterSound.getSounds()) {
40+
frequencyMap.put(sound.getId(), frequencyMap.getOrDefault(sound.getId(), 0) + letterSound.getUsageCount());
41+
}
3742
}
38-
}
3943

40-
// Update the values previously stored in the database
41-
for (Sound sound : soundDao.readAll()) {
42-
sound.setUsageCount(frequencyMap.getOrDefault(sound.getId(), 0));
43-
soundDao.update(sound);
44+
// Update the values previously stored in the database
45+
for (Sound sound : soundDao.readAll()) {
46+
sound.setUsageCount(frequencyMap.getOrDefault(sound.getId(), 0));
47+
soundDao.update(sound);
48+
}
49+
} catch (Exception e) {
50+
log.error("Error in scheduled task:", e);
51+
DiscordHelper.postToChannel(Channel.CONTENT, "Error in `" + e.getClass() + ": " + e.getMessage() + "`");
4452
}
4553

4654
log.info("execute complete");

src/main/java/ai/elimu/tasks/SyllableUsageCountScheduler.java

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
import ai.elimu.entity.content.Word;
1313
import ai.elimu.model.v2.enums.Language;
1414
import ai.elimu.util.ConfigHelper;
15+
import ai.elimu.util.DiscordHelper;
16+
import ai.elimu.util.DiscordHelper.Channel;
1517
import ai.elimu.util.SyllableFrequencyHelper;
1618
import java.util.ArrayList;
17-
import java.util.Calendar;
1819
import java.util.HashMap;
1920
import java.util.List;
2021
import java.util.Map;
@@ -45,57 +46,61 @@ public class SyllableUsageCountScheduler {
4546
public synchronized void execute() {
4647
log.info("execute");
4748

48-
log.info("Calculating usage count for Syllables");
49+
try {
50+
// <text, frequency>
51+
Map<String, Integer> syllableFrequencyMap = new HashMap<>();
4952

50-
Map<String, Integer> syllableFrequencyMap = new HashMap<>();
53+
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
5154

52-
Language language = Language.valueOf(ConfigHelper.getProperty("content.language"));
55+
List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
56+
log.info("storyBooks.size(): " + storyBooks.size());
57+
for (StoryBook storyBook : storyBooks) {
58+
log.debug("storyBook.getTitle(): " + storyBook.getTitle());
5359

54-
List<StoryBook> storyBooks = storyBookDao.readAllOrdered();
55-
log.info("storyBooks.size(): " + storyBooks.size());
56-
for (StoryBook storyBook : storyBooks) {
57-
log.debug("storyBook.getTitle(): " + storyBook.getTitle());
58-
59-
List<String> paragraphs = new ArrayList<>();
60-
List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
61-
for (StoryBookChapter storyBookChapter : storyBookChapters) {
62-
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
63-
for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
64-
paragraphs.add(storyBookParagraph.getOriginalText());
60+
List<String> paragraphs = new ArrayList<>();
61+
List<StoryBookChapter> storyBookChapters = storyBookChapterDao.readAll(storyBook);
62+
for (StoryBookChapter storyBookChapter : storyBookChapters) {
63+
List<StoryBookParagraph> storyBookParagraphs = storyBookParagraphDao.readAll(storyBookChapter);
64+
for (StoryBookParagraph storyBookParagraph : storyBookParagraphs) {
65+
paragraphs.add(storyBookParagraph.getOriginalText());
66+
}
6567
}
66-
}
6768

68-
Map<String, Integer> syllableFrequencyMapForBook = SyllableFrequencyHelper.getSyllableFrequency(paragraphs, language);
69-
syllableFrequencyMapForBook.keySet()
70-
.forEach(syllableText -> syllableFrequencyMap.put(syllableText, syllableFrequencyMap.getOrDefault(syllableText, 0) + syllableFrequencyMapForBook.get(syllableText)));
71-
}
69+
Map<String, Integer> syllableFrequencyMapForBook = SyllableFrequencyHelper.getSyllableFrequency(paragraphs, language);
70+
syllableFrequencyMapForBook.keySet()
71+
.forEach(syllableText -> syllableFrequencyMap.put(syllableText, syllableFrequencyMap.getOrDefault(syllableText, 0) + syllableFrequencyMapForBook.get(syllableText)));
72+
}
7273

73-
log.info("syllableFrequencyMap: " + syllableFrequencyMap);
74+
log.info("syllableFrequencyMap: " + syllableFrequencyMap);
7475

75-
for (String syllableText : syllableFrequencyMap.keySet()) {
76-
// Skip syllables that are actual words
77-
// TODO: add logic to Word editing
78-
Word word = wordDao.readByText(syllableText);
79-
if (word != null) {
80-
continue;
81-
}
76+
for (String syllableText : syllableFrequencyMap.keySet()) {
77+
// Skip syllables that are actual words
78+
// TODO: add logic to Word editing
79+
Word word = wordDao.readByText(syllableText);
80+
if (word != null) {
81+
continue;
82+
}
8283

83-
// Skip syllables that are not digrams
84-
// TODO: add support for trigrams
85-
if (syllableText.length() != 2) {
86-
continue;
87-
}
84+
// Skip syllables that are not digrams
85+
// TODO: add support for trigrams
86+
if (syllableText.length() != 2) {
87+
continue;
88+
}
8889

89-
Syllable existingSyllable = syllableDao.readByText(syllableText);
90-
if (existingSyllable == null) {
91-
Syllable syllable = new Syllable();
92-
syllable.setText(syllableText);
93-
syllable.setUsageCount(syllableFrequencyMap.get(syllableText));
94-
syllableDao.create(syllable);
95-
} else {
96-
existingSyllable.setUsageCount(syllableFrequencyMap.get(syllableText));
97-
syllableDao.update(existingSyllable);
90+
Syllable existingSyllable = syllableDao.readByText(syllableText);
91+
if (existingSyllable == null) {
92+
Syllable syllable = new Syllable();
93+
syllable.setText(syllableText);
94+
syllable.setUsageCount(syllableFrequencyMap.get(syllableText));
95+
syllableDao.create(syllable);
96+
} else {
97+
existingSyllable.setUsageCount(syllableFrequencyMap.get(syllableText));
98+
syllableDao.update(existingSyllable);
99+
}
98100
}
101+
} catch (Exception e) {
102+
log.error("Error in scheduled task:", e);
103+
DiscordHelper.postToChannel(Channel.CONTENT, "Error in `" + e.getClass() + ": " + e.getMessage() + "`");
99104
}
100105

101106
log.info("execute complete");

src/main/java/ai/elimu/tasks/WordUsageCountScheduler.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import ai.elimu.dao.WordDao;
55
import ai.elimu.entity.content.StoryBookParagraph;
66
import ai.elimu.entity.content.Word;
7+
import ai.elimu.util.DiscordHelper;
8+
import ai.elimu.util.DiscordHelper.Channel;
79

810
import java.util.HashMap;
911
import java.util.Map;
@@ -29,20 +31,25 @@ public class WordUsageCountScheduler {
2931
public synchronized void execute() {
3032
log.info("execute");
3133

32-
// <ID, frequency>
33-
Map<Long, Integer> frequencyMap = new HashMap<>();
34+
try {
35+
// <ID, frequency>
36+
Map<Long, Integer> frequencyMap = new HashMap<>();
3437

35-
// Calculate the frequency of each word
36-
for (StoryBookParagraph storyBookParagraph : storyBookParagraphDao.readAll()) {
37-
for (Word word : storyBookParagraph.getWords()) {
38-
frequencyMap.put(word.getId(), frequencyMap.getOrDefault(word.getId(), 0) + 1);
38+
// Calculate the frequency of each word
39+
for (StoryBookParagraph storyBookParagraph : storyBookParagraphDao.readAll()) {
40+
for (Word word : storyBookParagraph.getWords()) {
41+
frequencyMap.put(word.getId(), frequencyMap.getOrDefault(word.getId(), 0) + 1);
42+
}
3943
}
40-
}
4144

42-
// Update the values previously stored in the database
43-
for (Word word : wordDao.readAll()) {
44-
word.setUsageCount(frequencyMap.getOrDefault(word.getId(), 0));
45-
wordDao.update(word);
45+
// Update the values previously stored in the database
46+
for (Word word : wordDao.readAll()) {
47+
word.setUsageCount(frequencyMap.getOrDefault(word.getId(), 0));
48+
wordDao.update(word);
49+
}
50+
} catch (Exception e) {
51+
log.error("Error in scheduled task:", e);
52+
DiscordHelper.postToChannel(Channel.CONTENT, "Error in `" + e.getClass() + ": " + e.getMessage() + "`");
4653
}
4754

4855
log.info("execute complete");

0 commit comments

Comments
 (0)