Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several issues to think about here - is there going to be a single process using your API key at any one time, or is it possible that multiple processes would be running at once? If you have multiple <code>delayed_job</code> workers, I think the latter is likely. I haven't used <code>delayed_jobs</code> enough to give you a good solution to that, but my feeling is you would be restricted to a single worker.</p> <p>I am currently working on a similar problem with an API with a restriction of 1 request every 0.5 seconds, with a maximum of 1000 per day. I haven't worked out how I want to track the per-day usage yet, but I've handled the per-second restriction using threads. If you can frame the restriction as "1 request every 0.2 seconds", that might free you up from having to track it on a minute-by-minute basis (though you still have the issue of how to keep track multiple workers).</p> <p>The basic idea is that I have an request method that splits a single request into a queue of request parameters (based on the maximum number of objects allowed per request by the api), and then another method iterates over that queue and calls a block which sends the actual request to the remote server. Something like this:</p> <pre><code>def make_multiple_requests(queue, &amp;block) result = [] queue.each do |request| timer = Thread.new { sleep REQUEST_INTERVAL } execution = Thread.new { result &lt;&lt; yield(request) } [timer, execution].each(&amp;:join) end result end </code></pre> <p>To use it:</p> <pre><code>make_multiple_requests(queue) do |request| your_request_method_goes_here(request) end </code></pre> <p>The main benefit here is that if a request takes longer than the allowed interval, you don't have to wait around for the <code>sleep</code> to finish, and you can start your next request right away. It just guarantees that the next request won't start until at least the interval has passed. I've noticed that even though the interval is set correctly, I occasionally get an 'over-quota' response from the API. In those cases, the request is retried after the appropriate interval has passed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload