Module: HybridPlatformsConductor::CurrentDirMonitor

Defined in:
lib/hybrid_platforms_conductor/current_dir_monitor.rb

Overview

Implement a global monitor to protect accesses to the current directory. This is needed as the OS concept of current directory is not thread-safe: it is linked to a process, and we need to have thread-safe code running (mainly for parallel deployment and tests).

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.monitorObject (readonly)

Returns the value of attribute monitor.



10
11
12
# File 'lib/hybrid_platforms_conductor/current_dir_monitor.rb', line 10

def monitor
  @monitor
end

Class Method Details

.decorate_method(module_to_decorate, method_name) ⇒ Object

Decorate a given method with the monitor.

Parameters
  • module_to_decorate (Module): The module including the method to decorate

  • method_name (Symbol): The method name to be decorated



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hybrid_platforms_conductor/current_dir_monitor.rb', line 20

def self.decorate_method(module_to_decorate, method_name)
  original_method_name = "__hpc__#{method_name}__undecorated__".to_sym
  module_to_decorate.alias_method original_method_name, method_name
  module_to_decorate.define_method(method_name) do |*args, &block|
    result = nil
    CurrentDirMonitor.monitor.synchronize do
      # puts "TID #{Thread.current.object_id} from #{caller[2]} - Current dir monitor taken from #{Dir.pwd}"
      result = self.send(original_method_name, *args, &block)
      # puts "TID #{Thread.current.object_id} from #{caller[2]} - Current dir monitor released back to #{Dir.pwd}"
    end
    result
  end
end