I’m curious on what you did to patch it?
Abhishek Yadav

Haha :D Its not a big thing that I did to solve this.

Here’s what I was doing basically inside the lambda that’s passed as a callback to PubNub —

I simply moved this data hash (where the ActiveRecord stuff is going on) outside the lambda so it happened in the main Rails thread. And besides I already need this associated event data in the main thread to serve the request. So there is no added toll.

And yes doing —


also worked.

Now there is still a curious situation that was going on which I am trying to figure out. You may be able to help me out here.

You see that viewer method? It simply uses Viewer.find to get a viewer record. And it’s already executed in the main thread — so the result is supposed to be cached.

But inside the callback, viewer is called four times, and in the logs I could see queries being fired several times!

To test this I tried to add binding.pry in the main Rails thread and I simply did Viewer.find(id) several times and the query log clearly showed the query cache being used.

Then I did the following —

Thread.new { binding.pry } 

And from inside this pry shell, I tried running the finder query again and this time it fired new queries for each call and it never used the cache.

Anyway, although it’s an unrelated thing, was pretty interesting. Would love to know what you think about this :)

Thanks man!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.