driver-slack icon indicating copy to clipboard operation
driver-slack copied to clipboard

Bot disconnecting, but loop keeps running

Open fliespl opened this issue 8 years ago • 6 comments

Hi Marcel, quick question - is slack driver stable enough to be used in production env?

In our case we have noticed that every 2-3 days Slack Bot (Apps) is being marked as disconnected in channel.

Command line script is no killed though, there is no error in command line output (I believe periodic timer executes as well, but need to double check on that).

Our only option is to kill command and restart it. Any advice?

fliespl avatar Sep 25 '17 07:09 fliespl

Hi,

not that I know of. I will need to look into this issue.

mpociot avatar Sep 25 '17 07:09 mpociot

I have added locally ping / pong support to websocket and I am waiting for results (if bot gets inactive on slack).

Will let you know in 2-3 days if this could be the possible resolution.

fliespl avatar Sep 26 '17 12:09 fliespl

It seems that ping / pong on websocket fixed my issue.

Extended cleitn with: getWebSocket method:


class RealTimeClient extends \Slack\RealTimeClient
{
    public function getWebSocket()
    {
        return $this->websocket;
    }
}

And used such code in a loop periodic timer:

        /** @var \AppBundle\Drivers\Slack\RealTimeClient $client */
        $client = $botMan->getClient();

        $client->on('pong', function($data) use($command, $output) {
            $command->awaitingPings = 0;
        });
        $command = $this;
        $checkConnection = function() use($client, $output, $command) {
            if(!$client->isConnected()) {
                $output->writeln(date('[Y-m-d H:i:s]').' - Not connected');
                throw new \Exception('Not connected');
            }

            $id = 'ping:'.time();

            $data = [
                'id' => $id,
                'type' => 'ping'
            ];

            $client->getWebSocket()->send(json_encode($data));

            if($command->awaitingPings >= 5) {
                throw new \Exception('Too many incomplete pings');
            }

            $command->awaitingPings++;

            $memory = memory_get_usage();
            $output->writeln(sprintf('[%s] Missed pings: %s, Memory usage.: %.02f KByte',
                date('Y-m-d H:i:s'),
                $command->awaitingPings,
                $memory / 1024));
        };
        $loop->addPeriodicTimer(120, $checkConnection);

fliespl avatar Oct 03 '17 08:10 fliespl

That’s great! Do you want to PR this yourself? Otherwise I’ll take care of it later.

mpociot avatar Oct 03 '17 08:10 mpociot

@mpociot this code is more like a PoC than real solution that can be merged.

I will try to get to it later this week, do some refactoring and PR. :)

fliespl avatar Oct 03 '17 08:10 fliespl

Hi, I'm experiencing the same issue. The loop keeps running but after a few days the bot does not respond anymore. @fliespl did you ever get to creating a patch?

orottier avatar Jan 29 '18 08:01 orottier