Module: RackCluster

Defined in:
lib/app-deploy/rack_cluster.rb

Class Method Summary collapse

Class Method Details

.each(path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/app-deploy/rack_cluster.rb', line 4

def each path
  config_orig = {}
  rack_opts = AppDeploy.extract_config(path){ |opt, value|
    case opt
      when 'environment'; "--env #{value}"

      when *%w[server host]
        config_orig[opt.to_sym] = value
        "--#{opt} #{value}"

      when *%w[user group chdir servers require rackup daemonize port pid log]
        config_orig[opt.to_sym] = value
        nil # rack doesn't have this option

      else
        "--#{opt} #{value}"

    end
  }

  config_orig[:servers].times{ |n|
    config = config_orig.dup
    ruby_opts = ''
    rack_opts = ''

    config[:port] += n if config[:port]
    config[:pid]   = RackCluster.pid_path(config[:pid], config[:port])

    if config[:daemonize]
      config[:log] = RackCluster.log_path(config[:log], config[:port])

      args = [:pid, :log, :user, :group, :chdir].map{ |kind|
        config.send(:[], kind)
      }.join("', '")

      init_script = "RackDaemon.daemonize('#{args}')"
      rack_daemon = File.dirname(__FILE__) + '/rack_daemon.rb'
      ruby_opts  += " -r #{rack_daemon} -e \"#{init_script}\""
    end

    rack_opts += " --port #{config[:port]}" if config[:port]
    rack_opts += " --pid #{config[:pid]}"   if config[:pid]

    yield(config, ruby_opts, rack_opts)
  }

end

.include_server_number(path, number) ⇒ Object

extracted from thin Add the server port or number in the filename so each instance get its own file



68
69
70
71
# File 'lib/app-deploy/rack_cluster.rb', line 68

def include_server_number path, number
  ext = File.extname(path)
  path.gsub(/#{ext}$/, ".#{number}#{ext}")
end

.log_path(path, port) ⇒ Object



61
62
63
# File 'lib/app-deploy/rack_cluster.rb', line 61

def log_path path, port
  File.expand_path(RackCluster.include_server_number(path, port))
end

.pid_path(path, port) ⇒ Object



57
58
59
# File 'lib/app-deploy/rack_cluster.rb', line 57

def pid_path path, port
  RackCluster.include_server_number(path, port)
end

.start(config, ruby_opts, rack_opts) ⇒ Object



52
53
54
55
# File 'lib/app-deploy/rack_cluster.rb', line 52

def start config, ruby_opts, rack_opts
  puts "Starting #{config[:server]} on #{config[:host]}:#{config[:port]}..."
  sh "rackup#{ruby_opts}#{rack_opts} #{config[:rackup]}"
end