Class: Falcon::Service::Application

Inherits:
Proxy show all
Defined in:
lib/falcon/service/application.rb

Overview

Implements an application server using an internal clear-text proxy.

Instance Method Summary collapse

Methods inherited from Proxy

#authority, #endpoint, #name, #protocol, #root, #scheme, #ssl_context

Methods inherited from Generic

#include?, #logger, #name, wrap

Constructor Details

#initialize(environment) ⇒ Application

Returns a new instance of Application.



32
33
34
35
36
# File 'lib/falcon/service/application.rb', line 32

def initialize(environment)
  super
  
  @bound_endpoint = nil
end

Instance Method Details

#countObject

Number of instances to start.



47
48
49
# File 'lib/falcon/service/application.rb', line 47

def count
  @environment.evaluator.count
end

#middlewareObject

The middleware that will be served by this application.



40
41
42
43
# File 'lib/falcon/service/application.rb', line 40

def middleware
  # In a multi-threaded container, we don't want to modify the shared evaluator's cache, so we create a new evaluator:
  @environment.evaluator.middleware
end

#preload!Object

Preload any resources specified by the environment.



52
53
54
55
56
57
58
59
60
# File 'lib/falcon/service/application.rb', line 52

def preload!
  if scripts = @evaluator.preload
    scripts.each do |path|
      Console.logger.info(self) {"Preloading #{path}..."}
      full_path = File.expand_path(path, self.root)
      load(full_path)
    end
  end
end

#setup(container) ⇒ Object

Setup instances of the application into the container.



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
# File 'lib/falcon/service/application.rb', line 78

def setup(container)
  protocol = self.protocol
  scheme = self.scheme
  
  run_options = {
    name: self.name,
    restart: true,
  }
  
  run_options[:count] = count unless count.nil?
  
  container.run(**run_options) do |instance|
    Async do |task|
      Console.logger.info(self) {"Starting application server for #{self.root}..."}
      
      server = Server.new(self.middleware, @bound_endpoint, protocol: protocol, scheme: scheme)
      
      server.run
      
      instance.ready!
      
      task.children.each(&:wait)
    end
  end
  
  super
end

#startObject

Prepare the bound endpoint for the application instances. Invoke #preload! to load shared resources into the parent process.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/falcon/service/application.rb', line 64

def start
  Console.logger.info(self) {"Binding to #{self.endpoint}..."}
  
  @bound_endpoint = Async::Reactor.run do
    Async::IO::SharedEndpoint.bound(self.endpoint)
  end.wait
  
  preload!
  
  super
end

#stopObject

Close the bound endpoint.



107
108
109
110
111
112
# File 'lib/falcon/service/application.rb', line 107

def stop
  @bound_endpoint&.close
  @bound_endpoint = nil
  
  super
end