开发者

How to create method that hits an API based on a one sec interval?

开发者 https://www.devze.com 2023-03-03 06:29 出处:网络
I\'m creating a lib that I will have delayed_job run as a background service on heroku. The point of the lib method will be to hit a API to process a lot of data. The API I\'m hitting has a limit of

I'm creating a lib that I will have delayed_job run as a background service on heroku.

The point of the lib method will be to hit a API to process a lot of data. The API I'm hitting has a limit of 600 Graph API requests per 600 seconds.

That being said, I'd like to know if it would be possible to make sure my delayed job doesn't go over this.

Something like:

requests_made = 0
@items.each do |x|

    if request_made >= 600
        pause for 1 second??????? Is this possible?
    en开发者_JAVA技巧d

    ### HIT THE API
    requests_made = requests_made + 1
end

Is something like this possible? Is pausing possible? Heroku's delayed jobs will run a job for up to 4 hours so I don't see timing out being a problem. Thoughts?


sleep(1) is probably the most explicit and simplest method. Will using this be a problem?

Added as a response to the comment

To measure the time spent on calling an API, do

start_time = Time.now
call_the_API
spent_time = Time.now - start_time

Then you can do some conditionals on spent_time.

0

精彩评论

暂无评论...
验证码 换一张
取 消