Skip to content

Commit

Permalink
Added rake task to rebuild vhash, added HOST option to rebuild fullpa…
Browse files Browse the repository at this point in the history
…th/index for a given site only.
  • Loading branch information
gaspard committed Mar 7, 2011
1 parent 55e2767 commit 672c8d8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
12 changes: 9 additions & 3 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ def anon
@anon ||= User.find_by_id_and_site_id(self[:anon_id], self.id)
end

# Return an admin user, this user is used to rebuild index/vhash/etc.
def any_admin
@any_admin ||= User.find_by_status_and_site_id(User::Status[:admin], self.id)
end

# TODO: test
def root_node
secure(Node) { Node.find(self[:root_id]) }
Expand Down Expand Up @@ -297,7 +302,7 @@ def iformats_updated!
$iformats[self[:id]] = @iformats = nil
end
end

def virtual_classes
@iformats ||= begin
$iformats ||= {} # mem cache
Expand Down Expand Up @@ -359,7 +364,7 @@ def rebuild_vhash(nodes = nil, page = nil, page_count = nil)
end

# Recreates the fullpath ('/zip/zip/zip').
# TODO: find a way to use SiteWorker (need to remove get_nodes)
# TODO: find a way to use SiteWorker (need to remove get_nodes): fix rake when this is done.
def rebuild_fullpath(parent_id = nil, parent_fullpath = "", parent_basepath = "", start=[])
raise Zena::InvalidRecord, "Infinit loop in 'ancestors' (#{start.inspect} --> #{parent_id})" if start.include?(parent_id)
start += [parent_id]
Expand Down Expand Up @@ -400,7 +405,8 @@ def rebuild_fullpath(parent_id = nil, parent_fullpath = "", parent_basepath = ""
# Rebuild property indices for the Site. This method uses the Worker thread to rebuild and works on
# chunks of 50 nodes.
#
# The visitor used during index rebuild is the anonymous user.
# The visitor used during index rebuild should be an admin user (to index
# unpublished templates).
def rebuild_index(nodes = nil, page = nil, page_count = nil)
if !nodes
Site.logger.error("\n----------------- REBUILD INDEX FOR SITE #{host} -----------------\n")
Expand Down
45 changes: 35 additions & 10 deletions lib/tasks/zena.rake
Original file line number Diff line number Diff line change
Expand Up @@ -345,31 +345,56 @@ namespace :zena do
end
end

desc 'Rebuild index for all sites (without SiteWorker)'
desc 'Rebuild index for all sites or site defined by HOST param.'
task :rebuild_index => :environment do
include Zena::Acts::Secure

Site.all.each do |site|
# We avoid SiteWorker because it's async.
Thread.current[:visitor] = User.find_by_login_and_site_id(nil, site.id)
if ENV['HOST']
sites = [Site.find_by_host(ENV['HOST'])]
else
sites = Site.all
end
sites.each do |site|
# We avoid SiteWorker by passing nodes.
Thread.current[:visitor] = site.any_admin
nodes = Node.find(:all,
:conditions => ['site_id = ?', site.id]
)
site.rebuild_index(secure_result(nodes))
end
end

desc 'Rebuild fullpath for all sites (without SiteWorker)'
desc 'Rebuild fullpath for all sites or site defined by HOST param.'
task :rebuild_fullpath => :environment do
include Zena::Acts::Secure

Site.all.each do |site|
# We avoid SiteWorker because it's async.
Thread.current[:visitor] = User.find_by_login_and_site_id(nil, site.id)
if ENV['HOST']
sites = [Site.find_by_host(ENV['HOST'])]
else
sites = Site.all
end
sites.each do |site|
# Does not use SiteWorker.
site.rebuild_fullpath
end
end

desc 'Rebuild vhash for all sites or site defined by HOST param.'
task :rebuild_vhash => :environment do
include Zena::Acts::Secure
if ENV['HOST']
sites = [Site.find_by_host(ENV['HOST'])]
else
sites = Site.all
end
sites.each do |site|
# We avoid SiteWorker by passing nodes.
Thread.current[:visitor] = site.any_admin
nodes = Node.find(:all,
:conditions => ['site_id = ?', site.id]
)
site.rebuild_vhash(secure_result(nodes))
end
end

Rake::RDocTask.new do |rdoc|
files = ['README', 'doc/README_FOR_APP', 'CREDITS', 'MIT-LICENSE', 'app/**/*.rb',
'lib/**/*.rb']
Expand Down
12 changes: 12 additions & 0 deletions test/unit/site_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ def test_anonymous
anon.site = site
assert anon.is_anon?
end

context 'A site' do
subject do
Site.first
end

should 'respond to any_admin' do
assert_kind_of User, subject.any_admin
assert_equal users(:lion), subject.any_admin
end
end # A site


def test_public_group
login(:anon)
Expand Down

0 comments on commit 672c8d8

Please sign in to comment.