Skip to content

Thin Server Cluster

Gurpartap edited this page Mar 18, 2013 · 2 revisions

Option 1: Ruby config

app_root = "/apps/acmecorp.com"
servers = 5
port = 4000

Cognizant.application "acmecorp.com" do |app|
  servers.times do |n|
    app.monitor "thin-#{n}" do
      autostart!
      group "thin"
      uid "www-data"
      gid "www-data"

      env RACK_ENV: "production"
      chdir "#{app_root}/current"

      daemonize false
      pidfile "#{app_root}/shared/tmp/pids/thin.400#{n}.pid"

      start_command   "bundle exec thin start   --only #{n} --servers #{servers} --port #{port}"
      stop_command    "bundle exec thin stop    --only #{n} --servers #{servers} --port #{port}"
      restart_command "bundle exec thin restart --only #{n} --servers #{servers} --port #{port}"

      check :flapping, times: 3, within: 1.minute, retry_after: 15.seconds, retries: 10
      check :transition, from: :running, to: :stopped do |process|
        `say a thin server has stopped` # send an email, etc.
      end

      check :cpu_usage,    above: 50.percent,    every: 5.seconds, times: 5,      do: :restart
      check :memory_usage, above: 300.megabytes, every: 5.seconds, times: [3, 5], do: :restart
    end
  end
end

Option 2: YAML config

thin: &thin
  autostart: true
  group: thin
  uid: www-data
  gid: www-data
  env: 
    RACK_ENV: production
  chdir: /apps/acmecorp.com/current
  daemonize: false
  checks:
    flapping:
      times: 3
      within: 1
      retry_after: 15
      retries: 10
    # transition trigger is not useful in YAML config as blocks are not possible
    cpu_usage:
      above: 50
      every: 5
      times: 5
      do: stop
    memory_usage:
      above: 314572800 # 300 megabytes in bytes
      every: 5
      times: [3, 5]
      do: stop

applications:
  acmecorp.com:
    monitor:
      thin-0:
        <<: *thin
        start_command:   bundle exec thin start   --only 0 --servers 3 --port 4000
        stop_command:    bundle exec thin stop    --only 0 --servers 3 --port 4000
        restart_command: bundle exec thin restart --only 0 --servers 3 --port 4000
        pidfile: /apps/acmecorp.com/shared/tmp/pids/thin.4000.pid
      thin-1:
        <<: *thin
        start_command:   bundle exec thin start   --only 1 --servers 3 --port 4000
        stop_command:    bundle exec thin stop    --only 1 --servers 3 --port 4000
        restart_command: bundle exec thin restart --only 1 --servers 3 --port 4000
        pidfile: /apps/acmecorp.com/shared/tmp/pids/thin.4001.pid
      thin-2:
        <<: *thin
        start_command:   bundle exec thin start   --only 2 --servers 3 --port 4000
        stop_command:    bundle exec thin stop    --only 2 --servers 3 --port 4000
        restart_command: bundle exec thin restart --only 2 --servers 3 --port 4000
        pidfile: /apps/acmecorp.com/shared/tmp/pids/thin.4002.pid