Search code examples
ruby-on-railsrubymultithreadingsingletonresque

How to retrieve correct Rails singleton instance from Resque workers


I have a singleton store in my Rails application which holds some assets. It's accessible like this :

Synchronizer.instance

For some reasons I don't have any database service in my project apart from a Redis instance where Resque does its magic.

I would like to access the assets from the Resque workers like this :

Synchronizer.instance.asset(id)

Because workers are in different threads, the instance of Synchronizer I retrieve is not the same as the Rails environment's.

Rails: Synchronizer:0x007feb5c389a10 Resque: Synchronizer:0x00000008566f00

It kinds of kill the interest of using a Singleton in the first place. I could pass information about asset as a Hash into workers instead, but is there another way to access the proper singleton instance?


Solution

  • As Stefan commented, it is not possible to access a ruby object from another process, and using a singleton to hold assets was a bad idea anyway. I learnt that you should always avoid singletons when you can. I finally cached the assets in Redis and retrieve them when needed.