aki017 / slack-ruby-gem

A Ruby wrapper for the Slack API
MIT License
242 stars 79 forks source link

Client may die and there's no way to restart it #24

Closed dblock closed 9 years ago

dblock commented 9 years ago

The change in https://github.com/aki017/slack-ruby-gem/pull/23 assumes that the main entry point. For example https://github.com/dblock/slack-gamebot is actually a Puma web server that hosts a Slack client.

As a quick experiment I modified it to do:

      def start
        EM.run do
          ws = Faye::WebSocket::Client.new(@url)

          ws.on :open do |event|
          end

          counter = 0

          ws.on :message do |event|

            data = JSON.parse(event.data)

            puts data['type']

            if counter == 3
              puts "exit!"
              EM.stop
              return
            else
              counter += 1
            end

Here's what happens:

~/source/slack-gamebot/dblock (master)$ foreman start
10:03:33 web.1  | started with pid 48042
10:03:33 web.1  | Puma starting in single mode...
10:03:33 web.1  | * Version 2.11.0 (ruby 2.1.6-p336), codename: Intrepid Squirrel
10:03:33 web.1  | * Min threads: 0, max threads: 16
10:03:33 web.1  | * Environment: development
10:03:34 web.1  | warning: parser/current is loading parser/ruby21, which recognizes
10:03:34 web.1  | warning: 2.1.5-compliant syntax, but you are running 2.1.6.
10:03:34 web.1  | * Listening on tcp://0.0.0.0:5000
10:03:34 web.1  | Use Ctrl-C to stop
10:03:34 web.1  | I, [2015-05-27T10:03:34.994152 #48042]  INFO -- : Welcome 'gamebot' to the 'dblock' team at https://dblockdotorg.slack.com/.
10:03:35 web.1  | hello
10:03:35 web.1  | I, [2015-05-27T10:03:35.379089 #48042]  INFO -- : Successfully connected to https://dblockdotorg.slack.com/.
10:03:35 web.1  | presence_change
10:03:35 web.1  | presence_change
10:03:37 web.1  | user_typing
10:03:37 web.1  | exit!
# just sits here

Need a way to bounce EM.

tlunter commented 9 years ago

@dblock Couldn't you just put a while loop here? When start ends, the thread here just ends. Your process is just hanging because the slack thread has ended, but not the main thread running rack.

dblock commented 9 years ago

You're right. I fixed it in https://github.com/dblock/slack-gamebot/commit/617051c61eaf8328c4167f9c11b510defec3399b, and was able to test it using #stop in https://github.com/aki017/slack-ruby-gem/pull/26. Note that you have to re-create a client because you need a new websocket URL.