Class: Thin::Cluster

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

Overview

Control a set of servers. Generate start and stop commands and run them.

Constant Summary collapse

@@thin =
'thin'

Instance Attribute Summary collapse

Attributes included from Logging

#silent

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, address, first_port, size) ⇒ Cluster

Create a new cluster of servers bound to host on ports first_port to first_port + size - 1.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/thin/cluster.rb', line 17

def initialize(dir, address, first_port, size)
  @address    = address
  @first_port = first_port
  @size       = size
  
  @log_file   = 'thin.log'
  @pid_file   = 'thin.pid'
  
  @timeout    = 60 # sec
  
  Dir.chdir dir if dir
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



7
8
9
# File 'lib/thin/cluster.rb', line 7

def address
  @address
end

#environmentObject

Returns the value of attribute environment.



6
7
8
# File 'lib/thin/cluster.rb', line 6

def environment
  @environment
end

#first_portObject (readonly)

Returns the value of attribute first_port.



7
8
9
# File 'lib/thin/cluster.rb', line 7

def first_port
  @first_port
end

#groupObject

Returns the value of attribute group.



6
7
8
# File 'lib/thin/cluster.rb', line 6

def group
  @group
end

#log_fileObject

Returns the value of attribute log_file.



6
7
8
# File 'lib/thin/cluster.rb', line 6

def log_file
  @log_file
end

#pid_fileObject

Returns the value of attribute pid_file.



6
7
8
# File 'lib/thin/cluster.rb', line 6

def pid_file
  @pid_file
end

#sizeObject (readonly)

Returns the value of attribute size.



7
8
9
# File 'lib/thin/cluster.rb', line 7

def size
  @size
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/thin/cluster.rb', line 6

def timeout
  @timeout
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/thin/cluster.rb', line 6

def user
  @user
end

Class Method Details

.thin=(value) ⇒ Object

Script to run



10
11
12
# File 'lib/thin/cluster.rb', line 10

def self.thin=(value)
  @@thin = value
end

Instance Method Details

#log_file_for(port) ⇒ Object



91
92
93
# File 'lib/thin/cluster.rb', line 91

def log_file_for(port)
  include_port_number @log_file, port
end

#pid_file_for(port) ⇒ Object



95
96
97
# File 'lib/thin/cluster.rb', line 95

def pid_file_for(port)
  include_port_number @pid_file, port
end

#pid_for(port) ⇒ Object



99
100
101
# File 'lib/thin/cluster.rb', line 99

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

#restartObject

Restart the servers one at the time. Prevent downtime by making sure only one is stopped at the time. See blog.carlmercier.com/2007/09/07/a-better-approach-to-restarting-a-mongrel-cluster/



83
84
85
86
87
88
89
# File 'lib/thin/cluster.rb', line 83

def restart
  with_each_instance do |port|
    stop_on_port port
    sleep 0.1 # Let the OS do his thang
    start_on_port port
  end
end

#startObject

Start the servers



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

def start
  with_each_instance do |port|
    start_on_port port
  end
end

#start_on_port(port) ⇒ Object

Start the server on a single port



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/thin/cluster.rb', line 38

def start_on_port(port)
  logc "Starting #{address}:#{port} ... "
  
  run :start, :port        => port,
              :address     => @address,
              :environment => @environment,
              :daemonize   => true,
              :pid_file    => pid_file_for(port),
              :log_file    => log_file_for(port),
              :user        => @user,
              :group       => @group,
              :timeout     => @timeout,
              :trace       => @trace
  
  if wait_until_pid(:exist, port)
    log "started in #{pid_for(port)}" if $?.success?
  else
    log 'failed to start'
  end
end

#stopObject

Stop the servers



60
61
62
63
64
# File 'lib/thin/cluster.rb', line 60

def stop
  with_each_instance do |port|
    stop_on_port port
  end
end

#stop_on_port(port) ⇒ Object

Stop the server running on port



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/thin/cluster.rb', line 67

def stop_on_port(port)
  logc "Stopping #{address}:#{port} ... "
  
  run :stop, :pid_file => pid_file_for(port),
             :timeout  => @timeout
  
  if wait_until_pid(!:exist, port)
    log 'stopped' if $?.success?
  else
    log 'failed to stop'
  end
end