Class: Guard::Falcon::Controller

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/falcon/controller.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
	config: 'config.ru',
	concurrency: 2,
	verbose: false,
}

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Controller

Returns a new instance of Controller.



44
45
46
47
48
49
50
# File 'lib/guard/falcon/controller.rb', line 44

def initialize(**options)
	super
	
	@options = DEFAULT_OPTIONS.merge(options)
	@endpoint = nil
	@container = nil
end

Instance Method Details

#endpointObject



69
70
71
# File 'lib/guard/falcon/controller.rb', line 69

def endpoint
	@endpoint ||= build_endpoint
end

#loggerObject



53
54
55
# File 'lib/guard/falcon/controller.rb', line 53

def logger
	Compat::UI
end

#reloadObject



109
110
111
112
# File 'lib/guard/falcon/controller.rb', line 109

def reload
	stop
	start
end

#run_on_change(paths) ⇒ Object



121
122
123
# File 'lib/guard/falcon/controller.rb', line 121

def run_on_change(paths)
	reload
end

#run_serverObject



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
# File 'lib/guard/falcon/controller.rb', line 73

def run_server
	shared_endpoint = Async::Reactor.run do
		Async::IO::SharedEndpoint.bound(endpoint)
	end.result
	
	logger.info("Starting Falcon HTTP server on #{endpoint}.")
	
	container = Async::Container::Forked.new(concurrency: @options[:concurrency]) do
		begin
			rack_app, options = Rack::Builder.parse_file(@options[:config])
		rescue
			logger.error "Failed to load #{@options[:config]}: #{$!}"
			logger.error $!.backtrace
		end
		
		app = ::Falcon::Server.middleware(rack_app, verbose: @options[:verbose])
		server = ::Falcon::Server.new(app, shared_endpoint, endpoint.protocol, endpoint.scheme)
		
		Process.setproctitle "Guard::Falcon HTTP Server: #{endpoint}"
		
		server.run
	end
	
	shared_endpoint.close
	
	return container
end

#running?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/guard/falcon/controller.rb', line 105

def running?
	!@container.nil?
end

#startObject



101
102
103
# File 'lib/guard/falcon/controller.rb', line 101

def start
	@container = run_server
end

#stopObject



114
115
116
117
118
119
# File 'lib/guard/falcon/controller.rb', line 114

def stop
	if @container
		@container.stop
		@container = nil
	end
end