Reputation: 3659
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
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:
Create an async job
After the job is completed queue the same job and ask Sidekiq to wait some time. SMSDelegationJob.perform_later(wait: 10.seconds)
Don't forget to develop good logic for exception handling
Don't forget to set low polling interval
Smart root job manually or via console.
Good luck with it.
Upvotes: 1
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