Skip to content

Commit

Permalink
fix: correctly stop and reboot main process
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep committed Aug 8, 2024
1 parent d2b4f45 commit 51dc45c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 50 deletions.
79 changes: 40 additions & 39 deletions src/main/java/bugs/stackoverflow/belisarius/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,55 @@ public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) throws ApiException {
// Initialise database
DatabaseUtils.createVandalisedPostTable();
DatabaseUtils.createBlacklistedWordCaughtTable();
DatabaseUtils.createOffensiveWordCaughtTable();
DatabaseUtils.createReasonCaughtTable();
DatabaseUtils.createFeedbackTable();
LOGGER.info("Initialised database!");

init();
}

public static void init() throws ApiException {
PropertyService propertyService = new PropertyService();

// Login
String email = propertyService.getProperty("email");
String password = propertyService.getProperty("password");

StackExchangeClient client = new StackExchangeClient(email, password);
LOGGER.info("Created a chatexchange client.");

// Initialise database
DatabaseUtils.createVandalisedPostTable();
DatabaseUtils.createBlacklistedWordCaughtTable();
DatabaseUtils.createOffensiveWordCaughtTable();
DatabaseUtils.createReasonCaughtTable();
DatabaseUtils.createFeedbackTable();
LOGGER.info("Initialised database!");
// Initialise Higgs
int higgsDashboardId = Integer.parseInt(propertyService.getProperty("higgsBotId"));
int roomId = Integer.parseInt(propertyService.getProperty("roomid"));

String higgsUrl = propertyService.getProperty("higgsUrl");
String useHiggs = propertyService.getProperty("useHiggs");
String secret = propertyService.getProperty("higgsSecret");

try {
// Initialise Higgs
int higgsDashboardId = Integer.parseInt(propertyService.getProperty("higgsBotId"));
int roomId = Integer.parseInt(propertyService.getProperty("roomid"));

String higgsUrl = propertyService.getProperty("higgsUrl");
String useHiggs = propertyService.getProperty("useHiggs");
String secret = propertyService.getProperty("higgsSecret");

if ("true".equals(useHiggs) && higgsDashboardId != 0) {
HiggsService.initInstance(higgsUrl, secret);
}
LOGGER.info("Initialised Higgs!");

// Initialise Redunda
String useRedunda = propertyService.getProperty("useRedunda");
String redundaKey = propertyService.getProperty("redundaKey");
String version = propertyService.getProperty("version");

PingService redunda = new PingService(redundaKey, version);
if ("false".equals(useRedunda)) {
redunda.setDebugging(true);
}
LOGGER.info("Initialised Redunda!");

String site = propertyService.getProperty("site");

MonitorService monitorService = new MonitorService(client, roomId, site, redunda);
monitorService.runMonitor();
LOGGER.info("Monitor started.");
} catch (NumberFormatException exception) {
LOGGER.error("Unable to access values from login.properties!", exception);
if ("true".equals(useHiggs) && higgsDashboardId != 0) {
HiggsService.initInstance(higgsUrl, secret);
}
LOGGER.info("Initialised Higgs!");

// Initialise Redunda
String useRedunda = propertyService.getProperty("useRedunda");
String redundaKey = propertyService.getProperty("redundaKey");
String version = propertyService.getProperty("version");

PingService redunda = new PingService(redundaKey, version);
if ("false".equals(useRedunda)) {
redunda.setDebugging(true);
}
LOGGER.info("Initialised Redunda!");

String site = propertyService.getProperty("site");

MonitorService monitorService = new MonitorService(client, roomId, site, redunda);
monitorService.runMonitor();
LOGGER.info("Monitor started.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import bugs.stackoverflow.belisarius.Application;
import bugs.stackoverflow.belisarius.Belisarius;
import bugs.stackoverflow.belisarius.clients.Monitor;
import bugs.stackoverflow.belisarius.models.Post;
Expand All @@ -18,11 +19,17 @@
import org.sobotics.chatexchange.chat.event.EventType;
import org.sobotics.redunda.PingService;

import io.swagger.client.ApiException;

public class MonitorService {
private static final Logger LOGGER = LoggerFactory.getLogger(MonitorService.class);

private final boolean shouldOutput = new PropertyService().getProperty("outputMessage").equals("true");
private final Belisarius belisarius;
private final StackExchangeClient client;
private final String sitename;
private final int roomId;

private Room room;
private final PingService redunda;
private ScheduledExecutorService executorService;
Expand All @@ -34,16 +41,23 @@ public MonitorService(
PingService redunda
) {
this.belisarius = new Belisarius(sitename);
this.client = client;
this.sitename = sitename;
this.roomId = chatroomId;

if (chatroomId != 0) {
this.room = client.joinRoom(ChatUtils.getChatHost(sitename), chatroomId);

LOGGER.info("Joined room " + chatroomId + " on " + sitename + ".");
this.joinRoom();
}

this.redunda = redunda;
}

public final void joinRoom() {
this.room = client.joinRoom(ChatUtils.getChatHost(sitename), roomId);

LOGGER.info("Joined room " + roomId + " on " + sitename + ".");
}

public void runMonitor() {
// Add event listeners
room.addEventListener(EventType.USER_MENTIONED, event -> ChatUtils.handleMentionedEvent(event, this));
Expand Down Expand Up @@ -78,20 +92,23 @@ public void executeOnce(String postId) {
}

public void stop() {
executorService.shutdown();
// leave room and exit
client.close();
System.exit(0);
}

public void reboot() {
this.stop();
executorService = Executors.newSingleThreadScheduledExecutor();
try (client) {
sendMessageToChat(Belisarius.README + "] rebooted at " + Instant.now());
}

this.runMonitor();
sendMessageToChat(Belisarius.README + "] rebooted at " + Instant.now());
executorService.shutdown();

try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
LOGGER.error("sleep was interrupted", exception);
Application.init();
} catch (ApiException exception) {
LOGGER.info("Exception while rebooting, exiting now.", exception);
System.exit(0);
}
}

Expand Down

0 comments on commit 51dc45c

Please sign in to comment.