GithubHelp home page GithubHelp logo

Comments (7)

LouisKottmann avatar LouisKottmann commented on June 20, 2024 1

Right, that looks like the obvious way to do it. Thank you.

from slack-ruby-bot.

dblock avatar dblock commented on June 20, 2024

What's the stack for that status? I would get a backtrace first for when this happens.

Compare with https://github.com/slack-ruby/slack-ruby-bot-server/blob/0b8bbbba45a2b07f35b223b4c18ec86888fc0f49/lib/slack-ruby-bot-server/service.rb, you might be accidentally looping very quickly when you get disconnected and need to reconnect, or something like that.

from slack-ruby-bot.

LouisKottmann avatar LouisKottmann commented on June 20, 2024

How do you suggest I get a backtrace?

I have fixed my code to call stop! after I'm done with a new client, maybe that will solve the issue.
Maybe the clients never got GC'd and I'd increasingly had more and more clients connecting.

from slack-ruby-bot.

dblock avatar dblock commented on June 20, 2024

How do you suggest I get a backtrace?

Add a begin/rescue around start_async to begin with, but otherwise find where that message is emitted from and walk backwards from there to see what calls it, etc.

I have fixed my code to call stop! after I'm done with a new client, maybe that will solve the issue.
Maybe the clients never got GC'd and I'd increasingly had more and more clients connecting.

That's possible.

from slack-ruby-bot.

LouisKottmann avatar LouisKottmann commented on June 20, 2024

I will do that if I see the error again, and close this ticket otherwise. Thank you for your time.

from slack-ruby-bot.

LouisKottmann avatar LouisKottmann commented on June 20, 2024

The error did not appear again, so it was me calling an extra:

client = SlackRubyBot::Client.new(token: ENV['SLACK_API_TOKEN'])
client.start_async

for background jobs, and letting it getting GC'd witout further action. This would make clients pile up and eventually getting rate limited when they all tried reconnecting.

While in fact I should have called this when I was done with temporary clients:

client.stop!

I now use an ensure block like so:

def self.with_client_and_data_or_new(client, data)
  if client.nil? || data.nil?
    begin
      client  = self.get_new_client
      channel = self.get_channel_by_name(client, 'any_chan_name')
      data    = self.wrap_in_data_object(channel: channel.id,
                                         user: MY_BOT_USERID)
      yield(client, data)
    ensure
      client.stop!
      client = nil
    end
  else
    yield(client, data)
  end
end

private
def self.get_channel_by_name(client, channel_name)
  client
    &.channels
    &.select { |k, chan| chan['name'] == channel_name }
    &.map { |id, info| info }
    &.first
end

def self.wrap_in_data_object(channel: nil, user: nil)
  Hashie::Mash.new({ channel: channel, user: user})
end

def self.get_new_client
  client = SlackRubyBot::Client.new(token: ENV['SLACK_API_TOKEN'])
  client.start_async

  10.times do
    break if client.started?
    sleep 1
  end

  sleep 1

  client
end

that I use like this:

with_client_and_data_or_new(client, data) do |c, d|
  # do something with c(lient) & d(ata)
end

Is that a good approach or did I miss something obvious?

from slack-ruby-bot.

dblock avatar dblock commented on June 20, 2024

Does your background job need an RTM client that's listening on Slack events? If you're trying to call some methods like listing channels, instantiate a Slack::Web::Client.new(token: ...) and call whatever you need. There will be nothing to shutdown.

from slack-ruby-bot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.