Class: ResqueJobsTree::Node

Inherits:
Object
  • Object
show all
Includes:
Storage::Node
Defined in:
lib/resque_jobs_tree/node.rb

Constant Summary

Constants included from Storage

Storage::LAUNCHED_TREES, Storage::PARENTS_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Storage::Node

#childs_key, #cleanup, #exists?, #key, #lock_key, #only_stored_child?, #parent, #store, #stored_childs, #unstore

Constructor Details

#initialize(definition, resources, parent = nil, tree = nil) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
13
# File 'lib/resque_jobs_tree/node.rb', line 7

def initialize definition, resources, parent=nil, tree=nil
  @childs     = []
  @definition = definition
  @resources  = resources
  @parent     = parent
  @tree       = tree
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



5
6
7
# File 'lib/resque_jobs_tree/node.rb', line 5

def definition
  @definition
end

#resourcesObject (readonly)

Returns the value of attribute resources.



5
6
7
# File 'lib/resque_jobs_tree/node.rb', line 5

def resources
  @resources
end

#treeObject (readonly)

Returns the value of attribute tree.



5
6
7
# File 'lib/resque_jobs_tree/node.rb', line 5

def tree
  @tree
end

Instance Method Details

#after_performObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/resque_jobs_tree/node.rb', line 27

def after_perform
  run_callback :after_perform
  if root?
    tree.finish
  else
    lock do
      parent.enqueue if only_stored_child?
      unstore
    end
  end
end

#before_performObject



23
24
25
# File 'lib/resque_jobs_tree/node.rb', line 23

def before_perform
  run_callback :before_perform
end

#childsObject



69
70
71
72
# File 'lib/resque_jobs_tree/node.rb', line 69

def childs
  return @childs unless @childs.empty?
  @childs = definition.leaf? ?  [] : definition.childs.call(*resources)
end

#enqueueObject



15
16
17
# File 'lib/resque_jobs_tree/node.rb', line 15

def enqueue
  Resque.enqueue_to definition.tree.name, ResqueJobsTree::Job, *argumentize
end

#inspectObject



86
87
88
# File 'lib/resque_jobs_tree/node.rb', line 86

def inspect
  "<ResqueJobsTree::Node @name=#{name} @resources=#{resources}>"
end

#leaf?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/resque_jobs_tree/node.rb', line 57

def leaf?
  childs.empty?
end

#nameObject



53
54
55
# File 'lib/resque_jobs_tree/node.rb', line 53

def name
  definition.name
end

#on_failureObject



39
40
41
42
43
44
45
46
47
# File 'lib/resque_jobs_tree/node.rb', line 39

def on_failure
  if definition.options[:continue_on_failure]
    run_callback :on_failure
    after_perform
  else
    root.tree.on_failure
    root.cleanup
  end
end

#performObject



19
20
21
# File 'lib/resque_jobs_tree/node.rb', line 19

def perform
  definition.perform.call *resources
end

#registerObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/resque_jobs_tree/node.rb', line 74

def register
  store unless root?
  if leaf?
    tree.register_node self
  else
    childs.each do |node_name, *resources|
      node = definition.find(node_name).spawn resources, self
      node.register
    end
  end
end

#rootObject



65
66
67
# File 'lib/resque_jobs_tree/node.rb', line 65

def root
  @root ||= root? ? self : parent.root
end

#root?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/resque_jobs_tree/node.rb', line 61

def root?
  definition.root?
end