Class: Thin::Controllers::Controller

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/thin/controllers/controller.rb

Overview

Controls one Thin server. Allow to start, stop, restart and configure a single thin server.

Direct Known Subclasses

Cluster, Service

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, debug, debug?, #log, log, #log_error, log_error, #silent, #silent=, silent?, #trace, trace, trace?

Constructor Details

#initialize(options) ⇒ Controller

Returns a new instance of Controller.



28
29
30
31
32
33
34
35
# File 'lib/thin/controllers/controller.rb', line 28

def initialize(options)
  @options = options
  
  if @options[:socket]
    @options.delete(:address)
    @options.delete(:port)
  end
end

Instance Attribute Details

#optionsObject

Command line options passed to the thin script



26
27
28
# File 'lib/thin/controllers/controller.rb', line 26

def options
  @options
end

Instance Method Details

#configObject



103
104
105
106
107
108
109
110
111
# File 'lib/thin/controllers/controller.rb', line 103

def config
  config_file = @options.delete(:config) || raise(OptionRequired, :config)

  # Stringify keys
  @options.keys.each { |o| @options[o.to_s] = @options.delete(o) }

  File.open(config_file, 'w') { |f| f << @options.to_yaml }
  log ">> Wrote configuration to #{config_file}"
end

#restartObject

Raises:



93
94
95
96
97
98
99
100
101
# File 'lib/thin/controllers/controller.rb', line 93

def restart
  raise OptionRequired, :pid unless @options[:pid]
  
  tail_log(@options[:log]) do
    if Server.restart(@options[:pid])
      wait_for_file :creation, @options[:pid]
    end
  end
end

#startObject



37
38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/thin/controllers/controller.rb', line 37

def start
  # Constantize backend class
  @options[:backend] = eval(@options[:backend], TOPLEVEL_BINDING) if @options[:backend]
  
  server = Server.new(@options[:socket] || @options[:address], # Server detects kind of socket
                      @options[:port],                         # Port ignored on UNIX socket
                      @options)
  
  # Set options
  server.pid_file                       = @options[:pid]
  server.log_file                       = @options[:log]
  server.timeout                        = @options[:timeout]
  server.maximum_connections            = @options[:max_conns]
  server.maximum_persistent_connections = @options[:max_persistent_conns]
  server.threaded                       = @options[:threaded]
  server.no_epoll                       = @options[:no_epoll] if server.backend.respond_to?(:no_epoll=)

  # Detach the process, after this line the current process returns
  server.daemonize if @options[:daemonize]

  # +config+ must be called before changing privileges since it might require superuser power.
  server.config
  
  server.change_privilege @options[:user], @options[:group] if @options[:user] && @options[:group]

  # If a Rack config file is specified we eval it inside a Rack::Builder block to create
  # a Rack adapter from it. Or else we guess which adapter to use and load it.
  if @options[:rackup]
    server.app = load_rackup_config
  else
    server.app = load_adapter
  end

  # If a prefix is required, wrap in Rack URL mapper
  server.app = Rack::URLMap.new(@options[:prefix] => server.app) if @options[:prefix]

  # If a stats URL is specified, wrap in Stats adapter
  server.app = Stats::Adapter.new(server.app, @options[:stats]) if @options[:stats]

  # Register restart procedure which just start another process with same options,
  # so that's why this is done here.
  server.on_restart { Command.run(:start, @options) }

  server.start
end

#stopObject

Raises:



83
84
85
86
87
88
89
90
91
# File 'lib/thin/controllers/controller.rb', line 83

def stop
  raise OptionRequired, :pid unless @options[:pid]

  tail_log(@options[:log]) do
    if Server.kill(@options[:pid], @options[:force] ? 0 : (@options[:timeout] || 60))
      wait_for_file :deletion, @options[:pid]
    end
  end
end