Skip to content

Instantly share code, notes, and snippets.

@cpetersen
Created July 21, 2010 19:42
Show Gist options
  • Select an option

  • Save cpetersen/485008 to your computer and use it in GitHub Desktop.

Select an option

Save cpetersen/485008 to your computer and use it in GitHub Desktop.
module CouchRest
class ExtendedDocument
##########
# Start processing a view and return in 1 sec
# This seems like a hack, is there a better way to implement this?
##########
def self.process_index
r = RestClient::Resource.new("http://#{self.database.server.uri}/#{self.database.name}/#{self.design_doc_id}/_view/#{self.design_doc["views"].keys.first}?limit=0", :timeout => 1)
begin
r.get
rescue RestClient::RequestTimeout => timeout
# Timeout indicates the index is still processing
rescue
# Anything else indicates the index is complete
end
end
##########
# Parse _active_tasks and if the design doc is currently being processed return the percent complete
##########
def self.percent_processed
percent = nil
JSON.parse(RestClient.get("http://#{self.database.server.uri}/_active_tasks")).each do |task|
if(task["type"] == "View Group Indexer")
if(task["task"] == "#{self.database.name} #{self.design_doc_id}")
expression = /Processed (\d+) of (\d+) changes \((.+)\)/
data = expression.match(task["status"])
if data
complete_changes = data[1]
total_changes = data[2]
percent = data[3]
end
end
end
end
percent
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment