Class: Cumuli::App

Inherits:
Object
  • Object
show all
Defined in:
lib/cumuli/app/app.rb,
lib/cumuli/app/procs.rb,
lib/cumuli/app/stdout_logger.rb,
lib/cumuli/app/foreman_process.rb

Defined Under Namespace

Classes: ForemanProcess, Procs, StdoutLogger

Constant Summary collapse

DEFAULT_WAIT_TIME =

120

5
SIGNALS =
['TERM', 'INT', 'HUP']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



8
9
10
11
12
13
# File 'lib/cumuli/app/app.rb', line 8

def initialize(options={})
  @env = options[:env]
  @wait_time = options[:wait_time]
  @log_dir = options[:log_dir] || "#{Dir.pwd}/log"
  @app_dir = options[:app_dir] || Dir.pwd
end

Instance Attribute Details

#app_dirObject

Returns the value of attribute app_dir.



6
7
8
# File 'lib/cumuli/app/app.rb', line 6

def app_dir
  @app_dir
end

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/cumuli/app/app.rb', line 6

def env
  @env
end

#log_dirObject

Returns the value of attribute log_dir.



6
7
8
# File 'lib/cumuli/app/app.rb', line 6

def log_dir
  @log_dir
end

#wait_timeObject

Returns the value of attribute wait_time.



6
7
8
# File 'lib/cumuli/app/app.rb', line 6

def wait_time
  @wait_time
end

Instance Method Details

#app_portsObject



54
55
56
# File 'lib/cumuli/app/app.rb', line 54

def app_ports
  apps.map.values.compact
end

#appsObject



34
35
36
# File 'lib/cumuli/app/app.rb', line 34

def apps
  @apps ||= Procs.new(app_dir)
end

#foreman_processObject



30
31
32
# File 'lib/cumuli/app/app.rb', line 30

def foreman_process
  @termial ||= ForemanProcess.new(env, log_dir)
end

#listen_for_signalsObject



46
47
48
49
50
51
52
# File 'lib/cumuli/app/app.rb', line 46

def listen_for_signals
  SIGNALS.each do |signal|
    trap(signal) do
      stop
    end
  end
end

#loggerObject



26
27
28
# File 'lib/cumuli/app/app.rb', line 26

def logger
  @logger = StdoutLogger.new
end

#open_socket(port) ⇒ Object



77
78
79
80
81
82
# File 'lib/cumuli/app/app.rb', line 77

def open_socket(port)
  TCPSocket.new('localhost', port)
  true
rescue Errno::ECONNREFUSED
  false
end

#pidObject



38
39
40
# File 'lib/cumuli/app/app.rb', line 38

def pid
  foreman_process.pid
end

#startObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/cumuli/app/app.rb', line 15

def start
  return if foreman_process.started?

  Dir.chdir(app_dir) do 
    listen_for_signals
    logger.print "Starting ..."
    foreman_process.start
    wait_for_apps if wait?
  end
end

#stopObject



84
85
86
87
88
89
# File 'lib/cumuli/app/app.rb', line 84

def stop
  if foreman_process.started?
    foreman_process.stop
    @foreman_process = nil
  end
end

#wait?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cumuli/app/app.rb', line 42

def wait?
  wait_time && wait_time > 0
end

#wait_for_app(port) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/cumuli/app/app.rb', line 66

def wait_for_app(port)
  logger.print "waiting for apps on port: #{port}"
  timeout = wait_time || DEFAULT_WAIT_TIME
  Waiter.new("Application on port #{port} unavailable after #{timeout} seconds")
    .wait_until(timeout) { open_socket(port) }
  logger.print "Application on #{port} available"
rescue Exception => e
  stop
  raise e
end

#wait_for_appsObject



58
59
60
61
62
63
64
# File 'lib/cumuli/app/app.rb', line 58

def wait_for_apps
  app_ports.each do |port|
    wait_for_app(port)
  end

  logger.add_space
end