Marcin Doliwa
Marcin Doliwa

Reputation: 3659

Sending http requests every n seconds in my Rails app

What's the best way to send http requests to external api every n number of seconds? Where n is changing after every request.

I have inifinite loop which calculates time interval and sends http request, but I don't know what's the best way to use it in Rails app.

I though sidekiq would be perfect solution. With chained jobs, where job would send request, calculate time interval and schedule another job with set(wait: n). But it looks like Sidekiq has polling interval and set(wait: n) does not run request in exactly n seconds.

How would you do something like this?

Upvotes: 1

Views: 768

Answers (2)

yivo
yivo

Reputation: 3594

You are totally right about Sidekiq. It will be the best solution I think. Polling interval can be configured via average_scheduled_poll_interval . Here there are documentation

Do so:

  1. Create an async job

  2. After the job is completed queue the same job and ask Sidekiq to wait some time. SMSDelegationJob.perform_later(wait: 10.seconds)

  3. Don't forget to develop good logic for exception handling

  4. Don't forget to set low polling interval

  5. Smart root job manually or via console.

Good luck with it.

Upvotes: 1

Frankie Roberto
Frankie Roberto

Reputation: 1359

Is it n seconds between requests (i.e. from when the last one completed to when the next one starts), or should they start every n seconds, regardless of how long the last one took (or if it was successful or not)?

Answering that question should tell you whether the requests need to be made in parallel (using some form of concurrency), or whether you could just do it from a single long-lasting process.

Upvotes: 0

Related Questions