Class: Falcon::Controller::Serve

Inherits:
Async::Container::Controller
  • Object
show all
Defined in:
lib/falcon/controller/serve.rb

Direct Known Subclasses

Proxy, Redirect

Instance Method Summary collapse

Constructor Details

#initialize(command, **options) ⇒ Serve

Returns a new instance of Serve.



33
34
35
36
37
38
39
40
41
# File 'lib/falcon/controller/serve.rb', line 33

def initialize(command, **options)
  @command = command
  
  @endpoint = nil
  @bound_endpoint = nil
  @debug_trap = Async::IO::Trap.new(:USR1)
  
  super(**options)
end

Instance Method Details

#create_containerObject



43
44
45
# File 'lib/falcon/controller/serve.rb', line 43

def create_container
  @command.container_class.new
end

#endpointObject



47
48
49
# File 'lib/falcon/controller/serve.rb', line 47

def endpoint
  @command.endpoint
end

#load_appObject



51
52
53
# File 'lib/falcon/controller/serve.rb', line 51

def load_app
  @command.load_app
end

#nameObject



67
68
69
# File 'lib/falcon/controller/serve.rb', line 67

def name
  "Falcon Server"
end

#setup(container) ⇒ Object



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

def setup(container)
  container.run(name: self.name, restart: true, **@command.container_options) do |instance|
    Async do |task|
      # Load one app instance per container:
      app = self.load_app
      
      task.async do
        if @debug_trap.install!
          Async.logger.info(instance) do
            "- Per-process status: kill -USR1 #{Process.pid}"
          end
        end
        
        @debug_trap.trap do
          Async.logger.info(self) do |buffer|
            task.reactor.print_hierarchy(buffer)
          end
        end
      end
      
      server = Falcon::Server.new(app, @bound_endpoint, @endpoint.protocol, @endpoint.scheme)
      
      server.run
      
      instance.ready!
      
      task.children.each(&:wait)
    end
  end
end

#startObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/falcon/controller/serve.rb', line 55

def start
  @endpoint ||= self.endpoint
  
  @bound_endpoint = Async::Reactor.run do
    Async::IO::SharedEndpoint.bound(@endpoint)
  end.wait
  
  @debug_trap.ignore!
  
  super
end

#stopObject



102
103
104
105
106
107
108
# File 'lib/falcon/controller/serve.rb', line 102

def stop(*)
  @bound_endpoint&.close
  
  @debug_trap.default!
  
  super
end