Yurijmi
Yurijmi

Reputation: 95

How can i create a rails worker which is always connected to an external service?

I have a rails application which caches data from external service and also converts it to json. External service uses it's own protocol based on RPC convention.

Basically i need a background worker which keeps a connection alive by pinging back and forth using that protocol. When the rails app sends jobs to it, the worker translates it to queries for an external service and transmits converted json data back to application. Application then caches it and transmits it back to user.

The different approach is to use sidekiq and connect/disconnect on each job. This is inefficient.

I need some advice on this topic. Maybe even a different approach. Thanks in advance!

Upvotes: 1

Views: 376

Answers (2)

Mike Perham
Mike Perham

Reputation: 22228

Set up a pool of connections to be used by the jobs running within Sidekiq.

Upvotes: 1

Marc Rohloff
Marc Rohloff

Reputation: 1352

You could create a global instance of the connection client, either when the sidekiq process starts or on the first request. I would assume that the client is single threaded which you could deal with by setting the concurrency level of Sidekiq to 1. If that is an issue then you will need to do something more complex like creating a pool of connections.

Upvotes: 1

Related Questions