Skip to content
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

Refactor: Modularization of Task Scheduling #194

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package de.tum.in.www1.hephaestus.leaderboard;

import de.tum.in.www1.hephaestus.leaderboard.tasks.SlackWeeklyLeaderboardTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronExpression;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

/**
* Schedules tasks to run at the end of every leaderboard cycle.
* @see SlackWeeklyLeaderboardTask
*/
@Order(value = Ordered.LOWEST_PRECEDENCE)
@EnableScheduling
@Service
public class LeaderboardTaskScheduler {

private static final Logger logger = LoggerFactory.getLogger(LeaderboardTaskScheduler.class);

@Value("${hephaestus.leaderboard.notification.enabled}")
private boolean runScheduledMessage;

@Value("${hephaestus.leaderboard.schedule.day}")
private String scheduledDay;

@Value("${hephaestus.leaderboard.schedule.time}")
private String scheduledTime;

@Autowired
private ThreadPoolTaskScheduler taskScheduler;

@Autowired
private SlackWeeklyLeaderboardTask slackWeeklyLeaderboardTask;


@EventListener(ApplicationReadyEvent.class)
public void activateTaskScheduler() {

var timeParts = scheduledTime.split(":");

// CRON for the end of every leaderboard cycle
String cron = String.format(
"0 %s %s ? * %s",
timeParts.length > 1 ? timeParts[1] : 0,
timeParts[0],
scheduledDay
);

if (!CronExpression.isValidExpression(cron)) {
logger.error("Invalid cron expression: " + cron);
return;
}

scheduleSlackMessage(cron);

}

/**
* Schedule a Slack message to be sent at the end of every leaderboard cycle.
*/
private void scheduleSlackMessage(String cron) {
if (!runScheduledMessage) return;

if (!slackWeeklyLeaderboardTask.testSlackConnection()) {
logger.error("Failed to schedule Slack message");
return;
}

logger.info("Scheduling Slack message to run with {}", cron);
taskScheduler.schedule(slackWeeklyLeaderboardTask, new CronTrigger(cron));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package de.tum.in.www1.hephaestus.leaderboard;

import static com.slack.api.model.block.Blocks.*;
import static com.slack.api.model.block.composition.BlockCompositions.*;

import com.slack.api.bolt.App;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
Expand All @@ -11,59 +8,37 @@
import com.slack.api.model.User;
import com.slack.api.model.block.LayoutBlock;
import java.io.IOException;
import java.time.DayOfWeek;
import java.time.OffsetDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.IntStream;
import org.apache.commons.text.similarity.LevenshteinDistance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronExpression;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

@Order(value = Ordered.LOWEST_PRECEDENCE)
@EnableScheduling
/**
* Light wrapper around the Slack App to send messages to the Slack workspace.
* @implNote Use the exposed method to test the Slack connection beforehand.
*/
@Service
public class SlackMessageService {

private static final Logger logger = LoggerFactory.getLogger(SlackMessageService.class);

@Value("${hephaestus.leaderboard.notification.channel-id}")
private String channelId;

@Value("${hephaestus.leaderboard.notification.enabled}")
private boolean runScheduledMessage;

@Value("${hephaestus.host-url:localhost:8080}")
private String hephaestusUrl;

@Value("${hephaestus.leaderboard.schedule.day}")
private String scheduledDay;

@Value("${hephaestus.leaderboard.schedule.time}")
private String scheduledTime;

@Autowired
private App slackApp;

@Autowired
private LeaderboardService leaderboardService;

@Autowired
private ThreadPoolTaskScheduler taskScheduler;
/**
* Gets all members of the Slack workspace.
* @return
*/
public List<User> getAllMembers() {
try {
return slackApp.client().usersList(r -> r).getMembers();
} catch (IOException | SlackApiException e) {
logger.error("Failed to get all members from Slack: " + e.getMessage());
return new ArrayList<>();
}
}

/**
* Sends a message to the specified Slack channel.
Expand All @@ -88,39 +63,11 @@ public void sendMessage(String channelId, List<LayoutBlock> blocks, String fallb
}
}

@EventListener(ApplicationReadyEvent.class)
public void activateTaskScheduler() {
if (!runScheduledMessage) {
return;
}

var timeParts = scheduledTime.split(":");

String cron = String.format(
"0 %s %s ? * %s",
timeParts.length > 1 ? timeParts[1] : 0,
timeParts[0],
scheduledDay
);

if (!CronExpression.isValidExpression(cron)) {
logger.error("Invalid cron expression: " + cron);
return;
}

logger.info("Scheduling Slack message to run with {}", cron);
taskScheduler.schedule(new SlackWeeklyLeaderboardTask(), new CronTrigger(cron));
}

/**
* Test the Slack app initialization on application startup.
* Tests if the Slack app is correctly initialized and has access to the workspace.
* Does not guarantee that the app has the necessary permissions to send messages.
*/
@EventListener(ApplicationReadyEvent.class)
public void run() {
if (!runScheduledMessage) {
logger.info("Slack scheduled messages are disabled, skipping Slack app init test.");
return;
}
public boolean initTest() {
logger.info("Testing Slack app initialization...");
AuthTestResponse response;
try {
Expand All @@ -132,132 +79,10 @@ public void run() {
}
if (response.isOk()) {
logger.info("Slack app is successfully initialized.");
return true;
} else {
logger.error("Failed to initialize Slack app: " + response.getError());
}
}

/**
* Task to send a weekly leaderboard message to the Slack channel.
* @see SlackMessageService#activateTaskScheduler()
*/
private class SlackWeeklyLeaderboardTask implements Runnable {

/**
* Gets the Slack handles of the top 3 reviewers in the given time frame.
* @return
*/
private List<User> getTop3SlackReviewers(OffsetDateTime after, OffsetDateTime before) {
var leaderboard = leaderboardService.createLeaderboard(after, before, Optional.empty());
var top3 = leaderboard.subList(0, Math.min(3, leaderboard.size()));
logger.debug("Top 3 Users of the last week: " + top3.stream().map(e -> e.user().name()).toList());

List<User> allSlackUsers;
try {
allSlackUsers = slackApp.client().usersList(r -> r).getMembers();
} catch (SlackApiException | IOException e) {
logger.error("Failed to get Slack users: " + e.getMessage());
return new ArrayList<>();
}

return top3.stream().map(mapToSlackUser(allSlackUsers)).filter(user -> user != null).toList();
}

private Function<LeaderboardEntryDTO, User> mapToSlackUser(List<User> allSlackUsers) {
return entry -> {
var exactUser = allSlackUsers
.stream()
.filter(
user ->
user.getName().equals(entry.user().name()) ||
(user.getProfile().getEmail() != null &&
user.getProfile().getEmail().equals(entry.user().email()))
)
.findFirst();
if (exactUser.isPresent()) {
return exactUser.get();
}

// find through String edit distance
return allSlackUsers
.stream()
.min((a, b) ->
Integer.compare(
LevenshteinDistance.getDefaultInstance().apply(entry.user().name(), a.getName()),
LevenshteinDistance.getDefaultInstance().apply(entry.user().name(), b.getName())
)
)
.orElse(null);
};
}

private String formatDateForURL(OffsetDateTime date) {
return date.format(java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME).replace("+", "%2B");
}

@Override
public void run() {
// get date in unix format
long currentDate = OffsetDateTime.now().toEpochSecond();
// Calculate the the last leaderboard schedule
String[] timeParts = scheduledTime.split(":");
OffsetDateTime before = OffsetDateTime.now()
.with(TemporalAdjusters.previousOrSame(DayOfWeek.of(Integer.parseInt(scheduledDay))))
.withHour(Integer.parseInt(timeParts[0]))
.withMinute(timeParts.length > 0 ? Integer.parseInt(timeParts[1]) : 0)
.withSecond(0)
.withNano(0);
OffsetDateTime after = before.minusWeeks(1);

var top3reviewers = getTop3SlackReviewers(after, before);

logger.info("Sending scheduled message to Slack channel...");

List<LayoutBlock> blocks = asBlocks(
header(header ->
header.text(plainText(pt -> pt.text(":newspaper: Reviews of the last week :newspaper:")))
),
context(context ->
context.elements(
List.of(
markdownText(
"<!date^" + currentDate + "^{date} at {time}| Today at 9:00AM CEST> | " + hephaestusUrl
)
)
)
),
divider(),
section(section ->
section.text(
markdownText(
"Another *review leaderboard* has concluded. You can check out your placement <" +
hephaestusUrl +
"?after=" +
formatDateForURL(after) +
"&before=" +
formatDateForURL(before) +
"|here>."
)
)
),
section(section -> section.text(markdownText("Special thanks to our top 3 reviewers of last week:"))),
section(section ->
section.text(
markdownText(
IntStream.range(0, top3reviewers.size())
.mapToObj(i -> ((i + 1) + ". <@" + top3reviewers.get(i).getId() + ">"))
.reduce((a, b) -> a + "\n" + b)
.orElse("")
)
)
),
section(section -> section.text(markdownText("Happy coding and reviewing! :rocket:")))
);
try {
sendMessage(channelId, blocks, "Reviews of the last week");
} catch (IOException | SlackApiException e) {
logger.error("Failed to send scheduled message to Slack channel: " + e.getMessage());
}
return false;
}
}
}
Loading
Loading