Sha256: 5fb7d5c1d717d99f1c65c3da573a6d575ac2da0bee3de35a2d0480fad229e39c

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require_relative 'ext/slack-ruby-bot/client'

module SlackRubyBotServer
  module RealTime
    class Server < ::SlackRubyBot::Server
      attr_accessor :team

      def initialize(attrs = {})
        attrs = attrs.dup
        @team = attrs.delete(:team)
        raise 'Missing team' unless @team

        attrs[:token] = @team.token
        super(attrs)
        open!
      end

      def self.start_server!(team, server, wait = 1)
        team.server = server
        server.start_async
      rescue StandardError => e
        SlackRubyBotServer::Config.service_class.instance.send(:run_callbacks, :error, team, e)
        case e.message
        when 'account_inactive', 'invalid_auth' then
          SlackRubyBotServer::Config.service_class.instance.logger.error "#{team.name}: #{e.message}, team will be deactivated."
          SlackRubyBotServer::Config.service_class.instance.deactivate! team
        else
          wait = e.retry_after if e.is_a?(Slack::Web::Api::Errors::TooManyRequestsError)
          SlackRubyBotServer::Config.service_class.instance.logger.error "#{team.name}: #{e.message}, restarting in #{wait} second(s)."
          sleep(wait)
          start_server! team, server, [wait * 2, 60].min
        end
      end

      def restart!(_wait = 1)
        # when an integration is disabled, a live socket is closed, which causes the default behavior of the client to restart
        # it would keep retrying without checking for account_inactive or such, we want to restart via service which will disable an inactive team
        SlackRubyBotServer::Config.service_class.instance.logger.info "#{team.name}: socket closed, restarting ..."
        SlackRubyBotServer::Config.service_class.instance.restart! team
        open!
      end

      private

      def open!
        client.owner = team
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-ruby-bot-server-rtm-0.1.1 lib/slack-ruby-bot-server-rtm/server.rb