Sha256: 923948a63467a0b9fcae10198eb7d9e095cf7019febd1e359b920622a5c2f98d

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

require 'erb'

module Thin
  module Controllers
    # System service controller to launch all servers which
    # config files are in a directory.
    class Service < Controller
      INITD_PATH          = '/etc/init.d/thin'
      DEFAULT_CONFIG_PATH = '/etc/thin'
      TEMPLATE            = File.dirname(__FILE__) + '/service.sh.erb'
    
      def initialize(options)
        @options = options
      
        raise PlatformNotSupported, 'Running as a service only supported on Linux' unless Thin.linux?
      end
    
      def config_path
        @options[:all] || DEFAULT_CONFIG_PATH
      end
    
      def start
        run :start
      end
    
      def stop
        run :stop
      end
    
      def restart
        run :restart
      end
    
      def install(config_files_path=DEFAULT_CONFIG_PATH)
        if File.exist?(INITD_PATH)
          log ">> Thin service already installed at #{INITD_PATH}"
        else
          log ">> Installing thin service at #{INITD_PATH} ..."
          sh "mkdir -p #{File.dirname(INITD_PATH)}"
          log "writing #{INITD_PATH}"        
          File.open(INITD_PATH, 'w') do |f|
            f << ERB.new(File.read(TEMPLATE)).result(binding)
          end
          sh "chmod +x #{INITD_PATH}" # Make executable
        end
      
        sh "mkdir -p #{config_files_path}"

        log ''
        log "To configure thin to start at system boot:"
        log "on RedHat like systems:"
        log "  sudo /sbin/chkconfig --level 345 #{NAME} on"
        log "on Debian like systems (Ubuntu):"
        log "  sudo /usr/sbin/update-rc.d -f #{NAME} defaults"
        log ''
        log "Then put your config files in #{config_files_path}"
      end
    
      private
        def run(command)
          Dir[config_path + '/*'].each do |config|
            log "[#{command}] #{config} ..."
            Command.run(command, :config => config, :daemonize => true)
          end
        end
      
        def sh(cmd)
          log cmd
          system(cmd)
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
thin-0.6.3-x86-mswin32-60 lib/thin/controllers/service.rb
thin-0.6.3 lib/thin/controllers/service.rb
thin-0.6.4-x86-mswin32-60 lib/thin/controllers/service.rb
thin-0.6.4 lib/thin/controllers/service.rb