Search code examples
ruby-on-rails-3sidekiq

How can I share an object between a Rails instance and a Sidekiq instance


I have a listener (using the Listen gem) object that I'm adding to a constant within an initializer:

LISTENER = Listen.to(REPORT, ERROR, SENT) do |modified, added, removed|
  listener.ignore! /\.swp/
  listener.ignore /\.DS_Store/
  Communicator.notify(added)
end

I put a little admin interface around this functionality, and I display the status of the listener in the view.

In my deployment, I have a Utility instance where all my background jobs run. I may have 1 or many app servers spinning at one time, so I only want this listener to listen on the Utility instance. Sidekiq is my background processor. Therefore, in my admin interface I enlist a simple Sidekiq worker to spin up this listener.

When I obtain the status of the listener, it says it isn't running. But the process is there. This of course is because the App server is attempting to get the listener status from the constant on the application server!

How can I get the status of the object on the Sidekiq server?


Solution

  • Rails 4.2 has implemented GlobalID and here is a good blog post outlining ActiveJob and it covers using GlobalID. (You can parse live object! OMG section)

    I know this is an old post but I just came across it and thought this answer might help someone.