Module: HotPotato::Core

Included in:
AppTaskInfo, Admin, AppTask, AppTaskServer, Cache, QueueLogger, Route::AppTask, SupervisorServer, SupervisorInfo
Defined in:
lib/hot_potato/core.rb

Constant Summary collapse

CONFIG_PATH =
"#{APP_PATH}/config/config.yml"

Instance Method Summary collapse

Instance Method Details

#acquire_lock(key, duration = 10) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/hot_potato/core.rb', line 46

def acquire_lock(key, duration = 10)
  if !stat.getset("hotpotato.lock.#{key.to_s}", "1")
    stat.expire "hotpotato.lock.#{key.to_s}", duration
    return true
  else
    stat.expire "hotpotato.lock.#{key.to_s}", duration
    return false
  end
end

#configObject



24
25
26
# File 'lib/hot_potato/core.rb', line 24

def config
  @@config ||= YAML.load_file(CONFIG_PATH)[RACK_ENV]
end

#logObject



18
19
20
21
22
# File 'lib/hot_potato/core.rb', line 18

def log
  @@log ||= Logger.new(STDOUT)
  @@log.level = Logger::INFO
  return @@log
end

#queue_inject(name, message) ⇒ Object



32
33
34
35
# File 'lib/hot_potato/core.rb', line 32

def queue_inject(name, message)
  @@queue ||= Redis.new :host => config['redis_hostname'], :port => config['redis_port']
  @@queue.publish name.to_sym, Snappy.deflate(message.to_json)
end

#queue_subscribe(name, &block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/hot_potato/core.rb', line 37

def queue_subscribe(name, &block) 
  queue ||= Redis.new :host => config['redis_hostname'], :port => config['redis_port']
  queue.subscribe(*name) do |on|
    on.message do |channel, message|
      yield JSON.parse(Snappy.inflate(message))
    end
  end
end

#release_lock(key) ⇒ Object



56
57
58
# File 'lib/hot_potato/core.rb', line 56

def release_lock(key)
  stat.del("hotpotato.lock.#{key.to_s}")
end

#set_logger(provider, options = {}) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/hot_potato/core.rb', line 10

def set_logger(provider, options = {})
  if provider == :queue_logger
    @@log ||= QueueLogger.new(options)
  else
    @@log ||= Logger.new(STDOUT)
  end
end

#statObject



28
29
30
# File 'lib/hot_potato/core.rb', line 28

def stat
  @@cache ||= Cache.new
end