Permalink
Browse files

fix item variant creation, add job to update every 5m

  • Loading branch information...
birarda committed Sep 22, 2016
1 parent 6b3a4c3 commit bdfe9a75ab36cfc2bf693e602de61ee767a89937
View
@@ -81,6 +81,9 @@ gem 'sitemap_generator'
# use sidekiq for background jobs
gem 'sidekiq'
# use sidekiq-scheduler for scheduled jobs
gem 'sidekiq-scheduler', '~> 2.0'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
View
@@ -268,6 +268,7 @@ GEM
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
ruby_gntp (0.3.4)
rufus-scheduler (3.1.10)
sass (3.4.22)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
@@ -282,6 +283,12 @@ GEM
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (~> 1.5)
redis (~> 3.2, >= 3.2.1)
sidekiq-scheduler (2.0.9)
multi_json (~> 1)
redis (~> 3)
rufus-scheduler (~> 3.1.8)
sidekiq (>= 3)
tilt (>= 1.4.0)
sitemap_generator (5.1.0)
builder
skylight (0.10.6)
@@ -372,6 +379,7 @@ DEPENDENCIES
sass-rails (~> 5.0)
selectize-rails
sidekiq
sidekiq-scheduler (~> 2.0)
sitemap_generator
skylight
slim
View
@@ -92,8 +92,8 @@ def optionally_add_missing_variant
# find or create the ItemVariant that represents this item
ItemVariant.find_or_create_by({
item_id: self.item_id,
certification: self.certification,
paint_color: self.paint_color
certification: UserItem.certifications[self.certification],
paint_color: UserItem.paint_colors[self.paint_color]
})
end
@@ -0,0 +1,26 @@
class RarePercentageWorker
include Sidekiq::Worker
def perform
ItemVariant.all.each do |variant|
[:inventory, :wishlist].each do |list|
total_item_count =
UserItem.where({
item_id: variant.item_id,
certification: variant.certification,
paint_color: variant.paint_color,
list: UserItem.lists[list]
}).count('DISTINCT user_id')
listed_percentage = 0
if total_item_count != 0
total_users = User.count
listed_percentage = 100 * (total_item_count.to_f / total_users)
end
variant.update_attribute(list == :inventory ? :inventory_percentage : :wishlist_percentage, listed_percentage)
end
end
end
end
@@ -65,4 +65,6 @@
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
Sidekiq::Logging.logger.level = Logger::DEBUG
end
@@ -0,0 +1,8 @@
require 'sidekiq/scheduler'
Sidekiq.configure_server do |config|
config.on(:startup) do
Sidekiq.schedule = YAML.load_file(File.expand_path('../sidekiq_scheduler.yml', File.dirname(__FILE__)))
Sidekiq::Scheduler.reload_schedule!
end
end
View
@@ -2,6 +2,7 @@
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
require 'sidekiq/web'
require 'sidekiq-scheduler/web'
authenticate :user, lambda { |u| u.admin? } do
mount Sidekiq::Web => '/sidekiq'
end
@@ -0,0 +1,5 @@
update_rare_percentages:
every: 5m
class: RarePercentageWorker
queue: default
description: 'Updates Owned/Wanted % for all item variants'

0 comments on commit bdfe9a7

Please sign in to comment.