Class: Async::Node
- Inherits:
- 
      Object
      
        - Object
- Async::Node
 
- Defined in:
- lib/async/node.rb
Overview
A node in a tree, used for implementing the task hierarchy.
Instance Attribute Summary collapse
- #A useful identifier for the current node.(usefulidentifier) ⇒ Object readonly
- 
  
    
      #annotation  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute annotation. 
- 
  
    
      #children  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute children. 
- #head ⇒ Object
- #Optional list of children.(listofchildren.) ⇒ Object readonly
- 
  
    
      #parent  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute parent. 
- #tail ⇒ Object
Instance Method Summary collapse
- 
  
    
      #annotate(annotation)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Annotate the node with a description. 
- 
  
    
      #backtrace(*arguments)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Provides a backtrace for nodes that have an active execution context. 
- 
  
    
      #children?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Whether this node has any children. 
- 
  
    
      #consume  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    If the node has a parent, and is #finished?, then remove this node from the parent. 
- 
  
    
      #description  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    A description of the node, including the annotation and object name. 
- 
  
    
      #finished?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Whether the node can be consumed (deleted) safely. 
- 
  
    
      #initialize(parent = nil, annotation: nil, transient: false)  ⇒ Node 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Create a new node in the tree. 
- 
  
    
      #print_hierarchy(out = $stdout, backtrace: true)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Print the hierarchy of the task tree from the given node. 
- #root ⇒ Object
- 
  
    
      #stop(later = false)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Attempt to stop the current node immediately, including all non-transient children. 
- 
  
    
      #stopped?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Whether the node has been stopped. 
- 
  
    
      #terminate  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Immediately terminate all children tasks, including transient tasks. 
- #The parent node.=(parentnode. = (value)) ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- 
  
    
      #transient=(value)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Change the transient state of the node. 
- 
  
    
      #transient?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Represents whether a node is transient. 
- 
  
    
      #traverse(&block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Traverse the task tree. 
