Skip to content

Commit 89e39e5

Browse files
committed
Add items:sync rake task
`rake items:sync` will sync all items in the database. Some chunking was done in `ItemSyncJob` in order to get around Amazon's rate limitations. TODO: * Batch the Amazon query (ItemLookup can take up to 10 ASINs) * Add a delayed job queue (new issue?) * Add specs
1 parent 54fa761 commit 89e39e5

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

app/jobs/item_sync_job.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'amazon_product_api'
2+
3+
# This job is responsible for syncing items with Amazon.
4+
#
5+
# Amazon changes prices, details, etc. pretty frequently, so this job will
6+
# query the Amazon record associated with the ASIN and make any required local
7+
# updates.
8+
#
9+
# Bang methods (ex. `#amazon_item!`) perform an HTTP request.
10+
#
11+
class ItemSyncJob < ApplicationJob
12+
queue_as :default
13+
14+
# Syncs all items and writes the results to the log
15+
def perform(*_args)
16+
Rails.logger.info bold_green('Syncing all items')
17+
sync_all!
18+
Rails.logger.info bold_green('Done syncing!')
19+
end
20+
21+
private
22+
23+
def sync_all!
24+
# This is done in slices to avoid Amazon rate limits
25+
Item.all.each_slice(3) do |items|
26+
items.each { |item| sync! item }
27+
sleep 2.seconds
28+
end
29+
end
30+
31+
def sync!(item)
32+
Rails.logger.info green("Syncing item #{item.id}: #{item.name}")
33+
update_hash = amazon_item!(item).update_hash
34+
item.assign_attributes(update_hash)
35+
36+
if item.changed?
37+
Rails.logger.info bold_green("Changed:\n") + item.changes.pretty_inspect
38+
item.save!
39+
end
40+
end
41+
42+
def amazon_item!(item)
43+
client = AmazonProductAPI::HTTPClient.new
44+
client.item_lookup(item.asin).response
45+
end
46+
47+
# Some styles for log text. This is so minor that it's not worth
48+
# bringing in a full library.
49+
50+
def bold_green(text)
51+
bold(green(text))
52+
end
53+
54+
def green(text)
55+
"\e[32m#{text}\e[0m"
56+
end
57+
58+
def bold(text)
59+
"\e[1m#{text}\e[22m"
60+
end
61+
end

lib/tasks/items.rake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
namespace :items do
4+
desc 'Check Amazon listings and update local items accordingly.'
5+
task sync: :environment do
6+
ItemSyncJob.perform_now
7+
end
8+
end

0 commit comments

Comments
 (0)