1
0
mirror of https://github.com/demodude4u/Factorio-FBSR.git synced 2025-02-19 19:59:54 +02:00
This commit is contained in:
Weston Rye (Demod) 2017-06-22 01:05:13 -04:00
parent 8165b9f373
commit 504a878c67
6 changed files with 96 additions and 4 deletions

View File

@ -13,5 +13,6 @@
"refresh_seconds": 20,
"age_limit_hours": 24
},
"watchdog_interval_minutes": 1,
"factorio": "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Factorio"
}

View File

@ -44,6 +44,7 @@ import com.demod.fbsr.TaskReporting;
import com.demod.fbsr.TaskReporting.Level;
import com.demod.fbsr.WebUtils;
import com.demod.fbsr.WorldMap;
import com.demod.fbsr.app.WatchdogService.WatchdogReporter;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.util.concurrent.AbstractIdleService;
@ -420,6 +421,7 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
@Override
protected void shutDown() throws Exception {
ServiceFinder.removeService(this);
ServiceFinder.removeService(WatchdogReporter.class);
bot.stopAsync().awaitTerminated();
}
@ -479,6 +481,21 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
reportingUserID = Config.get().getString("discord_reporting_user_id");
ServiceFinder.addService(this);
ServiceFinder.addService(WatchdogReporter.class, new WatchdogReporter() {
@Override
public void notifyInactive(String label) {
TaskReporting reporting = new TaskReporting();
reporting.addWarning(label + " has gone inactive!");
sendReport("Watchdog", null, reporting);
}
@Override
public void notifyReactive(String label) {
TaskReporting reporting = new TaskReporting();
reporting.addInfo(label + " is now active again!");
sendReport("Watchdog", null, reporting);
}
});
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -163,8 +163,8 @@ public class BlueprintBotRedditService extends AbstractScheduledService {
" There was a problem completing your request. I have contacted my programmer to fix it for you!");
}
ServiceFinder.findService(BlueprintBotDiscordService.class).ifPresent(
s -> s.sendReport("Reddit / " + subreddit + " / " + author, REDDIT_AUTHOR_URL, reporting));
ServiceFinder.findService(BlueprintBotDiscordService.class)
.ifPresent(s -> s.sendReport("Reddit / " + subreddit + " / " + author, REDDIT_AUTHOR_URL, reporting));
return Optional.of(lines.stream().collect(Collectors.joining("\n\n")));
}
@ -317,6 +317,10 @@ public class BlueprintBotRedditService extends AbstractScheduledService {
if (cacheUpdated) {
saveCache(cacheJson);
}
ServiceFinder.findService(WatchdogService.class).ifPresent(watchdog -> {
watchdog.notifyActive("Reddit Bot");
});
} catch (NetworkException e) {
System.out.println("Network Problem [" + e.getClass().getSimpleName() + "]: " + e.getMessage());
authExpireMillis = 0;

View File

@ -7,6 +7,10 @@ import java.util.concurrent.ConcurrentHashMap;
public class ServiceFinder {
private static final Map<Class<?>, Object> registry = new ConcurrentHashMap<>();
public static <T, S extends T> void addService(Class<T> clazz, S service) {
registry.put(clazz, service);
}
public static void addService(Object service) {
registry.put(service.getClass(), service);
}
@ -16,7 +20,11 @@ public class ServiceFinder {
return Optional.ofNullable((T) registry.get(clazz));
}
public static void removeService(Class<?> clazz) {
registry.remove(clazz);
}
public static void removeService(Object service) {
registry.remove(service.getClass());
removeService(service.getClass());
}
}

View File

@ -10,7 +10,8 @@ public class StartAllServices {
public static void main(String[] args) {
ServiceManager manager = new ServiceManager(Arrays.asList(new Service[] { //
new BlueprintBotDiscordService(), //
new BlueprintBotRedditService(),//
new BlueprintBotRedditService(), //
new WatchdogService(),//
}));
manager.startAsync();

View File

@ -0,0 +1,61 @@
package com.demod.fbsr.app;
import java.util.LinkedHashSet;
import java.util.concurrent.TimeUnit;
import com.demod.factorio.Config;
import com.google.common.util.concurrent.AbstractScheduledService;
public class WatchdogService extends AbstractScheduledService {
public static interface WatchdogReporter {
public void notifyInactive(String label);
public void notifyReactive(String label);
}
private final LinkedHashSet<String> known = new LinkedHashSet<>();
private final LinkedHashSet<String> active = new LinkedHashSet<>();
private final LinkedHashSet<String> alarmed = new LinkedHashSet<>();
public synchronized void notifyActive(String label) {
known.add(label);
active.add(label);
if (alarmed.remove(label)) {
System.out.println("WATCHDOG: " + label + " is now active again!");
ServiceFinder.findService(WatchdogReporter.class).ifPresent(reporter -> {
reporter.notifyReactive(label);
});
}
}
@Override
protected synchronized void runOneIteration() throws Exception {
for (String label : known) {
if (!active.contains(label) && !alarmed.contains(label)) {
alarmed.add(label);
System.out.println("WATCHDOG: " + label + " has gone inactive!");
ServiceFinder.findService(WatchdogReporter.class).ifPresent(reporter -> {
reporter.notifyInactive(label);
});
}
}
active.clear();
}
@Override
protected Scheduler scheduler() {
return Scheduler.newFixedDelaySchedule(0, Config.get().getInt("watchdog_interval_minutes"), TimeUnit.MINUTES);
}
@Override
protected void shutDown() throws Exception {
ServiceFinder.removeService(this);
}
@Override
protected void startUp() throws Exception {
ServiceFinder.addService(this);
}
}