Module: Deployinator::Helpers::ConcurrencyHelpers

Extended by:
Celluloid
Defined in:
lib/deployinator/helpers/concurrency.rb

Defined Under Namespace

Classes: DuplicateReferenceError

Constant Summary collapse

@@futures =

Hash of future objects that have been instantiated so far

{}

Instance Method Summary collapse

Instance Method Details

#get_value(future, timeout = nil) ⇒ Object

Public: returns the value of the code block execution This also sends all the logged data in stream back to main log file and removes the temporary log file created for the thread Returns the return value of the last line executed in block



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/deployinator/helpers/concurrency.rb', line 43

def get_value(future, timeout=nil)
  if @filename
    file_path = "#{RUN_LOG_PATH}" + runlog_thread_filename(future)
    return_value = @@futures[future.to_sym].value(timeout)
    log_and_stream File.read(file_path) 
    File.delete(file_path) if File.exists?(file_path)
    return_value
  else
    @@futures[future.to_sym].value 
  end
end

#get_values(*futures) ⇒ Object

Public: returns the values of the specified futures

Returns hash of values



58
59
60
61
62
63
64
# File 'lib/deployinator/helpers/concurrency.rb', line 58

def get_values(*futures)
  value_hash = {}
  futures.each do |future|
    value_hash[future] = get_value(future)
  end
  value_hash
end

#reference_taken?(name) ⇒ Boolean

Public: check if the reference name for future is taken

Returns boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/deployinator/helpers/concurrency.rb', line 35

def reference_taken?(name)
  return @@futures.has_key?(name)
end

#run_parallel(name, &block) ⇒ Object

Public: run block of code in parallel

Returns Handle to future object created



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/deployinator/helpers/concurrency.rb', line 13

def run_parallel(name, &block)
  name = name.to_sym
  if reference_taken? name
    raise DuplicateReferenceError, "Name #{name} already taken for future."
  end
  log_and_stream '</br>Queueing  execution of future: ' + name.to_s + '</br>'
  @@futures[name] = Celluloid::Future.new do
    # Set filename for thread
    runlog_filename(name)
    # setting up separate logger
    log_and_stream '</br>Starting execution of future: ' + name.to_s + '</br>'
    block.call
  end
  @@futures[name]
end