Publishing to Juggernaut within threads in Rails 3.1

94 views
Skip to first unread message

Dave L

unread,
Nov 18, 2011, 5:31:00 AM11/18/11
to Juggernaut 2
I'm using Juggernaut with Rails 3.1 and have run into an issue in
production. In my development environment I am able to publish to
channels from within threads. I am using threads so that I can
implement a timed quiz and ask questions after a certain period of
time. For example:

in Quiz controller:

def start_quiz
@round = 0
Thread.new {
5.times do
Juggernaut.push("quiz_#{@quiz.id}", {:round => @round})
sleep 30
@round += 1
end
}
end

Is there any apparent reason that this should be giving me problems in
production? Juggernaut does not seem to be able to push once inside
the thread or the timing is way slow at least. Should I use something
like spawn instead?

Kennon Ballou

unread,
Nov 18, 2011, 5:33:27 AM11/18/11
to juggernaut...@googlegroups.com
Are you passing :threadsafe => true to your Redis instantiation? This smells like something to do with redis-rb and not juggernaut…

Dave L

unread,
Nov 18, 2011, 2:54:38 PM11/18/11
to Juggernaut 2
I don't believe I am instantiating Redis from within my Rails
environment. I've come across redis-rb (https://github.com/ezmobius/
redis-rb) and redis-node-client (https://github.com/maccman/redis-node-
client), do I need something like that?

On Nov 18, 2:33 am, Kennon Ballou <ken...@angryturnip.com> wrote:
> Are you passing :threadsafe => true to your Redis instantiation? This smells like something to do with redis-rb and not juggernaut…
>

>  smime.p7s
> 6KViewDownload

Kennon Ballou

unread,
Nov 18, 2011, 4:17:56 PM11/18/11
to juggernaut...@googlegroups.com
> I don't believe I am instantiating Redis from within my Rails
> environment. I've come across redis-rb (https://github.com/ezmobius/
> redis-rb) and redis-node-client (https://github.com/maccman/redis-node-
> client), do I need something like that?

The juggernaut gem requires the redis gem (redis-rb) automatically as a dependency. You can set the threadsafe option by making a initializer, try something like this:

config/initializer/juggernaut.rb:
require 'juggernaut'
Juggernaut.options = {:threadsafe => true}

From my looking through the juggernaut gem this should at least set that option when it connects to Redis (I don't really know how to test that though). Give that a try and see if it makes a difference.

KB

Dave L

unread,
Nov 18, 2011, 5:26:47 PM11/18/11
to Juggernaut 2
Looking at the gem for reddis, it says that, "Starting with redis-rb
2.2.0, the client is thread-safe by default." I have 2.2.2 installed
so now I'm thinking this is not the issue. I'm gonna do some more
testing later this weekend, hopefully I find something.

>  smime.p7s
> 6KViewDownload

Dave L

unread,
Nov 19, 2011, 6:17:56 PM11/19/11
to Juggernaut 2
Ok, so I have found something kinda strange. If I create a new thread
and use sleep within that thread, calls to Juggernaut.publish do not
publish. But if I make any call to Juggernaut afterwards, Juggernaut
publishes the message. Almost as if it lies dormant and gets pushed
out. For example, I made the following call with my chat channel:

Thread.new {
sleep 1
message = "<div class='chat_line'>TestUser: 1</div>"
Juggernaut.publish(channel, message)
sleep 2
message = "<div class='chat_line'>TestUser: 2</div>"
Juggernaut.publish(channel, message)
sleep 5
message = "<div class='chat_line'>TestUser: 3</div>"
Juggernaut.publish(channel, message)
}

Then if I call Juggenaut.publish anywhere else, the first message
comes through. And if I wait another 10 seconds and call
Juggernaut.publish, the second message comes through... This is only
happening in my production environment where I am using Thin and
Nginx, so it might have nothing to do with Juggernaut, but I am really
at a loss.

Alex MacCaw

unread,
Nov 19, 2011, 6:23:48 PM11/19/11
to juggernaut...@googlegroups.com
Are you using Threads in a Rails request?

Is there a reason you're using Threads (calls to Juggernaut are practically instant)?

--
You received this message because you are subscribed to the Google Groups "Juggernaut 2" group.
To post to this group, send email to juggernaut...@googlegroups.com.
To unsubscribe from this group, send email to juggernaut-for-r...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/juggernaut-for-rails?hl=en.




--
Alex MacCaw

+12147175129
@maccman

http://alexmaccaw.co.uk

Dave L

unread,
Nov 19, 2011, 8:12:31 PM11/19/11
to Juggernaut 2
Hi Alex,
Yea, I am using an ajax request in Rails to start a quiz. I'm using
Threads to process the different rounds of the quiz. For example, if
I have a quiz that is 5 rounds where each round is 30 seconds I would
like to use an ajax request to trigger Quiz.start and then within
start do something like:

def self.start
Thread.new {
Juggernaut.push("quiz_#{self.id}", {:round => @round})
@round += 1
sleep 30
}
end

I looked into things like spawn to handle this, but I figured Threads
would work just as well. It works fine in my development environment.

Alex MacCaw

unread,
Nov 19, 2011, 8:33:13 PM11/19/11
to juggernaut...@googlegroups.com
Hmm, threads seem a bad idea for this - seems a perfect scenario for a Job queue. 

Dave L

unread,
Nov 19, 2011, 10:07:51 PM11/19/11
to Juggernaut 2
Hi Alex,
Yea, I have been looking into job queues but have some requirements
that have been giving me trouble. For example, these quiz jobs would
need to run in parallel so that multiple quizzes can occur at the same
time and it seems like all the background job processors use a queue.
Do you have any idea as to why Threads might be an issue or have any
other suggestions.
Thanks a lot,
Dave
Reply all
Reply to author
Forward
0 new messages