Class: Lupe

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

Constant Summary collapse

DEFAULT_CONFIGURATION =
{
  :running     => true,
  :name        => 'lupita',
  :counter     => 0,
  :interval    => 4,
  :monitor     => false,
  :environment => 'development',
  :log_to      => 'log/%s.log',
  :description => '%s - Lupita',
  :print_ticks => false
}

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Lupe

Returns a new instance of Lupe.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lupe.rb', line 16

def initialize(configuration)
  @configuration = DEFAULT_CONFIGURATION
  @configuration.merge!(configuration)
  at_exit do
    logger.info 'Service is going down.'
    logger.info('Running teardown..')
    tear_down
  end
  shutdown = proc do
    puts 'bye.'
    @configuration[:running] = false
  end
  trap('QUIT', &shutdown)
  trap('TERM', &shutdown)
  trap('INT',  &shutdown)
end

Instance Method Details

#_configurationObject



52
53
54
55
56
57
58
# File 'lib/lupe.rb', line 52

def _configuration
  template = "%s : %s"
  bindings = []
  bindings.push("Start with configuration")
  bindings.push(@configuration.to_json)
  (template % bindings)
end

#_reset_logObject



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

def _reset_log
  logger.info 'running on monitor mode'
  File.open(log_path, 'w+') {}
end

#environmentObject



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

def environment
  @configuration[:environment]
end

#log_pathObject



35
36
37
38
39
40
41
42
# File 'lib/lupe.rb', line 35

def log_path
  template = @configuration[:log_to]
  bindings = []
  @configuration[:log_to].scan('%s').each do |interpolation|
    bindings.push(@configuration[:name])
  end
  (template % bindings)
end

#loggerObject



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

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

#program_nameObject



46
47
48
49
50
51
# File 'lib/lupe.rb', line 46

def program_name
  template = @configuration[:description]
  bindings = []
  bindings.push(@configuration[:name])
  (template % bindings)
end

#run(&operation) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lupe.rb', line 67

def run(&operation)
  logger.info(program_name)
  logger.info(_configuration)
  logger.info('Running setup..')
  setup
  loop do
    break unless @configuration[:running]
    sleep 1
    _reset_log if @configuration[:monitor]
    @configuration[:counter] += 1
    if @configuration[:counter] > @configuration[:interval]
      @configuration[:counter] = 0
      operation.call(logger)
      next
    end
    if @configuration[:print_ticks]
      logger.debug "tick: #{@configuration[:counter]}"
    end
  end
end

#setupObject



63
64
# File 'lib/lupe.rb', line 63

def setup
end

#tear_downObject



65
66
# File 'lib/lupe.rb', line 65

def tear_down
end