Sachin Singh
Sachin Singh

Reputation: 7225

How to share worker among two different applications on heroku?

I have two separate applications running on heroku and pointing to same database, first one responsible for user interfaceand second one for admin interface, I am using sidekiq with redis for background job processing, I have added one worker and I am able to share 'redis-server' by setting environment variable pointing to same Redis providing Addon, Now i wish to share worker too, because adding the extra worker will cost double.

Please suggest, whether this is even possible or not?

Upvotes: 10

Views: 1583

Answers (3)

Nick Davies
Nick Davies

Reputation: 1051

Mike's answer is still a safe bet, though having to include all relevant sidekiq options inline everywhere you enqueue this class is a bit gross.

Here's a blog post that goes into more detail, and offers a second approach:

http://coderascal.com/ruby/using-sidekiq-across-different-applications/

Basically the alternative is to have a "proxy" class on the enqueueing application that controls the sidekiq behavior (queue, retries, etc.). Then there would be a matching class name on the receiving/de-queueing application.

The author of the blog post overrides some standard Sidekiq logic to redirect from the proxy class to the non-proxy class on the receiving side. Overriding the gem's behavior is a bit risky, and it enforces an extra convention on your class naming. But you may be able to get away with not doing that part.

Upvotes: 0

Bruno Buccolo
Bruno Buccolo

Reputation: 1111

How about using 3 different heroku apps?

  1. User Interface App (ui repository)
    • web: bundle exec rails server
  2. Admin Interface App (admin repository)
    • web: bundle exec rails server
  3. Worker (admin repository)

Even if 2 and 3 use the same code base, you can have 2 different heroku apps. One that starts the admin, one that actually starts sidekiq. Would that solve your problem?

Upvotes: 0

Mike Perham
Mike Perham

Reputation: 22238

If both apps are using the same Redis URL and same namespace, you can spin up one worker with that same Redis config and it will be shared by both.

Note that your Sidekiq process will boot one app or the other. The code for your Workers must be in that app. The other app won't be able to reference the code but can push jobs using:

Sidekiq::Client.push('class' => 'SomeWorker', 'args' => [1,2,3])

Note that 'class' is a String so SomeWorker can actually be defined in the other app.

Upvotes: 15

Related Questions