mirror of
https://github.com/imgfloat/server.git
synced 2026-02-04 19:29:26 +00:00
Add emote sync
This commit is contained in:
@@ -43,6 +43,9 @@ public class Settings {
|
||||
@Column(nullable = false)
|
||||
private int emoteSyncIntervalMinutes;
|
||||
|
||||
@Column(name = "last_emote_sync_at")
|
||||
private Instant lastEmoteSyncAt;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
@@ -62,6 +65,7 @@ public class Settings {
|
||||
s.setMaxCanvasSideLengthPixels(7680);
|
||||
s.setCanvasFramesPerSecond(60);
|
||||
s.setEmoteSyncIntervalMinutes(60);
|
||||
s.setLastEmoteSyncAt(null);
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -145,6 +149,14 @@ public class Settings {
|
||||
this.emoteSyncIntervalMinutes = emoteSyncIntervalMinutes;
|
||||
}
|
||||
|
||||
public Instant getLastEmoteSyncAt() {
|
||||
return lastEmoteSyncAt;
|
||||
}
|
||||
|
||||
public void setLastEmoteSyncAt(Instant lastEmoteSyncAt) {
|
||||
this.lastEmoteSyncAt = lastEmoteSyncAt;
|
||||
}
|
||||
|
||||
@PrePersist
|
||||
public void initializeTimestamps() {
|
||||
Instant now = Instant.now();
|
||||
|
||||
@@ -50,14 +50,22 @@ public class EmoteSyncScheduler implements SchedulingConfigurer {
|
||||
|
||||
private Trigger buildTrigger() {
|
||||
return (TriggerContext triggerContext) -> {
|
||||
Instant lastCompletion = triggerContext.lastCompletion() == null
|
||||
? Instant.now()
|
||||
: triggerContext.lastCompletion();
|
||||
assert lastCompletion != null;
|
||||
return lastCompletion.plus(Duration.ofMinutes(resolveIntervalMinutes()));
|
||||
int interval = resolveIntervalMinutes();
|
||||
Instant lastCompletion = resolveLastCompletion(triggerContext, interval);
|
||||
return lastCompletion.plus(Duration.ofMinutes(interval));
|
||||
};
|
||||
}
|
||||
|
||||
private Instant resolveLastCompletion(TriggerContext triggerContext, int intervalMinutes) {
|
||||
Instant lastCompletion = triggerContext.lastCompletion();
|
||||
if (lastCompletion != null) {
|
||||
return lastCompletion;
|
||||
}
|
||||
Settings settings = settingsService.get();
|
||||
Instant persisted = settings.getLastEmoteSyncAt();
|
||||
return persisted != null ? persisted : Instant.now().minus(Duration.ofMinutes(intervalMinutes));
|
||||
}
|
||||
|
||||
private int resolveIntervalMinutes() {
|
||||
Settings settings = settingsService.get();
|
||||
int interval = settings.getEmoteSyncIntervalMinutes();
|
||||
@@ -67,15 +75,20 @@ public class EmoteSyncScheduler implements SchedulingConfigurer {
|
||||
private void syncEmotes() {
|
||||
int interval = resolveIntervalMinutes();
|
||||
LOG.info("Synchronizing emotes (interval {} minutes)", interval);
|
||||
|
||||
twitchEmoteService.refreshGlobalEmotes();
|
||||
List<Channel> channels = channelRepository.findAll();
|
||||
for (Channel channel : channels) {
|
||||
String broadcaster = channel.getBroadcaster();
|
||||
twitchEmoteService.refreshChannelEmotes(broadcaster);
|
||||
sevenTvEmoteService.refreshChannelEmotes(broadcaster);
|
||||
List<Channel> channels = List.of();
|
||||
try {
|
||||
channels = channelRepository.findAll();
|
||||
twitchEmoteService.refreshGlobalEmotes();
|
||||
for (Channel channel : channels) {
|
||||
String broadcaster = channel.getBroadcaster();
|
||||
twitchEmoteService.refreshChannelEmotes(broadcaster);
|
||||
sevenTvEmoteService.refreshChannelEmotes(broadcaster);
|
||||
}
|
||||
LOG.info("Completed emote sync for {} channels", channels.size());
|
||||
} catch (Exception ex) {
|
||||
LOG.error("Emote sync failed", ex);
|
||||
} finally {
|
||||
settingsService.updateLastEmoteSyncAt(Instant.now());
|
||||
}
|
||||
|
||||
LOG.info("Completed emote sync for {} channels", channels.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import dev.kruhlmann.imgfloat.repository.AudioAssetRepository;
|
||||
import dev.kruhlmann.imgfloat.repository.SettingsRepository;
|
||||
import dev.kruhlmann.imgfloat.repository.VisualAssetRepository;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
@@ -59,6 +60,12 @@ public class SettingsService {
|
||||
return savedSettings;
|
||||
}
|
||||
|
||||
public void updateLastEmoteSyncAt(Instant timestamp) {
|
||||
Settings settings = get();
|
||||
settings.setLastEmoteSyncAt(timestamp);
|
||||
repo.save(settings);
|
||||
}
|
||||
|
||||
public void logSettings(String msg, Settings settings) {
|
||||
try {
|
||||
logger.info("{}:\n{}", msg, objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(settings));
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE settings ADD COLUMN last_emote_sync_at TIMESTAMP;
|
||||
Reference in New Issue
Block a user