Created
May 25, 2015 23:12
-
-
Save vlado/52d77e12b86579da3728 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ElasticsearchSupport | |
class ReindexUpdaterStore | |
class << self | |
def redis | |
$redis | |
end | |
def start(klass) | |
key = key_for_reindex(klass) | |
redis.set(key, "true") | |
end | |
def stop(klass) | |
key = key_for_reindex(klass) | |
redis.expire(key, 1) | |
drain!(klass) | |
end | |
def store(klass, id) | |
if reindex_running?(klass) | |
old_ids = retrieve(klass) | |
ids = (old_ids << id).uniq.to_json | |
redis.set(key_for_updated_records(klass), ids) | |
end | |
end | |
private | |
def drain!(klass) | |
ids = retrieve(klass) | |
klass.where(id: ids).each(&:refresh_in_elasticsearch) | |
redis.expire(key_for_updated_records(klass), 1) | |
ids | |
end | |
def key_for_reindex(klass) | |
"#{klass.table_name}_reindex_running" | |
end | |
def key_for_updated_records(klass) | |
"updated_#{klass.table_name}_during_reindex" | |
end | |
def reindex_running?(klass) | |
key = key_for_reindex(klass) | |
redis.get(key) == "true" | |
end | |
def retrieve(klass) | |
key = key_for_updated_records(klass) | |
if value = redis.get(key) | |
JSON.parse(value) | |
else | |
[] | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment