From 431713b9a4e90bfb4a2424b702ed7a09051ddc15 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 20 Oct 2018 20:58:55 -0400 Subject: [PATCH] Wait for process termination when sending notifications --- .../java/net/runelite/client/Notifier.java | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/Notifier.java b/runelite-client/src/main/java/net/runelite/client/Notifier.java index 34eb10a7b9..8c2316c899 100644 --- a/runelite-client/src/main/java/net/runelite/client/Notifier.java +++ b/runelite-client/src/main/java/net/runelite/client/Notifier.java @@ -40,8 +40,8 @@ import java.nio.file.Path; import java.time.Instant; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j; import net.runelite.api.ChatMessageType; @@ -215,14 +215,23 @@ public class Notifier executorService.submit(() -> { - final boolean success = sendCommand(commands) - .map(process -> process.exitValue() == 0) - .orElse(false); - - if (!success) + try { - sendTrayNotification(title, message, type); + Process notificationProcess = sendCommand(commands); + + boolean exited = notificationProcess.waitFor(500, TimeUnit.MILLISECONDS); + if (exited && notificationProcess.exitValue() == 0) + { + return; + } } + catch (IOException | InterruptedException ex) + { + log.debug("error sending notification", ex); + } + + // fall back to tray notification + sendTrayNotification(title, message, type); }); } @@ -258,25 +267,21 @@ public class Notifier commands.add(script); } - sendCommand(commands); - } - - private Optional sendCommand(final List commands) - { - log.debug("Sending command {}", commands); - try { - return Optional.of(new ProcessBuilder(commands.toArray(new String[commands.size()])) - .redirectErrorStream(true) - .start()); + sendCommand(commands); } catch (IOException ex) { - log.warn(null, ex); + log.warn("error sending notification", ex); } + } - return Optional.empty(); + private static Process sendCommand(final List commands) throws IOException + { + return new ProcessBuilder(commands.toArray(new String[commands.size()])) + .redirectErrorStream(true) + .start(); } private void storeIcon()