shopify_unlimited 0.0.17.threadsafe → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa8f62c2ce2a1f1dcbf25df03bd1210234b7d76b
4
- data.tar.gz: 326c1b9728cee6f230768c50b0528a325b04f44a
3
+ metadata.gz: 6330aa14a7c439f7200721b4af410b9f7a099be8
4
+ data.tar.gz: 5d3379107c2df7a6d24af4c10e8d315a89f70222
5
5
  SHA512:
6
- metadata.gz: 0ae8d3d2dbed282c326976ab386d7ce31eb28db82ecb94e1a0560b37c470d35187a20b587ffdd73e5f0331d0a9487e2ee3f04ba34bfa6bf4143b3cb79ac30909
7
- data.tar.gz: b5495e4f699511dce761a08fe72a6d244d2f195504cc508c11bb16ce25c1cb642294dd1219cabe96017c0f5a9bca85c87087fa1990d2f35cd32c4cf6e10eaa4d
6
+ metadata.gz: ba55b1d9a242f79fd7de2d364841c1a7fe660d338e973dfc0910e88248f25321cef6a9f03e4232d5e3f748ef4452883d081acada92cdf0a3a76c283a8350d23c
7
+ data.tar.gz: 2c421eeef8d843bfe5dc0d8d3d91c2c0f90cda817e27a9a9f96b036dfc21e742f26d349ada86a5f3a655a2d870ab333293735b6d24dd776ff47da7c83bf89540
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ tags
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # ShopifyUnlimited
2
2
 
3
3
  1. Allows API calls like products = Product.find(:all, params:{limit: false}), which will make as many requests as needed to fetch all Products in batches of 250. The result set can be queried for the number of requests required: ```products.requests_made```
4
- 2.
5
- 2. Attempts to record api limits a little better by adding ```ShopifyAPI::Base.connection.shopify_credit```. This object contains the credit used since the first request (in a given process) and the time of the first request. It resets when the credit used goes down or the time elapsed has been more than 5 minutes.
4
+ 2.Contains a throttle tuned for 4-8 workers consuming a single shop's api limit. It will keep all workers busy while limiting the number of refused requests.
5
+ 3. Retries 404's once. This is because Shopify in the past has occasionally 404'd on count queries.
6
+
6
7
 
7
8
  ## Installation
8
9
 
@@ -18,6 +19,11 @@ Or install it yourself as:
18
19
 
19
20
  $ gem install shopify_unlimited
20
21
 
22
+
23
+ ## Usage
24
+ 1. to fetch all records using behind the scenes batching, use find with limit: false
25
+ 2. to see the throttle in action, set ENV['SHOPIFY_UNLIMITED_DEBUG'] and it will log info to STDOUT
26
+
21
27
  ## Contributing
22
28
 
23
29
  1. Fork it
@@ -7,19 +7,36 @@ end
7
7
 
8
8
 
9
9
  module ActiveResource
10
- class Base
10
+ class Base
11
11
  SHOPIFY_MAX_RECORDS_PER_REQUEST = 250
12
-
12
+
13
+ def self.find_all(params = {}, &block)
14
+ records = 0
15
+ params[:limit] ||= 50
16
+ params[:page] = 1
17
+
18
+ begin
19
+ page = find(:all, :params => params)
20
+ return records if page.nil?
21
+ page.each do |value|
22
+ records += 1
23
+ block.call(value)
24
+ end
25
+ params[:page] += 1
26
+ end until page.length < params[:limit]
27
+ records
28
+ end
29
+
13
30
  class << self
14
31
  # get reference to unbound class-method #find_every
15
32
  find_every = self.instance_method(:find_every)
16
33
 
17
34
  define_method(:find_every) do |options|
18
35
  options[:params] ||= {}
19
-
36
+
20
37
  # Determine number of ShopifyAPI requests to stitch together all records of this query.
21
38
  limit = options[:params][:limit]
22
-
39
+
23
40
 
