aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarko Zajc <marko@zajc.eu.org>2023-02-20 18:49:43 +0100
committerMarko Zajc <marko@zajc.eu.org>2023-02-20 19:12:04 +0100
commit579458ee874ee19b72282c653c176c4454f450a0 (patch)
tree2e3f4300e67c2a3c8efea0816af2322c3d1c9380
parent389a2e7f1d84f825242edee7282fa574ccd49069 (diff)
Update exchange rates as a background job (#2)
-rw-r--r--README.txt1
-rw-r--r--pom.xml4
-rw-r--r--src/main/java/libot/listeners/CalculatorRateUpdaterListener.java43
3 files changed, 9 insertions, 39 deletions
diff --git a/README.txt b/README.txt
index f09fa57..7764cfe 100644
--- a/README.txt
+++ b/README.txt
@@ -9,6 +9,7 @@ Module data:
9 9
10 Listeners -------------------------- 10 Listeners --------------------------
11 - PollListener 11 - PollListener
12 - CalculatorRateUpdaterListener
12 13
13 Commands --------------------------- 14 Commands ---------------------------
14 - CalculatorCommand 15 - CalculatorCommand
diff --git a/pom.xml b/pom.xml
index e1c64cf..7d7067d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,6 +31,7 @@
31 </scm> 31 </scm>
32 32
33 <dependencies> 33 <dependencies>
34
34 <!-- Cross-module --> 35 <!-- Cross-module -->
35 <dependency> 36 <dependency>
36 <groupId>zajc.libot</groupId> 37 <groupId>zajc.libot</groupId>
@@ -47,12 +48,13 @@
47 <artifactId>chatter-bot-api</artifactId> 48 <artifactId>chatter-bot-api</artifactId>
48 <version>2.0.1</version> 49 <version>2.0.1</version>
49 </dependency> 50 </dependency>
50 51
51 <!-- Annotations --> 52 <!-- Annotations -->
52 <dependency> 53 <dependency>
53 <groupId>com.github.spotbugs</groupId> 54 <groupId>com.github.spotbugs</groupId>
54 <artifactId>spotbugs-annotations</artifactId> 55 <artifactId>spotbugs-annotations</artifactId>
55 </dependency> 56 </dependency>
57
56 </dependencies> 58 </dependencies>
57 59
58 <build> 60 <build>
diff --git a/src/main/java/libot/listeners/CalculatorRateUpdaterListener.java b/src/main/java/libot/listeners/CalculatorRateUpdaterListener.java
index 7dfd339..cf051ca 100644
--- a/src/main/java/libot/listeners/CalculatorRateUpdaterListener.java
+++ b/src/main/java/libot/listeners/CalculatorRateUpdaterListener.java
@@ -1,64 +1,31 @@
1package libot.listeners; 1package libot.listeners;
2 2
3import static com.github.markozajc.ef.EHandle.handle; 3import static com.github.markozajc.ef.EHandle.handle;
4import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
5import static java.util.concurrent.TimeUnit.*; 4import static java.util.concurrent.TimeUnit.*;
6import static org.slf4j.LoggerFactory.getLogger; 5import static org.slf4j.LoggerFactory.getLogger;
7 6
8import java.io.IOException; 7import java.io.IOException;
9import java.util.concurrent.ScheduledExecutorService;
10 8
11import org.slf4j.Logger; 9import org.slf4j.Logger;
12 10
13import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14import libot.commands.CalculatorCommand; 11import libot.commands.CalculatorCommand;
15import net.dv8tion.jda.api.events.*; 12import libot.core.entities.BotContext;
16import net.dv8tion.jda.api.hooks.ListenerAdapter;
17 13
18public class CalculatorRateUpdaterListener extends ListenerAdapter { 14public class CalculatorRateUpdaterListener implements BotEventListener {
19 15
20 private static final Logger LOG = getLogger(CalculatorRateUpdaterListener.class); 16 private static final Logger LOG = getLogger(CalculatorRateUpdaterListener.class);
21
22 private static final long UPDATE_INTERVAL = HOURS.toMillis(1); 17 private static final long UPDATE_INTERVAL = HOURS.toMillis(1);
23 private static final long UPDATE_TIMEOUT = MINUTES.toMillis(5); 18 private static final long UPDATE_TIMEOUT = MINUTES.toMillis(5);
24 private static final ScheduledExecutorService UPDATER;
25 private static boolean updaterRunning;
26
27 static {
28 if (CalculatorCommand.ENABLED)
29 UPDATER = newSingleThreadScheduledExecutor();
30 else
31 UPDATER = null;
32 }
33 19
34 @Override 20 @Override
35 @SuppressFBWarnings(value = "JLM_JSR166_UTILCONCURRENT_MONITORENTER", justification = "false positive") 21 public void onStartup(BotContext bot) {
36 public void onReady(ReadyEvent event) { 22 if (CalculatorCommand.ENABLED) {
37 if (UPDATER == null) 23 bot.cron().scheduleAtFixedRate(handle(CalculatorRateUpdaterListener::updateRates, t -> {
38 return;
39
40 synchronized (UPDATER) {
41 if (updaterRunning)
42 return;
43 updaterRunning = true;
44
45 UPDATER.scheduleAtFixedRate(handle(CalculatorRateUpdaterListener::updateRates, t -> {
46 LOG.error("Failed to update exchange rates", t); 24 LOG.error("Failed to update exchange rates", t);
47 }), 0, UPDATE_INTERVAL, MILLISECONDS); 25 }), 0, UPDATE_INTERVAL, MILLISECONDS);
48 } 26 }
49 } 27 }
50 28
51 @Override
52 @SuppressFBWarnings(value = "JLM_JSR166_UTILCONCURRENT_MONITORENTER", justification = "false positive")
53 public void onShutdown(ShutdownEvent event) {
54 if (UPDATER == null)
55 return;
56
57 synchronized (UPDATER) {
58 UPDATER.shutdown();
59 }
60 }
61
62 private static void updateRates() throws IOException, InterruptedException { 29 private static void updateRates() throws IOException, InterruptedException {
63 LOG.debug("Updating exchange rates"); 30 LOG.debug("Updating exchange rates");
64 31