Class: Masamune::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/masamune/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Environment



35
36
37
# File 'lib/masamune/environment.rb', line 35

def initialize(parent = nil)
  self.parent = parent
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



33
34
35
# File 'lib/masamune/environment.rb', line 33

def catalog
  @catalog
end

#filesystemObject

Returns the value of attribute filesystem.



32
33
34
# File 'lib/masamune/environment.rb', line 32

def filesystem
  @filesystem
end

#parentObject

Returns the value of attribute parent.



31
32
33
# File 'lib/masamune/environment.rb', line 31

def parent
  @parent
end

Instance Method Details

#configurationObject



47
48
49
# File 'lib/masamune/environment.rb', line 47

def configuration
  @configuration ||= Masamune::Configuration.new(environment: self)
end

#configure {|configuration| ... } ⇒ Object

Yields:



43
44
45
# File 'lib/masamune/environment.rb', line 43

def configure
  yield configuration
end

#console(*a) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/masamune/environment.rb', line 103

def console(*a)
  line = a.join(' ').chomp
  mutex.synchronize do
    logger.info(line)
    $stdout.puts line unless configuration.quiet || configuration.debug
    $stdout.flush
    $stderr.flush
  end
end

#hive_helperObject



136
137
138
# File 'lib/masamune/environment.rb', line 136

def hive_helper
  @hive_helper ||= Masamune::Helpers::Hive.new(self)
end

#log_file_nameObject



94
95
96
97
# File 'lib/masamune/environment.rb', line 94

def log_file_name
  return unless filesystem.path?(:log_dir)
  @log_file_name ||= filesystem.get_path(:log_dir, log_file_template)
end

#log_file_templateObject



81
82
83
# File 'lib/masamune/environment.rb', line 81

def log_file_template
  @log_file_template || "#{Time.now.to_i}-#{$PROCESS_ID}.log"
end

#log_file_template=(log_file_template) ⇒ Object



85
86
87
88
# File 'lib/masamune/environment.rb', line 85

def log_file_template=(log_file_template)
  @log_file_template = log_file_template
  reload_logger!
end

#loggerObject



99
100
101
# File 'lib/masamune/environment.rb', line 99

def logger
  @logger ||= Logger.new(log_file_io)
end

#mutexObject



51
52
53
# File 'lib/masamune/environment.rb', line 51

def mutex
  @mutex ||= Mutex.new
end

#postgres_helperObject



140
141
142
# File 'lib/masamune/environment.rb', line 140

def postgres_helper
  @postgres_helper ||= Masamune::Helpers::Postgres.new(self)
end

#reload_logger!Object



90
91
92
# File 'lib/masamune/environment.rb', line 90

def reload_logger!
  @logger = @log_file_name = nil
end

#trace(*a) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/masamune/environment.rb', line 113

def trace(*a)
  line = a.join(' ').chomp
  mutex.synchronize do
    logger.info(line)
    $stdout.puts line if configuration.verbose && !configuration.debug
    $stdout.flush
    $stderr.flush
  end
end

#versionObject



39
40
41
# File 'lib/masamune/environment.rb', line 39

def version
  "masamune #{Masamune::VERSION}"
end

#with_exclusive_lock(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/masamune/environment.rb', line 55

def with_exclusive_lock(name)
  raise 'filesystem path :run_dir not defined' unless filesystem.path?(:run_dir)
  lock_name = [name, configuration.lock].compact.join(':')
  logger.debug("acquiring lock '#{lock_name}'")
  lock_file = lock_file(lock_name)
  lock_mode = File::LOCK_EX
  lock_mode |= File::LOCK_NB
  lock_status = lock_file.flock(lock_mode)
  if lock_status && lock_status.zero?
    yield if block_given?
  else
    logger.error "acquire lock attempt failed for '#{lock_name}'"
  end
ensure
  if lock_file && lock_status && lock_status.zero?
    logger.debug("releasing lock '#{lock_name}'")
    lock_file.flock(File::LOCK_UN)
  end
end

#with_process_lock(name) ⇒ Object



75
76
77
78
79
# File 'lib/masamune/environment.rb', line 75

def with_process_lock(name)
  with_exclusive_lock("#{name}_#{Process.pid}") do
    yield
  end
end