#!/usr/bin/env ruby # Try to load external libs, helpers and constants begin require "rubygems" require "optparse" require 'daemons' require_relative "../lib/pushyd/conf" rescue LoadError => e raise "EXITING: some basic libs were not found (#{e.message})" end # Handle configuration begin # Defaults cmd_config = nil cmd_logfile = nil # Init Chamber-based configuration from Gemspec Conf.init File.dirname(__FILE__) + "/../" Conf.app_env = "production" # Parse options and check compliance OptionParser.new do |opts| opts.banner = "Usage: #{File.basename $PROGRAM_NAME} [options] start|stop" opts.on("-l", "--log LOGFILE") { |path| cmd_logfile = File.expand_path(path)} opts.on("-c", "--config CONFIGFILE") { |path| cmd_config = File.expand_path(path)} opts.on("-e", "--environment ENV") { |env| Conf.app_env = env } opts.on("", "--dev") { Conf.app_env = "development" } end.order!(ARGV) # Load Chamber-based configuration Conf.prepare config: cmd_config, logfile: cmd_logfile # Override log file Conf[:log] ||= {} Conf[:log][:file] = cmd_logfile.to_s if cmd_logfile rescue OptionParser::InvalidOption => e abort "EXITING: InvalidOption: #{e.message} \n #{e.backtrace.to_yaml}" rescue ConfigParseError => e abort "EXITING: ConfigParseError: #{e.message} \n #{e.backtrace.to_yaml}" rescue StandardError => e abort "EXITING: StandardError: #{e.message} \n #{e.backtrace.to_yaml}" else end # Display final configuration, quit if config dump requested puts "--- #{Conf.app_name} #{Conf.app_ver}" puts "Environment \t #{Conf.app_env}" puts "Config files \t #{Conf.files}" puts "Started at \t #{Conf.app_started}" puts "Loging to file \t #{Conf[:log][:file]}" if Conf[:log].is_a? Enumerable puts puts Conf.dump # Run daemon run_options = { ontop: false, :backtrace => true, :multiple => false } Daemons.run_proc('pushy-daemon', run_options) do # Load code puts "--- load code" require_relative "../lib/pushyd" # Start daemon puts "--- start daemon" PushyDaemon::Daemon.run end