Constructor Details
#initialize(parent = nil, annotation: nil, transient: false) ⇒ Node
Create a new node in the tree.
| 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # File 'lib/async/node.rb', line 73 def initialize(parent = nil, annotation: nil, transient: false) @parent = nil @children = nil @annotation = annotation @object_name = nil @transient = transient @head = nil @tail = nil if parent parent.add_child(self) end end | 
Instance Attribute Details
#A useful identifier for the current node.(usefulidentifier) ⇒ Object (readonly)
| 108 | # File 'lib/async/node.rb', line 108 attr :annotation | 
#annotation ⇒ Object (readonly)
Returns the value of attribute annotation.
| 108 109 110 | # File 'lib/async/node.rb', line 108 def annotation @annotation end | 
#children ⇒ Object (readonly)
Returns the value of attribute children.
| 105 106 107 | # File 'lib/async/node.rb', line 105 def children @children end | 
#head ⇒ Object
| 96 97 98 | # File 'lib/async/node.rb', line 96 def head @head end | 
#Optional list of children.(listofchildren.) ⇒ Object (readonly)
| 105 | # File 'lib/async/node.rb', line 105 attr :children | 
#parent ⇒ Object
Returns the value of attribute parent.
| 102 103 104 | # File 'lib/async/node.rb', line 102 def parent @parent end | 
#tail ⇒ Object
| 99 100 101 | # File 'lib/async/node.rb', line 99 def tail @tail end | 
Instance Method Details
#annotate(annotation) ⇒ Object
Annotate the node with a description.
| 142 143 144 145 146 147 148 149 150 151 152 153 154 | # File 'lib/async/node.rb', line 142 def annotate(annotation) if block_given? begin current_annotation = @annotation @annotation = annotation return yield ensure @annotation = current_annotation end else @annotation = annotation end end | 
#backtrace(*arguments) ⇒ Object
Provides a backtrace for nodes that have an active execution context.
| 174 175 176 | # File 'lib/async/node.rb', line 174 def backtrace(*arguments) nil end | 
#children? ⇒ Boolean
Whether this node has any children.
| 112 113 114 | # File 'lib/async/node.rb', line 112 def children? @children && !@children.empty? end | 
#consume ⇒ Object
If the node has a parent, and is #finished?, then remove this node from the parent.
| 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | # File 'lib/async/node.rb', line 228 def consume if parent = @parent and finished? parent.remove_child(self) # If we have children, then we need to move them to our the parent if they are not finished: if @children while child = @children.shift if child.finished? child.set_parent(nil) else parent.add_child(child) end end @children = nil end parent.consume end end | 
#description ⇒ Object
A description of the node, including the annotation and object name.
| 159 160 161 162 163 164 165 166 167 168 169 | # File 'lib/async/node.rb', line 159 def description @object_name ||= "#{self.class}:#{format '%#018x', object_id}#{@transient ? ' transient' : nil}" if annotation = self.annotation "#{@object_name} #{annotation}" elsif line = self.backtrace(0, 1)&.first "#{@object_name} #{line}" else @object_name end end | 
#finished? ⇒ Boolean
Whether the node can be consumed (deleted) safely. By default, checks if the children set is empty.
| 222 223 224 | # File 'lib/async/node.rb', line 222 def finished? @children.nil? || @children.finished? end | 
#print_hierarchy(out = $stdout, backtrace: true) ⇒ Object
Print the hierarchy of the task tree from the given node.
| 304 305 306 307 308 309 310 311 312 | # File 'lib/async/node.rb', line 304 def print_hierarchy(out = $stdout, backtrace: true) self.traverse do |node, level| indent = "\t" * level out.puts "#{indent}#{node}" print_backtrace(out, indent, node) if backtrace end end | 
#stop(later = false) ⇒ Object
Attempt to stop the current node immediately, including all non-transient children. Invokes #stop_children to stop all children.
| 283 284 285 286 | # File 'lib/async/node.rb', line 283 def stop(later = false) # The implementation of this method may defer calling `stop_children`. stop_children(later) end | 
#stopped? ⇒ Boolean
Whether the node has been stopped.
| 296 297 298 | # File 'lib/async/node.rb', line 296 def stopped? @children.nil? end | 
#terminate ⇒ Object
Immediately terminate all children tasks, including transient tasks. Internally invokes ‘stop(false)` on all children. This should be considered a last ditch effort and is used when closing the scheduler.
| 268 269 270 271 272 273 274 275 276 277 278 | # File 'lib/async/node.rb', line 268 def terminate # Attempt to stop the current task immediately, and all children: stop(false) # If that doesn't work, take more serious action: @children&.each do |child| child.terminate end return @children.nil? end | 
#The parent node.=(parentnode. = (value)) ⇒ Object
| 102 | # File 'lib/async/node.rb', line 102 attr :parent | 
#to_s ⇒ Object Also known as: inspect
| 179 180 181 | # File 'lib/async/node.rb', line 179 def to_s "\#<#{self.description}>" end | 
#transient=(value) ⇒ Object
Change the transient state of the node.
A transient node is not considered when determining if a node is finished, and propagates up if the parent is consumed.
| 131 132 133 134 135 136 137 | # File 'lib/async/node.rb', line 131 def transient=(value) if @transient != value @transient = value @parent&.children&.adjust_transient_count(value) end end | 
#transient? ⇒ Boolean
Represents whether a node is transient. Transient nodes are not considered when determining if a node is finished. This is useful for tasks which are internal to an object rather than explicit user concurrency. For example, a child task which is pruning a connection pool is transient, because it is not directly related to the parent task, and should not prevent the parent task from finishing.
| 122 123 124 | # File 'lib/async/node.rb', line 122 def transient? @transient end | 
#traverse(&block) ⇒ Object
Traverse the task tree.
| 253 254 255 256 257 | # File 'lib/async/node.rb', line 253 def traverse(&block) return enum_for(:traverse) unless block_given? self.traverse_recurse(&block) end |