Skip to content

Commit

Permalink
app: update all users every 5 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
PoustouFlan committed Aug 5, 2024
1 parent 517b03d commit d5af76a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/cryptodrink/data/model/UserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Data;

import javax.persistence.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -31,5 +32,5 @@ public class UserModel {
@ManyToMany(mappedBy = "users")
private List<ScoreboardModel> scoreboards = new ArrayList<>();

private LocalDateTime lastRefreshed;
private Instant lastRefreshed;
}
3 changes: 2 additions & 1 deletion src/main/java/org/cryptodrink/domain/entity/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

Expand All @@ -18,5 +19,5 @@ public class UserEntity {
private Integer rank;
private Integer score;
private String website;
private LocalDateTime lastRefreshed;
private Instant lastRefreshed;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.cryptodrink.domain.service;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class ApplicationService {

@Inject
UserService userService;

@PostConstruct
public void onStart() {
userService.startScheduledUpdates();
}
}

24 changes: 24 additions & 0 deletions src/main/java/org/cryptodrink/domain/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@ApplicationScoped
public class UserService {
Expand Down Expand Up @@ -118,4 +121,25 @@ public UserEntity getUserFromToken(String token) {
return null;
}
}

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public void startScheduledUpdates() {
scheduler.scheduleAtFixedRate(this::updateAllUsers, 0, 5, TimeUnit.MINUTES);
}

private void updateAllUsers() {
logger.info("Starting user updates...");

users.listAll().forEach(user -> {
scheduler.submit(() -> {
try {
cryptoHack.updateUserInfo(user.getUsername());
logger.info("Successfully updated user info for {}", user.getUsername());
} catch (Exception e) {
logger.error("Failed to update user info for {}", user.getUsername(), e);
}
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -127,7 +128,7 @@ public void saveUserInfo(CryptoHackResponse userInfo) {
user.setRank(userInfo.getRank());
user.setScore(userInfo.getScore());
user.setWebsite(userInfo.getWebsite());
user.setLastRefreshed(LocalDateTime.now());
user.setLastRefreshed(Instant.now());
users.persist(user);

List<SolvedChallengeModel> solvedChallenges = user.getSolvedChallenges();
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/cryptodrink/utils/TimeString.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.cryptodrink.utils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.*;
import java.time.format.DateTimeFormatter;

public class TimeString {
Expand All @@ -14,4 +12,9 @@ public static String convertToUTCString(LocalDateTime localDateTime) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
return zonedDateTime.format(formatter);
}

public static String convertToUTCString(Instant instant) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
return formatter.withZone(ZoneId.of("UTC")).format(instant);
}
}

0 comments on commit d5af76a

Please sign in to comment.