24
41
  results = []
25
42
  results.singleton_class.class_eval do
@@ -30,7 +47,7 @@ module ActiveResource
30
47
  # Bail out to default functionality unless limit == false
31
48
  # NOTE: the algorithm was switched from doing a count and pre-calculating pages
32
49
  # because Shopify 404s on some count requests
33
- if limit == false
50
+ if limit == false
34
51
  options[:params].update(:limit => SHOPIFY_MAX_RECORDS_PER_REQUEST)
35
52
 
36
53
  limit = SHOPIFY_MAX_RECORDS_PER_REQUEST
@@ -51,8 +68,8 @@ module ActiveResource
51
68
  results.requests_made += 1
52
69
  end
53
70
 
54
- results
71
+ results
55
72
  end
56
- end
73
+ end
57
74
  end
58
75
  end
@@ -12,19 +12,21 @@ module ShopifyUnlimited
12
12
  @throttle_increment = @throttle
13
13
  @requests_threshold = 10
14
14
  @debug = ! ENV['SHOPIFY_UNLIMITED_DEBUG'].blank?
15
+ @not_found_retries = Integer(ENV['SHOPIFY_UNLIMITED_NOTFOUND_RETRIES']) rescue 1
15
16
  end
16
-
17
+
17
18
  def dbg(msg)
18
19
  puts msg if @debug
19
20
  end
20
-
21
+
21
22
  def run
22
23
  if @running
23
24
  return yield
24
25
  end
25
26
  @running = true
26
27
  value = nil
27
- retries ||= 0
28
+ tries ||= 0
29
+ not_found_tries ||= 0
28
30
  orig_logger = ActiveResource::Base.logger
29
31
  begin
30
32
  dbg "--- throttle.run ---"
@@ -45,9 +47,9 @@ module ShopifyUnlimited
45
47
  case e.response.code
46
48
  when '404'
47
49
  dbg " 404"
48
- sleep 5 + retries + (rand * rand * 5)
49
- retries += 1
50
- if retries < 2
50
+ sleep 5 + not_found_tries + (rand * rand * 5)
51
+ not_found_tries += 1
52
+ if not_found_tries <= @not_found_retries
51
53
  ActiveResource::Base.logger ||= Logger.new(STDOUT)
52
54
  ActiveResource::Base.logger.info "Shopify returned not found: #{e.message}. Retrying"
53
55
  retry
@@ -59,9 +61,9 @@ module ShopifyUnlimited
59
61
  dbg " 429"
60
62
  ActiveResource::Base.logger.info "Shopify hit api limit" if ActiveResource::Base.logger
61
63
  @throttle += rand/5
62
- retries += 1
63
- sleep (@throttle * 4 * retries) + rand/10
64
- if retries < 10
64
+ tries += 1
65
+ sleep (@throttle * 4 * tries) + rand/10
66
+ if tries < 10
65
67
  retry
66
68
  else
67
69
  raise
@@ -79,8 +81,6 @@ module ShopifyUnlimited
79
81
  sleep fuzzy_sleep_time
80
82
  else
81
83
  @throttle = (0.94 + rand/20) * @throttle
82
- dbg " sleep #{rand/20}"
83
- sleep rand/20
84
84
  end
85
85
  dbg "=== throttle.done ==="
86
86
  value
@@ -1,3 +1,3 @@
1
1
  module ShopifyUnlimited
2
- VERSION = "0.0.17.threadsafe"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify_unlimited
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17.threadsafe
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Johnston
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-10 00:00:00.000000000 Z
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource
@@ -129,12 +129,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  requirements:
132
- - - ">"
132
+ - - ">="
133
133
  - !ruby/object:Gem::Version
134
- version: 1.3.1
134
+ version: '0'
135
135
  requirements: []
136
136
  rubyforge_project:
137
- rubygems_version: 2.2.2
137
+ rubygems_version: 2.6.10
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: 'allow use of limit: false in api requests'