unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* Is it possible to create a thread in Rails subscribe to Redis message channel?
@ 2015-03-24 15:28 胡明
  2015-03-24 22:44 ` Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: 胡明 @ 2015-03-24 15:28 UTC (permalink / raw)
  To: unicorn-public

Hi there

I have one question about related to thread in Unicorn; I have asked the
quesiton on stackoverflow.com but no one answers:

http://stackoverflow.com/questions/29180275/is-it-possible-to-create-a-thread-in-rails-subscribe-to-redis-message-channel

I also pasted the question in this email, hope that someone could help me
with this:

==============================================

I am trying to create a thread in Rails to subscribe a message channel of
Redis. Is there a way to do this? I am using unicorn.

I have tried to do this in the unicorn configuration like this:

after_fork do |server, worker|

  Thread.new do
    begin
      $redis.subscribe(:one, :two) do |on|
        on.subscribe do |channel, subscriptions|
          puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
        end
        on.message do |channel, message|
          puts "##{channel}: #{message}"
          $redis.unsubscribe if message == "exit"
        end
        on.unsubscribe do |channel, subscriptions|
          puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
        end
      end
    rescue Redis::BaseConnectionError => error
      puts "#{error}, retrying in 1s"
      sleep 1
      retry
    end
  endend

But it will make the unicorn server unable to handle any web request. I
thought that if I am using a different thread to subscribe to Redis, it
won't block the main thread; am I missing something here?

-- 
Regards,
Hu, Ming


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-24 22:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-24 15:28 Is it possible to create a thread in Rails subscribe to Redis message channel? 胡明
2015-03-24 22:44 ` Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).