Class: Application

Inherits:
Object
  • Object
show all
Includes:
ClassLogging
Defined in:
lib/unicorn-cuba-base.rb

Instance Method Summary collapse

Methods included from ClassLogging

included, #log, #logger_for

Constructor Details

#initialize(program_name, defaults = {}, &block) ⇒ Application

Returns a new instance of Application.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/unicorn-cuba-base.rb', line 49

def initialize(program_name, defaults = {}, &block)
	instance_eval(&block)

	@cli = setup_cli(program_name, defaults, @cli_setup) or fail 'no cli defined'
	@settings = @settings_setup ? setup_settings(@settings_setup) : @cli.parse!

	root_logger = if @settings.syslog_facility
		# open /dev/null as log file if we are using syslog and we are not in foreground
		@settings.log_file = Pathname.new '/dev/null' unless @settings.foreground
		RootSyslogLogger.new(program_name, @settings.syslog_facility, @settings.foreground)
	else
		RootLogger.new
	end

	root_logger.level = RootLogger::WARN
	root_logger.level = RootLogger::INFO if @settings.verbose
	root_logger.level = RootLogger::DEBUG if @settings.debug
	root_logger.enable_perf_logging if @settings.perf_stats
	Controller.logger = root_logger
	MemoryLimit.logger = Controller.logger_for(MemoryLimit)

	unicorn_settings = {}
	unicorn_settings[:logger] = root_logger.logger_for(Unicorn::HttpServer)
	unicorn_settings[:pid] = @settings.pid_file.to_s
	unicorn_settings[:worker_processes] = @settings.worker_processes
	unicorn_settings[:timeout] = @settings.worker_timeout
	unicorn_settings[:listeners] = @settings.listener
	unicorn_settings[:user] = @settings.user if @settings.user
	unicorn_settings[:rewindable_input] = false # don't keep the upload data in memory or on disk (tmp)
	unicorn_settings[:after_fork] = @after_fork if @after_fork

	unless @settings.foreground
		unicorn_settings[:stderr_path] = @settings.log_file.to_s
		unicorn_settings[:stdout_path] = @settings.log_file.to_s

		Unicorn::Launcher.daemonize!(unicorn_settings)

		# capture startup messages
		@settings.log_file.open('ab') do |log|
			log.sync = true
			STDERR.reopen log
			STDOUT.reopen log
		end
	end

	Controller.settings[:listeners] = @settings.listener
	#Controller.settings[:access_log_file] = @settings.access_log_file

	Controller.plugin Plugin::ErrorMatcher
	Controller.plugin Plugin::Logging
	Controller.plugin Plugin::ResponseHelpers
	Controller.plugin Plugin::MemoryLimit

	self.class.logger = root_logger.logger_for(self.class)
	log.info "#{program_name} starting up; pid: #{Process.pid}"

	@main_setup or fail 'no main controller provided'
	main_controller = setup_main(@main_setup) or fail 'no main controller class returned'

	main_controller.use Rack::ErrorHandling
	main_controller.use Rack::XIDLogging, root_logger, @settings.xid_header if @settings.xid_header
	if @settings.syslog_facility
		main_controller.use Rack::CommonLoggerXID, root_logger.with_meta(type: 'http-access')
	else
		access_log_file = @settings.access_log_file.open('a+')
		access_log_file.sync = true
		main_controller.use Rack::CommonLogger, access_log_file
	end
	main_controller.use Rack::MemoryLimit, @settings.limit_memory * 1024 ** 2
	main_controller.use Rack::UnhandledRequest

	Unicorn::HttpServer.new(main_controller, unicorn_settings).start.join
end

Instance Method Details

#after_fork(&block) ⇒ Object



45
46
47
# File 'lib/unicorn-cuba-base.rb', line 45

def after_fork(&block)
	@after_fork = block
end

#cli(&block) ⇒ Object



33
34
35
# File 'lib/unicorn-cuba-base.rb', line 33

def cli(&block)
	@cli_setup = block
end

#main(&block) ⇒ Object



41
42
43
# File 'lib/unicorn-cuba-base.rb', line 41

def main(&block)
	@main_setup = block
end

#settings(&block) ⇒ Object



37
38
39
# File 'lib/unicorn-cuba-base.rb', line 37

def settings(&block)
	@settings_setup = block
end