Class: Elders::Stack
- Inherits:
-
Object
- Object
- Elders::Stack
- Defined in:
- lib/elders/stack.rb
Instance Attribute Summary collapse
-
#promise ⇒ Object
Returns the value of attribute promise.
-
#tasks ⇒ Object
readonly
Attributes.
Instance Method Summary collapse
-
#clean ⇒ Object
Clean all the stack tasks.
-
#initialize(tasks) ⇒ Stack
constructor
Create a new the stack.
-
#start(params = nil) ⇒ Object
Start.
-
#wait(limit = nil) ⇒ Object
Wait until all the promises to finish.
Constructor Details
#initialize(tasks) ⇒ Stack
Create a new the stack
11 12 13 14 |
# File 'lib/elders/stack.rb', line 11 def initialize(tasks) # Set tasks @tasks = tasks end |
Instance Attribute Details
#promise ⇒ Object
Returns the value of attribute promise.
7 8 9 |
# File 'lib/elders/stack.rb', line 7 def promise @promise end |
#tasks ⇒ Object (readonly)
Attributes
6 7 8 |
# File 'lib/elders/stack.rb', line 6 def tasks @tasks end |
Instance Method Details
#clean ⇒ Object
Clean all the stack tasks
36 37 38 39 40 41 42 |
# File 'lib/elders/stack.rb', line 36 def clean # Each the tasks @tasks.each do |task| # Clean all of them task.clean end end |
#start(params = nil) ⇒ Object
Start
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/elders/stack.rb', line 18 def start(params = nil) # Start the tasks and get it promises promises = @tasks.map do |task| # Start the task task.start params # Get the promise task.promise end # Wait for all the tasks @promise = Concurrent::Promise.all? *promises # Execute the promises @promise.execute end |
#wait(limit = nil) ⇒ Object
Wait until all the promises to finish
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/elders/stack.rb', line 46 def wait(limit = nil) # Define the timeout timeout = Time.now.to_i + limit unless limit.nil? # Success? success = false # Inifiny loop loop do # Timeout? break if !timeout.nil? && Time.now.to_i >= timeout # Set the success success = @promise.fulfilled? # Completed? break if success end # Return the success success end |