Module: AppDeploy::DaemonCluster

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

Class Method Summary collapse

Class Method Details

.each(path) ⇒ Object



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
# File 'lib/app-deploy/daemon_cluster.rb', line 6

def each path
  config_orig = YAML.load(File.read(path)).inject({}){ |h, kv|
    k, v = kv
    h[k.to_sym] = v
    h
  }

  config_orig = { :servers => 1,
                  :log     => 'log/daemon_cluster.log',
                  :pid     => 'tmp/pids/daemon_cluster.pid'
                }.merge(config_orig)

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

    config[:num] = n
    config[:pid] = DaemonCluster.pid_path(config, n)
    config[:log] = DaemonCluster.log_path(config, n)

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

    init_script = "require 'app-deploy/daemon'; AppDeploy::Daemon.daemonize(#{args});"
    ruby_opts   = "-r rubygems -e \"#{init_script} load('#{config[:script]}')\""

    yield( config, "#{config[:ruby]} #{ruby_opts} #{config[:args]}" )
  }
end

.log_path(config, number) ⇒ Object



48
49
50
51
52
# File 'lib/app-deploy/daemon_cluster.rb', line 48

def log_path config, number
  # log should expand path since daemons' working dir is different
  path, script, args = config[:log], config[:script], config[:args]
  File.expand_path(DaemonCluster.path_with_number(path, script, args, number))
end

.path_with_number(path, script, args, number) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/app-deploy/daemon_cluster.rb', line 54

def path_with_number path, script, args, number
  name = (args ? File.basename("#{script} #{args}") :
                 File.basename(script)               ).gsub(/\s+/, '_')

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

.pid_path(config, number) ⇒ Object



43
44
45
46
# File 'lib/app-deploy/daemon_cluster.rb', line 43

def pid_path config, number
  path, script, args = config[:pid], config[:script], config[:args]
  DaemonCluster.path_with_number(path, script, args, number)
end

.start(config, cmd) ⇒ Object



37
38
39
40
41
# File 'lib/app-deploy/daemon_cluster.rb', line 37

def start config, cmd
  puts "Starting ##{config[:num]} #{config[:ruby]} #{config[:script]} #{config[:args]}..."
  sh cmd
  puts
end