Class: Zena::SiteWorker

Inherits:
Struct
  • Object
show all
Includes:
Acts::Secure
Defined in:
lib/zena/site_worker.rb

Constant Summary collapse

CHUNK_SIZE =

Execute operations on 250 nodes at a time

250

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Acts::Secure

#secure_scope, #secure_write_scope, #visitor=

Methods included from Acts::Secure::SecureResult

#construct_id_map, #secure_result

Instance Attribute Details

#actionObject

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



2
3
4
# File 'lib/zena/site_worker.rb', line 2

def action
  @action
end

#pageObject

Returns the value of attribute page

Returns:

  • (Object)

    the current value of page



2
3
4
# File 'lib/zena/site_worker.rb', line 2

def page
  @page
end

#site_idObject

Returns the value of attribute site_id

Returns:

  • (Object)

    the current value of site_id



2
3
4
# File 'lib/zena/site_worker.rb', line 2

def site_id
  @site_id
end

Class Method Details

.perform(site, action, page = 1) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/zena/site_worker.rb', line 8

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

Instance Method Details

#get_nodesObject



45
46
47
48
49
50
51
52
53
# File 'lib/zena/site_worker.rb', line 45

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

#infoObject

Return a textual description of the operation.



60
61
62
63
64
65
66
67
# File 'lib/zena/site_worker.rb', line 60

def info
  if site_id == current_site.site_id
    "#{action}, #{_('page')} #{page}/#{page_count}"
  else
    # Do not show jobs from other sites
    "-"
  end
end

#page_countObject



55
56
57
# File 'lib/zena/site_worker.rb', line 55

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

#perform(site = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zena/site_worker.rb', line 19

def perform(site = nil)
  if site.nil?
    site ||= Site.find(site_id)
    setup_visitor(site.any_admin, site)
  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
      begin
        site.send(action, nodes, page, page_count)
      rescue => err
        # If we let the action fail, it will rerun and we will recreate an action for page + 1 !
        Site.logger.warn "[JOB] Failed: '#{action}'"
        Site.logger.warn err.message
      end
      
    end
  end
end