Use improved shutdown for jsonRpc command

This commit is contained in:
AsamK 2023-11-09 16:08:39 +01:00
parent f252597002
commit b7fedff511
2 changed files with 17 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputType;
import org.asamk.signal.Shutdown;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.jsonrpc.SignalJsonRpcDispatcherHandler;
import org.asamk.signal.manager.Manager;
@ -15,7 +16,10 @@ import org.asamk.signal.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.channels.Channels;
import java.util.List;
import java.util.function.Supplier;
@ -57,6 +61,7 @@ public class JsonRpcDispatcherCommand implements LocalCommand, MultiLocalCommand
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
Shutdown.installHandler();
final var receiveMode = ns.<ReceiveMode>get("receive-mode");
final var receiveConfig = getReceiveConfig(ns);
m.setReceiveConfig(receiveConfig);
@ -67,6 +72,8 @@ public class JsonRpcDispatcherCommand implements LocalCommand, MultiLocalCommand
final var handler = new SignalJsonRpcDispatcherHandler(jsonOutputWriter,
lineSupplier,
receiveMode == ReceiveMode.MANUAL);
final var thread = Thread.currentThread();
Shutdown.registerShutdownListener(thread::interrupt);
handler.handleConnection(m);
}
@ -74,6 +81,7 @@ public class JsonRpcDispatcherCommand implements LocalCommand, MultiLocalCommand
public void handleCommand(
final Namespace ns, final MultiAccountManager c, final OutputWriter outputWriter
) throws CommandException {
Shutdown.installHandler();
final var receiveMode = ns.<ReceiveMode>get("receive-mode");
final var receiveConfig = getReceiveConfig(ns);
c.getManagers().forEach(m -> m.setReceiveConfig(receiveConfig));
@ -85,10 +93,14 @@ public class JsonRpcDispatcherCommand implements LocalCommand, MultiLocalCommand
final var handler = new SignalJsonRpcDispatcherHandler(jsonOutputWriter,
lineSupplier,
receiveMode == ReceiveMode.MANUAL);
final var thread = Thread.currentThread();
Shutdown.registerShutdownListener(thread::interrupt);
handler.handleConnection(c);
}
private static Supplier<String> getLineSupplier() {
return IOUtils.getLineSupplier(new InputStreamReader(System.in, IOUtils.getConsoleCharset()));
// Use FileChannel for stdin, because System.in is uninterruptible
final var stdInCh = Channels.newInputStream((new FileInputStream(FileDescriptor.in)).getChannel());
return IOUtils.getLineSupplier(new InputStreamReader(stdInCh, IOUtils.getConsoleCharset()));
}
}

View file

@ -15,6 +15,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
@ -93,6 +94,9 @@ public class IOUtils {
return () -> {
try {
return bufferedReader.readLine();
} catch (ClosedChannelException ignored) {
logger.trace("Line supplier has been interrupted.");
return null;
} catch (IOException e) {
logger.error("Error occurred while reading line", e);
return null;