Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unscope all create or update queries #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/controllers/concerns/wp_preview_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module WpPreviewTools
extend ActiveSupport::Concern

THRESHOLD = 5

#
# Validates preview tokens for wp_post_models that are not published.
# for instance the statuses `draft` and `pending`.
Expand Down Expand Up @@ -34,12 +36,15 @@ def token(wp_post_model)
# Retries loading of a WpCache model, if it was not found the first time.
# This to avoid NotFound errors due to the delaying of WP API calls.
#
def retry_when_preview(retrying = false, &block)
def retry_when_preview(retrying = false, threshold = 0, &block)
raise "retry_when_preview requires a block" unless block_given?
return block.call
rescue ActiveRecord::ActiveRecordError => e
raise e if !params[:preview] || retrying
raise e unless params[:preview] || retrying
return nil if THRESHOLD < threshold

sleep 1.5
return retry_when_preview(true, &block)

return retry_when_preview(true, threshold + 1, &block)
end
end
14 changes: 7 additions & 7 deletions app/models/concerns/wp_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def classes
# TODO (cies): add a configurable amount of delay, defaulting to 0.5secs
def schedule_create_or_update(wp_id, preview = false, request = nil)
extra_info = request ? " after #{request.fullpath} -- #{request.body.read}" : ""
Rails.logger.info("SCHEDULED by #{self.class}" + extra_info)
Rails.logger.info("SCHEDULED by #{self.name}" + extra_info)
WpApiWorker.perform_async(self, wp_id, preview)
end

Expand All @@ -41,7 +41,7 @@ def create_or_update(wp_type, wp_id, preview = false)
# WP API will return a code if the route is incorrect or
# the specified entry is none existant. If so return early.
return if wp_json[0] and invalid_api_responses.include? wp_json[0]["code"]
where(wp_id: wp_id).first_or_initialize.update_wp_cache(wp_json)
unscoped.where(wp_id: wp_id).first_or_initialize.update_wp_cache(wp_json)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should DRY this up a bit 🍶

end

def create_or_update_all
Expand Down Expand Up @@ -69,30 +69,30 @@ def create_or_update_all_paginated
break if wp_json.empty?
ids << wp_json.map do |json|
wp_id = json['ID']
where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
unscoped.where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
wp_id
end
page = page + 1
end
where('wp_id NOT IN (?)', ids.flatten).destroy_all unless ids.empty?
unscoped.where('wp_id NOT IN (?)', ids.flatten).destroy_all unless ids.empty?
end

# TODO (dunyakirkali) doc
def create_or_update_all_non_paginated
wp_json = get_from_wp_api(wp_type)
ids = wp_json.map do |json|
wp_id = json['ID']
where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
unscoped.where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
wp_id
end
where('wp_id NOT IN (?)', ids).destroy_all unless ids.empty?
unscoped.where('wp_id NOT IN (?)', ids).destroy_all unless ids.empty?
end

#
# Purge a cached piece of content, while logging any exceptions.
#
def purge(wp_id)
where(wp_id: wp_id).first!.destroy
unscoped.where(wp_id: wp_id).first!.destroy
rescue
logger.warn "Could not purge #{self} with id #{wp_id}, no record with that id was found."
end
Expand Down