Class: Elders::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/elders/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks) ⇒ Stack

Create a new the stack

Parameters:

  • tasks (Array<Task>)
    • The tasks from the stack



11
12
13
14
# File 'lib/elders/stack.rb', line 11

def initialize(tasks)
  # Set tasks
  @tasks = tasks
end

Instance Attribute Details

#promiseObject

Returns the value of attribute promise.



7
8
9
# File 'lib/elders/stack.rb', line 7

def promise
  @promise
end

#tasksObject (readonly)

Attributes



6
7
8
# File 'lib/elders/stack.rb', line 6

def tasks
  @tasks
end

Instance Method Details

#cleanObject

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

Parameters:

  • params (String) (defaults to: nil)
    • Params used by all the tasks



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

Parameters:

  • limit (Integer) (defaults to: nil)
    • Limit of waiting in seconds



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