rampatra / jbot

Make Slack and Facebook Bots in Java.
GNU General Public License v3.0
1.2k stars 350 forks source link

me.ramswaroop.jbot.core.slack.Bot : Error pinging Slack. Slack bot may go offline when not active. Exception: #157

Closed PedroAguiar closed 4 years ago

PedroAguiar commented 5 years ago

Over long periods of inactivity, the slack bot fails through it's Ping Task execution due to java.lang.IllegalStateException: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method.

We are using version 4.1.0. Our use case only handles direct mentions and direct messages to query a database and respond with a really small and simple message. The DB payload is really small as well.

Overnight usually when no one is talking to the bot the Ping Task is executed to keep the WebSocketSession alive, during this ping - pong exercise the bot fails and does not recover from it. We have tried catching all exceptions thrown by the reply() method, terminating the session by calling session.close(CloseStatus.SERVICE_OVERLOAD) and restarting it by calling startRTMAndWebSocketConnection()

We have also made all reply() calls thread-safe by wrapping our WebSocketSession inside a ConcurrentWebSocketSessionDecorator and encapsulating all reply() calls to the following method:

private void doReply(WebSocketSession session, Event event, Message message) {
        synchronized (sendMessageLock) {
            try {
                reply(toConcurrent(session), event, message)
            } catch (Exception e) {
                log.error(e.getMessage())
                session.close(CloseStatus.SERVICE_OVERLOAD)
                startRTMAndWebSocketConnection()
            }
       }
}

private ConcurrentWebSocketSessionDecorator toConcurrent(WebSocketSession session) {
        return new ConcurrentWebSocketSessionDecorator(session, sessionSendLimit, messageBufferSizeLimit)
    }

Despite our efforts to make this reply call thread-safe and able to handle concurrency the issue seems to reside inside the PingTask. Note: Our slack-bot resides inside an ECS task with the desired count set to 1 and no autoscaling in place. All methods call the doReply() method above and do not create new threads to do so.

rampatra commented 5 years ago

@PedroAguiar sorry for the late reply but this problem is fixed in version jbot-4.1.2-rc.1 can you please use this version and update this github issue?

PedroAguiar commented 4 years ago

Thank you @rampatra, it fixed the issue although I've had to put in place dummy configuration properties for the facebook side, even when the slack profile is the only one enabled.

rampatra commented 4 years ago

Ah, I see. Maybe I can add a check and ignore the settings. Will keep this in mind for the next release.