Commit 3c5a1c64 authored by Adam Gausmann's avatar Adam Gausmann

1.1.2 patch: Add delay between execution periods to throttle CPU usage.

parent 2183f10b
Pipeline #5442620 passed with stages
in 25 seconds
......@@ -5,7 +5,7 @@
<parent>
<artifactId>samurai</artifactId>
<groupId>ninja.nonemu</groupId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -6,7 +6,7 @@
<groupId>ninja.nonemu</groupId>
<artifactId>samurai</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<modules>
<module>api</module>
<module>runtime</module>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>samurai</artifactId>
<groupId>ninja.nonemu</groupId>
<version>1.1.1</version>
<version>1.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -19,6 +19,7 @@ import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
public class BotImpl implements Bot {
private static final long EXECUTION_INTERVAL_MS = 20;
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
......@@ -31,12 +32,27 @@ public class BotImpl implements Bot {
}
BotImpl bot = new BotImpl();
Logger logger = bot.getLogger();
bot.init();
try {
long lastTime = System.currentTimeMillis();
while (bot.isRunning()) {
bot.run();
long time = System.currentTimeMillis();
long leftover = EXECUTION_INTERVAL_MS - (time - lastTime);
if (leftover < 0) {
logger.debug("Running {}ms overtime (interval period is {}ms)", -leftover, EXECUTION_INTERVAL_MS);
} else {
try {
Thread.sleep(leftover);
} catch (InterruptedException e) {
logger.error("Sleep interrupted", e);
}
}
lastTime = time;
}
} finally {
bot.cleanup();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment