Sha256: 0531f0bb5fd08709adcc10b68526335e750ee336f8c1ca778170c278cd83a714

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

module Zena
  class SiteWorker < Struct.new(:site_id, :action, :page)
    include Zena::Acts::Secure

    # Execute operations on 250 nodes at a time
    CHUNK_SIZE = 250

    def self.perform(site, action, page = 1)
      action = new(site.id, action, page)

      if Bricks::CONFIG['worker']
        Delayed::Job.enqueue action
      else
        # No worker: do it now
        action.perform(site)
      end
    end

    def perform(site = nil)
      if site.nil?
        site ||= Site.find(site_id)
        Thread.current[:visitor] = site.any_admin
      end

      if page.nil?
        site.send(action)
      else
        if nodes = get_nodes
          # Register next one (if we are lucky and have many workers, we can parallelize work)
          Zena::SiteWorker.perform(site, action, page + 1)

          # do action on nodes
          site.send(action, nodes, page, page_count)
        end
      end
    end

    def get_nodes
      nodes = Node.find(:all,
        :conditions => ['site_id = ?', site_id],
        :limit  => CHUNK_SIZE,
        :offset => (page - 1) * CHUNK_SIZE,
        :order => 'id DESC'
      )
      secure_result(nodes)
    end

    def page_count
      (Node.count(:conditions => ['site_id = ?', site_id]) / CHUNK_SIZE) + 1
    end

    # Return a textual description of the operation.
    def info
      if site_id == current_site.id
        "#{action}, #{_('page')} #{page}/#{page_count}"
      else
        # Do not show jobs from other sites
        "-"
      end
    end
  end
end # Zena

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
zena-1.2.2 lib/zena/site_worker.rb
zena-1.2.1 lib/zena/site_worker.rb
zena-1.2.0 lib/zena/site_worker.rb
zena-1.1.3 lib/zena/site_worker.rb
zena-1.1.2 lib/zena/site_worker.rb
zena-1.1.1 lib/zena/site_worker.rb
zena-1.1.0 lib/zena/site_worker.rb