class TinyConveyor::Belt

Thread manager, allow creation and execution of parcels

Attributes

parcels[R]

Public Class Methods

new() click to toggle source

@return [Belt] newly created belt

Calls superclass method
# File lib/tiny_conveyor/belt.rb, line 11
def initialize
  super()
  @parcels = []
  @threads = []
  @state = :waiting
end

Public Instance Methods

add_parcel(name, description, task) click to toggle source

@param name [string] name of the task @param description [string] small description of the task @param action [Proc] action to run in separate thread @return [Array<Parcel>] pending parcels

# File lib/tiny_conveyor/belt.rb, line 22
def add_parcel(name, description, task)
  parcel = Parcel.new(name, description, task)
  @parcels << parcel
end
remove_parcel(parcel) click to toggle source
# File lib/tiny_conveyor/belt.rb, line 49
def remove_parcel(parcel)
  new_parcels_array = @parcels.reject { |p| p == parcel }
  @parcels = new_parcels_array
end
remove_parcel_by_uuid(uuid) click to toggle source

@param uuid [string] uuid of the parcel to remove @return [Array<Parcel>] pending parcels

# File lib/tiny_conveyor/belt.rb, line 29
def remove_parcel_by_uuid(uuid)
  parcel = @parcels.find { |p| p.uuid == uuid }
  return @parcels if parcel.nil?

  remove_parcel(parcel)
end
run_belt_entirely() click to toggle source
# File lib/tiny_conveyor/belt.rb, line 40
def run_belt_entirely
  execute until @parcels.empty?
end
running?() click to toggle source

@return [boolean] true if the belt is running, false otherwhise

# File lib/tiny_conveyor/belt.rb, line 45
def running?
  @state == :running
end
start_belt() click to toggle source
# File lib/tiny_conveyor/belt.rb, line 36
def start_belt
  self.async.run_belt_entirely
end

Private Instance Methods

execute() click to toggle source
# File lib/tiny_conveyor/belt.rb, line 56
def execute
  return true if running?

  tasks = @parcels.clone
  @state = :running
  until tasks.empty?
    current_parcel = tasks.shift
    current_parcel&.execute
    @parcels.shift
  end
  @state = :waiting
end