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