Created
July 30, 2010 09:39
-
-
Save roidrage/500240 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
# Script to automate view and database compaction | |
# Based on CouchPotato and for use in a Rails project. | |
# Will only compact one view at a time to keep the load | |
# | |
# Installation: Drop into bin/ or script/ in your project. | |
# | |
# Usage: RAILS_ENV=development ./bin/couchdb_compact.rb | |
# | |
# Courtesy of your friendly folks at http://scalarium.com | |
RAILS_ENV = ENV["RAILS_ENV"] || "production" | |
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") | |
require 'rubygems' | |
require 'json' | |
require 'restclient' | |
database = CouchPotato.database.send(:database) | |
$couch_host = database.host | |
$couch_root = database.root | |
$couch_database = database.name | |
def wait_for_active_tasks | |
view_compaction_active = false | |
while !view_compaction_active | |
RestClient.get("#{$couch_host}/_active_tasks") do |response, request| | |
if task = JSON.load(response.body).find{|task| task["type"] == "View Group Compaction" && task["task"].starts_with?($couch_database)} | |
puts "Task active (#{task["task"]}#{task["status"].blank? ? ": no status message" : ": #{task["status"]}"}), waiting for it to finish" | |
sleep 1 | |
else | |
view_compaction_active = true | |
end | |
end | |
end | |
end | |
design_docs_uri = "#{$couch_root}/_all_docs?startkey=%22_design%22&endkey=%22_design0%22" | |
RestClient.get(design_docs_uri, {:accept => 'application/json', :content_type => 'application/json'}) do |response, request| | |
design_docs = JSON.load(response.body) | |
design_docs["rows"].each do |doc| | |
design_doc = doc["id"].split("/").last | |
design_doc_compact_uri = "#{$couch_root}/_compact/#{design_doc}" | |
puts "Rebuilding view #{$couch_database}/#{doc["id"]}" | |
RestClient.post(design_doc_compact_uri, '', :content_type => :json) | |
wait_for_active_tasks | |
end | |
end | |
puts "Compacting database #{$couch_database}" | |
RestClient.post("#{$couch_root}/_compact", "", :content_type => :json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment