mirror of
https://github.com/demodude4u/Factorio-FBSR.git
synced 2025-02-19 19:59:54 +02:00
Resolved #62
This commit is contained in:
parent
8165b9f373
commit
504a878c67
@ -13,5 +13,6 @@
|
|||||||
"refresh_seconds": 20,
|
"refresh_seconds": 20,
|
||||||
"age_limit_hours": 24
|
"age_limit_hours": 24
|
||||||
},
|
},
|
||||||
|
"watchdog_interval_minutes": 1,
|
||||||
"factorio": "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Factorio"
|
"factorio": "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Factorio"
|
||||||
}
|
}
|
@ -44,6 +44,7 @@ import com.demod.fbsr.TaskReporting;
|
|||||||
import com.demod.fbsr.TaskReporting.Level;
|
import com.demod.fbsr.TaskReporting.Level;
|
||||||
import com.demod.fbsr.WebUtils;
|
import com.demod.fbsr.WebUtils;
|
||||||
import com.demod.fbsr.WorldMap;
|
import com.demod.fbsr.WorldMap;
|
||||||
|
import com.demod.fbsr.app.WatchdogService.WatchdogReporter;
|
||||||
import com.google.common.collect.LinkedHashMultiset;
|
import com.google.common.collect.LinkedHashMultiset;
|
||||||
import com.google.common.collect.Multiset;
|
import com.google.common.collect.Multiset;
|
||||||
import com.google.common.util.concurrent.AbstractIdleService;
|
import com.google.common.util.concurrent.AbstractIdleService;
|
||||||
@ -420,6 +421,7 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
|
|||||||
@Override
|
@Override
|
||||||
protected void shutDown() throws Exception {
|
protected void shutDown() throws Exception {
|
||||||
ServiceFinder.removeService(this);
|
ServiceFinder.removeService(this);
|
||||||
|
ServiceFinder.removeService(WatchdogReporter.class);
|
||||||
bot.stopAsync().awaitTerminated();
|
bot.stopAsync().awaitTerminated();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -479,6 +481,21 @@ public class BlueprintBotDiscordService extends AbstractIdleService {
|
|||||||
reportingUserID = Config.get().getString("discord_reporting_user_id");
|
reportingUserID = Config.get().getString("discord_reporting_user_id");
|
||||||
|
|
||||||
ServiceFinder.addService(this);
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -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!");
|
" There was a problem completing your request. I have contacted my programmer to fix it for you!");
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceFinder.findService(BlueprintBotDiscordService.class).ifPresent(
|
ServiceFinder.findService(BlueprintBotDiscordService.class)
|
||||||
s -> s.sendReport("Reddit / " + subreddit + " / " + author, REDDIT_AUTHOR_URL, reporting));
|
.ifPresent(s -> s.sendReport("Reddit / " + subreddit + " / " + author, REDDIT_AUTHOR_URL, reporting));
|
||||||
|
|
||||||
return Optional.of(lines.stream().collect(Collectors.joining("\n\n")));
|
return Optional.of(lines.stream().collect(Collectors.joining("\n\n")));
|
||||||
}
|
}
|
||||||
@ -317,6 +317,10 @@ public class BlueprintBotRedditService extends AbstractScheduledService {
|
|||||||
if (cacheUpdated) {
|
if (cacheUpdated) {
|
||||||
saveCache(cacheJson);
|
saveCache(cacheJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServiceFinder.findService(WatchdogService.class).ifPresent(watchdog -> {
|
||||||
|
watchdog.notifyActive("Reddit Bot");
|
||||||
|
});
|
||||||
} catch (NetworkException e) {
|
} catch (NetworkException e) {
|
||||||
System.out.println("Network Problem [" + e.getClass().getSimpleName() + "]: " + e.getMessage());
|
System.out.println("Network Problem [" + e.getClass().getSimpleName() + "]: " + e.getMessage());
|
||||||
authExpireMillis = 0;
|
authExpireMillis = 0;
|
||||||
|
@ -7,6 +7,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
public class ServiceFinder {
|
public class ServiceFinder {
|
||||||
private static final Map<Class<?>, Object> registry = new ConcurrentHashMap<>();
|
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) {
|
public static void addService(Object service) {
|
||||||
registry.put(service.getClass(), service);
|
registry.put(service.getClass(), service);
|
||||||
}
|
}
|
||||||
@ -16,7 +20,11 @@ public class ServiceFinder {
|
|||||||
return Optional.ofNullable((T) registry.get(clazz));
|
return Optional.ofNullable((T) registry.get(clazz));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void removeService(Class<?> clazz) {
|
||||||
|
registry.remove(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
public static void removeService(Object service) {
|
public static void removeService(Object service) {
|
||||||
registry.remove(service.getClass());
|
removeService(service.getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,8 @@ public class StartAllServices {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ServiceManager manager = new ServiceManager(Arrays.asList(new Service[] { //
|
ServiceManager manager = new ServiceManager(Arrays.asList(new Service[] { //
|
||||||
new BlueprintBotDiscordService(), //
|
new BlueprintBotDiscordService(), //
|
||||||
new BlueprintBotRedditService(),//
|
new BlueprintBotRedditService(), //
|
||||||
|
new WatchdogService(),//
|
||||||
}));
|
}));
|
||||||
|
|
||||||
manager.startAsync();
|
manager.startAsync();
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user