Class: Thin::Controllers::Cluster

Inherits:
Controller show all
Defined in:
lib/thin/controllers/cluster.rb

Overview

Control a set of servers.

  • Generate start and stop commands and run them.

  • Inject the port or socket number in the pid and log filenames.

Servers are started throught the thin command-line script.

Constant Summary collapse

CLUSTER_OPTIONS =

Cluster only options that should not be passed in the command sent to the indiviual servers.

[:servers, :only]

Instance Attribute Summary

Attributes inherited from Controller

#options

Instance Method Summary collapse

Methods inherited from Controller

#config

Methods included from Logging

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

Constructor Details

#initialize(options) ⇒ Cluster

Create a new cluster of servers launched using options.



13
14
15
16
17
# File 'lib/thin/controllers/cluster.rb', line 13

def initialize(options)
  super
  # Cluster can only contain daemonized servers
  @options.merge!(:daemonize => true)
end

Instance Method Details

#addressObject



20
# File 'lib/thin/controllers/cluster.rb', line 20

def address;    @options[:address]  end

#first_portObject



19
# File 'lib/thin/controllers/cluster.rb', line 19

def first_port; @options[:port]     end

#log_fileObject



23
# File 'lib/thin/controllers/cluster.rb', line 23

def log_file;   @options[:log]      end

#log_file_for(number) ⇒ Object



72
73
74
# File 'lib/thin/controllers/cluster.rb', line 72

def log_file_for(number)
  include_server_number log_file, number
end

#onlyObject



25
# File 'lib/thin/controllers/cluster.rb', line 25

def only;       @options[:only]     end

#pid_fileObject



22
# File 'lib/thin/controllers/cluster.rb', line 22

def pid_file;   @options[:pid]      end

#pid_file_for(number) ⇒ Object



76
77
78
# File 'lib/thin/controllers/cluster.rb', line 76

def pid_file_for(number)
  include_server_number pid_file, number
end

#pid_for(number) ⇒ Object



84
85
86
# File 'lib/thin/controllers/cluster.rb', line 84

def pid_for(number)
  File.read(pid_file_for(number)).chomp.to_i
end

#restartObject

Stop and start the servers.



56
57
58
59
60
# File 'lib/thin/controllers/cluster.rb', line 56

def restart
  stop
  sleep 0.1 # Let's breath a bit shall we ?
  start
end

#server_id(number) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/thin/controllers/cluster.rb', line 62

def server_id(number)
  if socket
    socket_for(number)
  elsif swiftiply?
    [address, first_port, number].join(':')
  else
    [address, number].join(':')
  end
end

#sizeObject



24
# File 'lib/thin/controllers/cluster.rb', line 24

def size;       @options[:servers]  end

#socketObject



21
# File 'lib/thin/controllers/cluster.rb', line 21

def socket;     @options[:socket]   end

#socket_for(number) ⇒ Object



80
81
82
# File 'lib/thin/controllers/cluster.rb', line 80

def socket_for(number)
  include_server_number socket, number
end

#startObject

Start the servers



32
33
34
# File 'lib/thin/controllers/cluster.rb', line 32

def start
  with_each_server { |n| start_server n }
end

#start_server(number) ⇒ Object

Start a single server



37
38
39
40
41
# File 'lib/thin/controllers/cluster.rb', line 37

def start_server(number)
  log "Starting server on #{server_id(number)} ... "

  run :start, number
end

#stopObject

Stop the servers



44
45
46
# File 'lib/thin/controllers/cluster.rb', line 44

def stop
  with_each_server { |n| stop_server n }
end

#stop_server(number) ⇒ Object

Stop a single server



49
50
51
52
53
# File 'lib/thin/controllers/cluster.rb', line 49

def stop_server(number)
  log "Stopping server on #{server_id(number)} ... "

  run :stop, number
end

#swiftiply?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/thin/controllers/cluster.rb', line 27

def swiftiply?
  @options.has_key?(:swiftiply)
end