Module: WithLock::Common

Included in:
Client, Server
Defined in:
lib/with_lock.rb

Instance Method Summary collapse

Instance Method Details

#default_settingsObject



71
72
73
74
75
76
77
# File 'lib/with_lock.rb', line 71

def default_settings
  {
    'directory' => File.join('tmp'),
    'url' => "druby://localhost:9999",
    'scope' => "#{File.basename(File.expand_path('.'))}#{":#{Rails.env}" if defined?(Rails)}"
  }
end

#load_settings(filename) ⇒ Object



79
80
81
# File 'lib/with_lock.rb', line 79

def load_settings(filename)
  YAML.load(ERB.new(File.read(filename)).result)
end

#local_settings_filenameObject



59
60
61
# File 'lib/with_lock.rb', line 59

def local_settings_filename
  File.join('config','with_lock.local.yml')
end

#loggerObject



29
30
31
32
33
34
35
# File 'lib/with_lock.rb', line 29

def logger
  return @@logger if defined?(@@logger) && !@@logger.nil?
  @@logger = nil
  @@logger = Rails.logger if defined?(Rails)
  FileUtils.mkdir_p('log') if @@logger.nil?
  @@logger ||= Logger.new(File.join('log','with_lock.log'))
end

#pidObject



45
46
47
48
49
# File 'lib/with_lock.rb', line 45

def pid
  File.read(pidfile).strip
rescue => e
  nil
end

#pidfileObject



41
42
43
# File 'lib/with_lock.rb', line 41

def pidfile
  File.join('tmp','pids','with_lock.pid')
end

#running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


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

def running?(pid)
  `ps -p#{pid} | wc -l`.to_i == 2
end

#settingsObject



63
64
65
66
67
68
69
# File 'lib/with_lock.rb', line 63

def settings
  return @@settings if defined? @@settings
  @@settings = default_settings
  @@settings = @@settings.merge!(load_settings(settings_filename)||{}) if File.exists?(settings_filename)
  @@settings.merge!(load_settings(local_settings_filename)||{}) if File.exists?(local_settings_filename)
  @@settings
end

#settings_filenameObject



55
56
57
# File 'lib/with_lock.rb', line 55

def settings_filename
  File.join('config','with_lock.yml')
end

#urlObject



37
38
39
# File 'lib/with_lock.rb', line 37

def url
  settings['url']
end