#!/usr/bin/env ruby # Description # # Author:: Robert Sharp # Copyright:: Copyright (c) 2010 Robert Sharp # License:: Open Software Licence v3.0 # # This software is licensed for use under the Open Software Licence v. 3.0 # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php # and in the file copyright.txt. Under the terms of this licence, all derivative works # must themselves be licensed under the Open Software Licence v. 3.0 # # # # Start the Jerbil Server # require 'jerbil' require 'jerbil/servers' require 'jerbil/config' require 'jerbil/version' require 'jerbil/support' require 'jerbil/monitor' require 'jellog' require 'jeckyl' require 'socket' require 'drb' require 'rubygems' require 'daemons' require 'optparse' config_file = nil # let it be the default daemonize = true log_to_syslog = true verbose = false env = nil OptionParser.new do |opts| opts.banner = "Usage: jerbild [opts]" opts.separator "" opts.separator " start the Jerbil Daemon" opts.separator "" opts.on("-n", "--no-daemon", "Do not daemonize") do daemonize = false end opts.on("-S", "--no-syslog", "Do not log to syslog") do log_to_syslog = false end opts.on("-c", "--config [file]", String, "use this config file to find Jerbil" ) do |cfile| if FileTest.readable?(cfile) then config_file = cfile else puts "Config file cannot be read." exit 1 end end opts.on("-e", "--environment [TYPE]", [:prod, :test, :dev], "Select env (prod, test, dev), default is prod") do |e| env = e end opts.on("-V", "--verbose", "output more information about what is going on ") do verbose = true end opts.on("-h", "--help", "Provide Help") do |h| opts.separator "" puts opts exit 0 end end.parse! puts "Starting the Jerbil Server (#{Jerbil::Ident})" if verbose begin config = Jerbil.get_config(config_file) #my_self = Jerbil.get_local_server(config_file) rescue Jerbil::JerbilConfigError => err puts "Error in Jerbil Config File: #{err.message}" exit 1 rescue Jerbil::MissingServer puts "Failed to find a local server in the config file. Check environment settings" exit 1 end if env then config[:environment] = env end if verbose then puts "Config hash:" config.each_pair do |key, value| puts " #{key}: #{value}" end end # create a private key for jerbil pkey = Jerbil::Support.create_private_key(:jerbil, config[:environment], config[:key_dir]) puts "Created Private Key" if verbose # check the pid directory has been set unless config[:pid_dir] then puts "You must provide a writeable pid_dir in the config file!" exit 1 end # prevent the server from generating syslogs Jellog::Logger.disable_syslog unless log_to_syslog # create a local log dopts = {:backtrace=>true, :app_name=>'jerbil_daemon', :log_dir=>config[:log_dir], :log_output=>true, :dir_mode=>:normal, :dir=>config[:pid_dir] } logger = nil begin if daemonize then puts "Daemonizing now" if verbose Daemons.daemonize(dopts) log_opts = Jellog::Config.intersection(config) #log_opts[:log_level] = :debug logger = Jellog::Logger.new('jerbil_sd', log_opts) #config = config.merge(log_opts) # add them back! else puts "Running in the foreground" if verbose logger = $stderr end rescue => err File.open('/tmp/jerbil_panic.log', 'w') do |pfile| pfile.puts "Panic during daemonization:" pfile.puts "#{err.class.to_s}: #{err.message}" err.backtrace.each do |bt| pfile.puts(bt) end end exit 1 end logger.puts "Logging started for Jerbil Daemon" if verbose logger.puts "Daemonized" if verbose && daemonize # will only output messages to screen if not daemonised and therefore safe to # assume verbose anyway. my_self = Jerbil::Servers.create_local_server(config[:environment], pkey) logger.puts "Created local server: #{my_self.ident}" jerbild = Jerbil::Broker.new(config, pkey) logger.puts "Started Broker" logger.puts "Starting Monitor" # create a separate process to look for other servers # and log them to this one mon_pid = fork do monitor = Jerbil::Monitor.new(config, pkey) # exits automatically end DRb.start_service(my_self.drb_address, jerbild) logger.puts "Started DRb" # now create the pid files jer_pid = Jerbil::Support.write_pid_file(:jerbil, config[:environment], config[:pid_dir]) logger.puts "Written Jerbil pid #{jer_pid} to pid file" # Jerbil::Support.write_pid_file(:jmonitor, config[:environment], config[:pid_dir], mon_pid) # # logger.puts "Written Monitor pid #{mon_pid} to pid file" $0 = "jerbild-#{config[:environment]}" logger.puts "Changed process name for #{my_self.ident}" $SAFE = 1 # using DRb, so prevent anything nasty DRb.thread.join